From c46b8570f12514e454efcc2bd41b930f5705ec95 Mon Sep 17 00:00:00 2001 From: hazim Date: Tue, 5 Mar 2019 15:23:11 +0800 Subject: [PATCH] create new contact recode and clean up --- .../DataTransferObjects/ContactObject.php | 98 +++++++++++++++++++ .../CannotCreateContactException.php | 17 ++++ .../Contacts/Services/CreatesContact.php | 48 +++++++++ .../Contacts/CreateContactLogic.php | 42 ++++++++ .../Contacts/CreateContactController.php | 17 ++++ routes/api.php | 4 +- 6 files changed, 225 insertions(+), 1 deletion(-) create mode 100644 app/Classes/Modules/Contacts/DataTransferObjects/ContactObject.php create mode 100644 app/Classes/Modules/Contacts/Exceptions/CannotCreateContactException.php create mode 100644 app/Classes/Modules/Contacts/Services/CreatesContact.php create mode 100644 app/Classes/Modules/ControllersLogic/Contacts/CreateContactLogic.php create mode 100644 app/Http/Controllers/Contacts/CreateContactController.php diff --git a/app/Classes/Modules/Contacts/DataTransferObjects/ContactObject.php b/app/Classes/Modules/Contacts/DataTransferObjects/ContactObject.php new file mode 100644 index 0000000..aba325d --- /dev/null +++ b/app/Classes/Modules/Contacts/DataTransferObjects/ContactObject.php @@ -0,0 +1,98 @@ +type = $type; + $this->referenceId = $referenceId; + $this->name = $name; + $this->designation = $designation; + $this->contact = $contact; + $this->email = $email; + } + + /** + * @return int + */ + public function getType(): int + { + return $this->type; + } + + /** + * @return int + */ + public function getReferenceId(): int + { + return $this->referenceId; + } + + /** + * @return string + */ + public function getName(): string + { + return $this->name; + } + + /** + * @return string + */ + public function getDesignation(): string + { + return $this->designation; + } + + /** + * @return string + */ + public function getContact(): string + { + return $this->contact; + } + + /** + * @return string + */ + public function getEmail(): string + { + return $this->email; + } + + + + + + +} \ No newline at end of file diff --git a/app/Classes/Modules/Contacts/Exceptions/CannotCreateContactException.php b/app/Classes/Modules/Contacts/Exceptions/CannotCreateContactException.php new file mode 100644 index 0000000..90278b0 --- /dev/null +++ b/app/Classes/Modules/Contacts/Exceptions/CannotCreateContactException.php @@ -0,0 +1,17 @@ +repository = $repository; + } + + + public function execute(ContactObject $object){ + + try { + + + $this->repository->type = $object->getType(); + $this->repository->reference_id = $object->getReferenceId(); + $this->repository->name = $object->getName(); + $this->repository->designation = $object->getDesignation(); + $this->repository->contact = $object->getContact(); + $this->repository->email = $object->getEmail(); + + $this->repository->save(); + + return $this->repository; + + + } catch (\Exception $exception){ + throw new CannotCreateContactException($exception->getMessage()); + } + + } + +} \ No newline at end of file diff --git a/app/Classes/Modules/ControllersLogic/Contacts/CreateContactLogic.php b/app/Classes/Modules/ControllersLogic/Contacts/CreateContactLogic.php new file mode 100644 index 0000000..165ccb5 --- /dev/null +++ b/app/Classes/Modules/ControllersLogic/Contacts/CreateContactLogic.php @@ -0,0 +1,42 @@ +createsContact = $createsContact; + } + + + public function execute(int $type, int $referenceId, Request $request){ + + try { + $object = new ContactObject($type, $referenceId, $request->input('name'), $request->input('designation'), $request->input('contact'), $request->input('email')); + + return $this->createsContact->execute($object); + } catch (CannotCreateContactException $exception){ + throw new InternalServerErrorException("Failed to create contact because {$exception->getMessage()}"); + } + + + } + +} \ No newline at end of file diff --git a/app/Http/Controllers/Contacts/CreateContactController.php b/app/Http/Controllers/Contacts/CreateContactController.php new file mode 100644 index 0000000..51b34cb --- /dev/null +++ b/app/Http/Controllers/Contacts/CreateContactController.php @@ -0,0 +1,17 @@ +execute(Constants::MODULE_TYPES[$type], $referenceId, $request); + } + +} \ No newline at end of file diff --git a/routes/api.php b/routes/api.php index b549ffc..0f269d3 100644 --- a/routes/api.php +++ b/routes/api.php @@ -70,7 +70,9 @@ Route::group(['middleware' => 'api', 'prefix' => 'v1', 'as' => 'api.'], function Route::get('list/{type}/{reference}', 'ContactController@index')->name('list'); // Create new contact - Route::post('{type}/{reference}', 'ContactController@store')->name('create'); + Route::post('{type}/{reference}', 'CreateContactController@create')->name('create'); + + Route::post('update/{type}/reference/{id}', 'CreateContactController@update')->name('update'); // Update contact Route::put('{type}/{reference}/{id}', 'ContactController@store')->name('update');