From 1e751ecc324a1a98a78448cc2c17e79634d43cfc Mon Sep 17 00:00:00 2001 From: hazim Date: Thu, 9 May 2019 12:26:14 +0800 Subject: [PATCH] Cleaned-BackEnd-Contact --- .../Contacts/DeleteContact.php | 24 --------- .../Contacts/ListContacts.php | 19 ------- .../Contacts/UpdateContact.php | 46 ---------------- .../DataTransferObjects/ContactObject.php | 2 +- .../Contacts/Services/DeletesContact.php | 42 +++++++++++++++ .../Contacts/Services/ListsContact.php | 2 +- .../Contacts/CreateContactLogic.php | 2 +- .../Contacts/DeleteContactLogic.php | 35 ++++++++++++ .../Contacts/UpdateContactLogic.php | 5 +- .../Contacts/ContactController.php | 53 ------------------- .../Contacts/CreateContactController.php | 1 + .../Contacts/DeleteContactController.php | 18 +++++++ .../Contacts/ListContactController.php | 2 - .../Contacts/UpdateContactController.php | 6 ++- app/Http/Resources/Contact.php | 2 +- routes/api.php | 8 +-- 16 files changed, 110 insertions(+), 157 deletions(-) delete mode 100644 app/Classes/ControllersLogic/Contacts/DeleteContact.php delete mode 100644 app/Classes/ControllersLogic/Contacts/ListContacts.php delete mode 100644 app/Classes/ControllersLogic/Contacts/UpdateContact.php create mode 100644 app/Classes/Modules/Contacts/Services/DeletesContact.php create mode 100644 app/Classes/Modules/ControllersLogic/Contacts/DeleteContactLogic.php delete mode 100644 app/Http/Controllers/Contacts/ContactController.php create mode 100644 app/Http/Controllers/Contacts/DeleteContactController.php diff --git a/app/Classes/ControllersLogic/Contacts/DeleteContact.php b/app/Classes/ControllersLogic/Contacts/DeleteContact.php deleted file mode 100644 index 53a7c88..0000000 --- a/app/Classes/ControllersLogic/Contacts/DeleteContact.php +++ /dev/null @@ -1,24 +0,0 @@ -delete()){ - - // Return single address as a resource - return new ContactResource($contact); - - } - - } -} diff --git a/app/Classes/ControllersLogic/Contacts/ListContacts.php b/app/Classes/ControllersLogic/Contacts/ListContacts.php deleted file mode 100644 index fa7514b..0000000 --- a/app/Classes/ControllersLogic/Contacts/ListContacts.php +++ /dev/null @@ -1,19 +0,0 @@ -where('reference_id', $reference)->orderBy('created_at')->paginate(5); - - // Return collection of contacts as a resource - return ContactResource::collection($contacts); - - } -} diff --git a/app/Classes/ControllersLogic/Contacts/UpdateContact.php b/app/Classes/ControllersLogic/Contacts/UpdateContact.php deleted file mode 100644 index 3dfd949..0000000 --- a/app/Classes/ControllersLogic/Contacts/UpdateContact.php +++ /dev/null @@ -1,46 +0,0 @@ -repository = $repository; - } - - - public function execute(Request $request, int $type, string $reference) { - - $contact = $this->repository->findOrFail($id); - - $contact->type = $type; - $contact->reference_id = $reference; - $contact->name = $request->input('name'); - $contact->designation = $request->input('designation'); - $contact->contact = $request->input('contact'); - $contact->email = $request->input('email'); - - - $contact = $request->method() === 'PUT' ? Contact::findOrFail($request->input('id')) : new Contact; - - - - if($contact->save()){ - return new ContactResource($contact); - } - - } -} diff --git a/app/Classes/Modules/Contacts/DataTransferObjects/ContactObject.php b/app/Classes/Modules/Contacts/DataTransferObjects/ContactObject.php index aba325d..a5b997e 100644 --- a/app/Classes/Modules/Contacts/DataTransferObjects/ContactObject.php +++ b/app/Classes/Modules/Contacts/DataTransferObjects/ContactObject.php @@ -1,6 +1,6 @@ repository = $repository; + } + + + public function execute(String $id){ + + $contact = $this->repository->findOrFail($id); + + if($contact->delete()){ + + // Return single address as a resource + return new ContactResource($contact); + + } + } + +} \ No newline at end of file diff --git a/app/Classes/Modules/Contacts/Services/ListsContact.php b/app/Classes/Modules/Contacts/Services/ListsContact.php index 1b07d48..982ce8c 100644 --- a/app/Classes/Modules/Contacts/Services/ListsContact.php +++ b/app/Classes/Modules/Contacts/Services/ListsContact.php @@ -30,7 +30,7 @@ class ListsContact public function execute(int $type, int $referenceId){ $contacts = $this->repository->where('type', $type) - ->where('reference_id', $referenceId)->get(); + ->where('reference_id', $referenceId)->orderBy('created_at')->paginate(5); // Return collection of contacts as a resource return ContactResource::collection($contacts); diff --git a/app/Classes/Modules/ControllersLogic/Contacts/CreateContactLogic.php b/app/Classes/Modules/ControllersLogic/Contacts/CreateContactLogic.php index d9887bb..88cfdcf 100644 --- a/app/Classes/Modules/ControllersLogic/Contacts/CreateContactLogic.php +++ b/app/Classes/Modules/ControllersLogic/Contacts/CreateContactLogic.php @@ -27,7 +27,7 @@ class CreateContactLogic { try{ - $object = new ContactObject($request->input('name'), $request->input('designation'), $request->input('contact'), $request->input('email')); + $object = new ContactObject($type, $referenceId, $request->input('name'), $request->input('designation'), $request->input('contact'), $request->input('email')); return $this->createContact->execute($object, $type, $referenceId); } diff --git a/app/Classes/Modules/ControllersLogic/Contacts/DeleteContactLogic.php b/app/Classes/Modules/ControllersLogic/Contacts/DeleteContactLogic.php new file mode 100644 index 0000000..111c114 --- /dev/null +++ b/app/Classes/Modules/ControllersLogic/Contacts/DeleteContactLogic.php @@ -0,0 +1,35 @@ +DeletesContact = $DeletesContact; + } + + + public function execute(String $id){ + return $this->DeletesContact->execute($id); + } + +} + diff --git a/app/Classes/Modules/ControllersLogic/Contacts/UpdateContactLogic.php b/app/Classes/Modules/ControllersLogic/Contacts/UpdateContactLogic.php index 45b7e5e..e10a271 100644 --- a/app/Classes/Modules/ControllersLogic/Contacts/UpdateContactLogic.php +++ b/app/Classes/Modules/ControllersLogic/Contacts/UpdateContactLogic.php @@ -28,10 +28,9 @@ class UpdateContactLogic } - public function execute(int $id, Request $request) + public function execute(int $id, string $referenceId, int $type, Request $request) { - $object = new ContactObject($request->input('name'), $request->input('designation'), $request->input('contact'), $request->input('email')); - + $object = new ContactObject($type, $referenceId, $request->input('name'), $request->input('designation'), $request->input('contact'), $request->input('email')); return $this->updateContact->execute($id, $object); } } \ No newline at end of file diff --git a/app/Http/Controllers/Contacts/ContactController.php b/app/Http/Controllers/Contacts/ContactController.php deleted file mode 100644 index 1453c73..0000000 --- a/app/Http/Controllers/Contacts/ContactController.php +++ /dev/null @@ -1,53 +0,0 @@ -execute(Constants::MODULE_TYPES[$type], $reference); - - } - - /** - * Store a newly created resource in storage. - * - * @param \Illuminate\Http\Request $request - * @param string $type - * @param string $reference - * @return ContactResource - */ - public function store(Request $request, string $type, string $reference) - { - return (new UpdateContact())->execute($request, Constants::MODULE_TYPES[$type], $reference); - } - - - /** - * Remove the specified resource from storage. - * - * @param string $id - * - * @return ContactResource - */ - public function destroy(string $id): ContactResource { - - return (new DeleteContact())->execute($id); - - } -} diff --git a/app/Http/Controllers/Contacts/CreateContactController.php b/app/Http/Controllers/Contacts/CreateContactController.php index 268089c..cd98cda 100644 --- a/app/Http/Controllers/Contacts/CreateContactController.php +++ b/app/Http/Controllers/Contacts/CreateContactController.php @@ -9,6 +9,7 @@ use App\Classes\Modules\ControllerLogic\Contacts\CreateContactLogic; class CreateContactController { public function create(string $type, int $referencesId, Request $request, CreateContactLogic $logic){ + return $logic->execute(Constants::MODULE_TYPES[$type], $referencesId, $request); } diff --git a/app/Http/Controllers/Contacts/DeleteContactController.php b/app/Http/Controllers/Contacts/DeleteContactController.php new file mode 100644 index 0000000..a8c9a10 --- /dev/null +++ b/app/Http/Controllers/Contacts/DeleteContactController.php @@ -0,0 +1,18 @@ +execute($id); + } +} \ No newline at end of file diff --git a/app/Http/Controllers/Contacts/ListContactController.php b/app/Http/Controllers/Contacts/ListContactController.php index 50281df..d080a77 100644 --- a/app/Http/Controllers/Contacts/ListContactController.php +++ b/app/Http/Controllers/Contacts/ListContactController.php @@ -8,8 +8,6 @@ namespace App\Http\Controllers\Contacts; - - use App\Modules\ControllerLogic\Contacts\ListContactLogic; diff --git a/app/Http/Controllers/Contacts/UpdateContactController.php b/app/Http/Controllers/Contacts/UpdateContactController.php index 5456a76..27a8474 100644 --- a/app/Http/Controllers/Contacts/UpdateContactController.php +++ b/app/Http/Controllers/Contacts/UpdateContactController.php @@ -9,13 +9,15 @@ namespace App\Http\Controllers\Contacts; +use App\Classes\Constants; use App\Modules\ControllerLogic\Contacts\UpdateContactLogic; use Illuminate\Http\Request; class UpdateContactController { - public function update(int $id,Request $request, UpdateContactLogic $logic){ - return $logic->execute($id, $request); + public function update(string $type, string $referenceId, int $id, Request $request, UpdateContactLogic $logic){ + + return $logic->execute($id,$referenceId, Constants::MODULE_TYPES[$type], $request); } } \ No newline at end of file diff --git a/app/Http/Resources/Contact.php b/app/Http/Resources/Contact.php index e22a859..1167f6a 100644 --- a/app/Http/Resources/Contact.php +++ b/app/Http/Resources/Contact.php @@ -19,7 +19,7 @@ class Contact extends JsonResource 'id' => $this->id, 'type' => $this->type, 'reference_id' => $this->reference_id, - 'name' => $this->first_name.' '. $this->last_name, + 'name' => $this->name, 'designation' => $this->designation, 'contact' => $this->contact, 'email' => $this->email, diff --git a/routes/api.php b/routes/api.php index e96be19..f10e9a6 100644 --- a/routes/api.php +++ b/routes/api.php @@ -78,16 +78,16 @@ Route::group(['middleware' => 'api', 'prefix' => 'v1', 'as' => 'api.'], function Route::group(['prefix' => 'contact', 'as' => 'contact.', 'namespace' => 'Contacts'], function () { // List contacts - Route::get('list/{type}/{reference}', 'ContactController@index')->name('list'); + Route::get('list/{type}/{reference}', 'ListContactController@list')->name('list'); // Create new contact - Route::post('{type}/{reference}', 'ContactController@store')->name('create'); + Route::post('{type}/{reference}', 'CreateContactController@create')->name('create'); // Update contact - Route::put('{type}/{reference}/{id}', 'ContactController@store')->name('update'); + Route::put('{type}/{reference}/{id}', 'UpdateContactController@update')->name('update'); // Delete contact - Route::delete('{id}', 'ContactController@destroy')->name('delete'); + Route::delete('{id}', 'DeleteContactController@destroy')->name('delete'); });