diff --git a/app/Http/Controllers/BaseAPIController.php b/app/Http/Controllers/BaseAPIController.php index 7ace9bb31cad..ef7460b6480c 100644 --- a/app/Http/Controllers/BaseAPIController.php +++ b/app/Http/Controllers/BaseAPIController.php @@ -38,11 +38,12 @@ use Utils; * description="Find out more about Invoice Ninja", * url="https://www.invoiceninja.com" * ), + * security={"api_key": {}}, * @SWG\SecurityScheme( * securityDefinition="api_key", * type="apiKey", * in="header", - * name="TOKEN" + * name="X-Ninja-Token" * ) * ) */ diff --git a/app/Http/Controllers/ContactApiController.php b/app/Http/Controllers/ContactApiController.php new file mode 100644 index 000000000000..4e93f1518d43 --- /dev/null +++ b/app/Http/Controllers/ContactApiController.php @@ -0,0 +1,179 @@ +contactRepo = $contactRepo; + } + + /** + * @SWG\Get( + * path="/contacts", + * summary="List contacts", + * tags={"contact"}, + * @SWG\Response( + * response=200, + * description="A list of contacts", + * @SWG\Schema(type="array", @SWG\Items(ref="#/definitions/Contact")) + * ), + * @SWG\Response( + * response="default", + * description="an ""unexpected"" error" + * ) + * ) + */ + public function index() + { + $contacts = Contact::scope() + ->withTrashed() + ->orderBy('created_at', 'desc'); + + return $this->listResponse($contacts); + } + + /** + * @SWG\Get( + * path="/contacts/{contact_id}", + * summary="Retrieve a contact", + * tags={"contact"}, + * @SWG\Parameter( + * in="path", + * name="contact_id", + * type="integer", + * required=true + * ), + * @SWG\Response( + * response=200, + * description="A single contact", + * @SWG\Schema(type="object", @SWG\Items(ref="#/definitions/Contact")) + * ), + * @SWG\Response( + * response="default", + * description="an ""unexpected"" error" + * ) + * ) + */ + public function show(ContactRequest $request) + { + return $this->itemResponse($request->entity()); + } + + /** + * @SWG\Post( + * path="/contacts", + * tags={"contact"}, + * summary="Create a contact", + * @SWG\Parameter( + * in="body", + * name="contact", + * @SWG\Schema(ref="#/definitions/Contact") + * ), + * @SWG\Response( + * response=200, + * description="New contact", + * @SWG\Schema(type="object", @SWG\Items(ref="#/definitions/Contact")) + * ), + * @SWG\Response( + * response="default", + * description="an ""unexpected"" error" + * ) + * ) + */ + public function store(CreateContactRequest $request) + { + $contact = $this->contactRepo->save($request->input()); + + return $this->itemResponse($contact); + } + + /** + * @SWG\Put( + * path="/contacts/{contact_id}", + * tags={"contact"}, + * summary="Update a contact", + * @SWG\Parameter( + * in="path", + * name="contact_id", + * type="integer", + * required=true + * ), + * @SWG\Parameter( + * in="body", + * name="contact", + * @SWG\Schema(ref="#/definitions/Contact") + * ), + * @SWG\Response( + * response=200, + * description="Updated contact", + * @SWG\Schema(type="object", @SWG\Items(ref="#/definitions/Contact")) + * ), + * @SWG\Response( + * response="default", + * description="an ""unexpected"" error" + * ) + * ) + * + * @param mixed $publicId + */ + public function update(UpdateContactRequest $request, $publicId) + { + if ($request->action) { + return $this->handleAction($request); + } + + $data = $request->input(); + $data['public_id'] = $publicId; + $expense = $this->contactRepo->save($data, $request->entity()); + + return $this->itemResponse($contact); + } + + /** + * @SWG\Delete( + * path="/contacts/{contact_id}", + * tags={"contact"}, + * summary="Delete a contact", + * @SWG\Parameter( + * in="path", + * name="contact_id", + * type="integer", + * required=true + * ), + * @SWG\Response( + * response=200, + * description="Deleted contact", + * @SWG\Schema(type="object", @SWG\Items(ref="#/definitions/Contact")) + * ), + * @SWG\Response( + * response="default", + * description="an ""unexpected"" error" + * ) + * ) + */ + public function destroy(UpdateContactRequest $request) + { + $contact = $request->entity(); + + $this->contactRepo->delete($contact); + + return $this->itemResponse($contact); + } +} diff --git a/app/Http/Requests/ContactRequest.php b/app/Http/Requests/ContactRequest.php new file mode 100644 index 000000000000..9c8750ad504d --- /dev/null +++ b/app/Http/Requests/ContactRequest.php @@ -0,0 +1,8 @@ +user()->can('create', ENTITY_CONTACT); + } + + /** + * Get the validation rules that apply to the request. + * + * @return array + */ + public function rules() + { + return [ + 'first_name' => 'required', + 'last_name' => 'required', + 'email' => 'required', + ]; + } +} diff --git a/app/Http/Requests/UpdateContactRequest.php b/app/Http/Requests/UpdateContactRequest.php new file mode 100644 index 000000000000..e6a40d18bb56 --- /dev/null +++ b/app/Http/Requests/UpdateContactRequest.php @@ -0,0 +1,30 @@ +user()->can('edit', $this->entity()); + } + + /** + * Get the validation rules that apply to the request. + * + * @return array + */ + public function rules() + { + return [ + 'first_name' => 'required', + 'last_name' => 'required', + 'email' => 'required', + ]; + } +} diff --git a/app/Http/routes.php b/app/Http/routes.php index 669297016cb8..336220302b8e 100644 --- a/app/Http/routes.php +++ b/app/Http/routes.php @@ -307,6 +307,7 @@ Route::group(['middleware' => 'api', 'prefix' => 'api/v1'], function () { Route::get('accounts', 'AccountApiController@show'); Route::put('accounts', 'AccountApiController@update'); Route::resource('clients', 'ClientApiController'); + Route::resource('contacts', 'ContactApiController'); Route::get('quotes', 'QuoteApiController@index'); Route::get('download/{invoice_id}', 'InvoiceApiController@download'); Route::resource('invoices', 'InvoiceApiController'); diff --git a/app/Ninja/Transformers/ContactTransformer.php b/app/Ninja/Transformers/ContactTransformer.php index 4fd72ffde690..4e6ad4a633aa 100644 --- a/app/Ninja/Transformers/ContactTransformer.php +++ b/app/Ninja/Transformers/ContactTransformer.php @@ -6,6 +6,8 @@ use App\Models\Contact; /** * Class ContactTransformer. + * + * @SWG\Definition(definition="Contact", @SWG\Xml(name="Contact")) */ class ContactTransformer extends EntityTransformer { @@ -13,6 +15,17 @@ class ContactTransformer extends EntityTransformer * @param Contact $contact * * @return array + * + * @SWG\Property(property="id", type="integer", example=1, readOnly=true) + * @SWG\Property(property="first_name", type="string", example="John") + * @SWG\Property(property="last_name", type="string", example="Doe") + * @SWG\Property(property="email", type="string", example="john.doe@company.com") + * @SWG\Property(property="updated_at", type="integer", example=1451160233, readOnly=true) + * @SWG\Property(property="archived_at", type="integer", example=1451160233, readOnly=true) + * @SWG\Property(property="is_primary", type="boolean", example=false) + * @SWG\Property(property="phone", type="string", example="(212) 555-1212") + * @SWG\Property(property="last_login", type="string", format="date-time", example="2016-01-01 12:10:00") + * @SWG\Property(property="send_invoice", type="boolean", example=false) */ public function transform(Contact $contact) {