From 46fe8430efd69ec5c2e0a670edd316b0a76298cb Mon Sep 17 00:00:00 2001 From: hazim Date: Thu, 7 Mar 2019 15:31:16 +0800 Subject: [PATCH] update contact recode and clean up --- .../Contacts/UpdateContact.php | 23 +++- .../DataTransferObjects/ContactObject.php | 128 ++++++++++++++++++ .../CannotCreateContactException.php | 21 +++ .../Contacts/Services/CreatesContact.php | 43 ++++++ .../Contacts/Services/ListsContact.php | 40 ++++++ .../Contacts/Services/UpdateContact.php | 44 ++++++ .../Contacts/CreateContactLogic.php | 38 ++++++ .../Contacts/ListContactLogic.php | 42 ++++++ .../Contacts/UpdateContactLogic.php | 37 +++++ .../Contacts/CreateContactController.php | 15 ++ .../Contacts/ListContactController.php | 22 +++ .../Contacts/UpdateContactController.php | 21 +++ app/Http/Resources/Contact.php | 6 +- .../views/pages/Orders/profile.blade.php | 81 +++++++++++ routes/api.php | 13 +- routes/web.php | 4 + 16 files changed, 572 insertions(+), 6 deletions(-) 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/Contacts/Services/ListsContact.php create mode 100644 app/Classes/Modules/Contacts/Services/UpdateContact.php create mode 100644 app/Classes/Modules/ControllersLogic/Contacts/CreateContactLogic.php create mode 100644 app/Classes/Modules/ControllersLogic/Contacts/ListContactLogic.php create mode 100644 app/Classes/Modules/ControllersLogic/Contacts/UpdateContactLogic.php create mode 100644 app/Http/Controllers/Contacts/CreateContactController.php create mode 100644 app/Http/Controllers/Contacts/ListContactController.php create mode 100644 app/Http/Controllers/Contacts/UpdateContactController.php create mode 100644 resources/views/pages/Orders/profile.blade.php diff --git a/app/Classes/ControllersLogic/Contacts/UpdateContact.php b/app/Classes/ControllersLogic/Contacts/UpdateContact.php index efc2266..3dfd949 100644 --- a/app/Classes/ControllersLogic/Contacts/UpdateContact.php +++ b/app/Classes/ControllersLogic/Contacts/UpdateContact.php @@ -8,9 +8,23 @@ use Illuminate\Http\Request; class UpdateContact { + + /** @var Contact */ + private $repository; + + /** + * UpdateContact constructor. + * @param Contact $repository + */ + public function __construct(Contact $repository) + { + $this->repository = $repository; + } + + public function execute(Request $request, int $type, string $reference) { - - $contact = $request->method() === 'PUT' ? Contact::findOrFail($request->input('id')) : new Contact; + + $contact = $this->repository->findOrFail($id); $contact->type = $type; $contact->reference_id = $reference; @@ -19,6 +33,11 @@ class UpdateContact $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 new file mode 100644 index 0000000..0ff45ae --- /dev/null +++ b/app/Classes/Modules/Contacts/DataTransferObjects/ContactObject.php @@ -0,0 +1,128 @@ +name = $name; + $this->designation = $designation; + $this->email = $email; + $this->contact = $contact; + } + + /** + * @return string + */ + public function getName(): string + { + return $this->name; + } + + /** + * @return string + */ + public function getDesignation(): string + { + return $this->designation; + } + + /** + * @return string + */ + public function getEmail(): string + { + return $this->email; + } + + /** + * @return string + */ + public function getContact(): string + { + return $this->contact; + } + + /** + * @return int|null + */ + public function getType(): ?int + { + return $this->type; + } + + /** + * @return int|null + */ + public function getReferenceId(): ?int + { + return $this->referenceId; + } + + /** + * @return int|null + */ + public function getId() + { + return $this->id; + } + + /** + * @param int|null $type + */ + public function setType($type) + { + $this->type = $type; + } + + /** + * @param int|null $referenceId + */ + public function setReferenceId($referenceId) + { + $this->referenceId = $referenceId; + } + + /** + * @param int|null $id + */ + public function setId($id) + { + $this->id = $id; + } + + + + +} \ 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..1fbe8cf --- /dev/null +++ b/app/Classes/Modules/Contacts/Exceptions/CannotCreateContactException.php @@ -0,0 +1,21 @@ +repository = $repository; + } + + /** + * @param ContactObject $contact + * @param int $type + * @param int $referenceId + * @return Contact + */ + public function execute(ContactObject $contact, int $type, int $referenceId) + { + $this->repository->type = $type; + $this->repository->reference_id = $referenceId; + $this->repository->name = $contact->getName(); + $this->repository->designation = $contact->getDesignation(); + $this->repository->email = $contact->getEmail(); + $this->repository->contact = $contact->getContact(); + + $this->repository->save(); + + return $this->repository; + } + +} \ No newline at end of file diff --git a/app/Classes/Modules/Contacts/Services/ListsContact.php b/app/Classes/Modules/Contacts/Services/ListsContact.php new file mode 100644 index 0000000..1b07d48 --- /dev/null +++ b/app/Classes/Modules/Contacts/Services/ListsContact.php @@ -0,0 +1,40 @@ +repository = $repository; + } + + public function execute(int $type, int $referenceId){ + + $contacts = $this->repository->where('type', $type) + ->where('reference_id', $referenceId)->get(); + + // Return collection of contacts as a resource + return ContactResource::collection($contacts); + } + + +} \ No newline at end of file diff --git a/app/Classes/Modules/Contacts/Services/UpdateContact.php b/app/Classes/Modules/Contacts/Services/UpdateContact.php new file mode 100644 index 0000000..97ba8d2 --- /dev/null +++ b/app/Classes/Modules/Contacts/Services/UpdateContact.php @@ -0,0 +1,44 @@ +repository = $repository; + } + + + /** + * @param int $id + * @param ContactObject $object + * @return Contact + */ + public function execute(int $id, ContactObject $object) + { + /** @var Contact $contact */ + $contact = $this->repository->findOrFail($id); + $contact ->name = $object->getName(); + $contact ->designation = $object->getDesignation(); + $contact ->contact = $object->getContact(); + $contact ->email = $object->getEmail(); + + $contact->save(); + + return $contact; + + } + +} \ 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..24c2c5f --- /dev/null +++ b/app/Classes/Modules/ControllersLogic/Contacts/CreateContactLogic.php @@ -0,0 +1,38 @@ +createContact = $createContact; + } + + + public function execute(string $type, int $referenceId, Request $request) + { + try{ + + $object = new ContactObject($request->input('name'), $request->input('designation'), $request->input('contact'), $request->input('email')); + return $this->createContact->execute($object, $type, $referenceId); + + } + catch (){ + + } + } + +} \ No newline at end of file diff --git a/app/Classes/Modules/ControllersLogic/Contacts/ListContactLogic.php b/app/Classes/Modules/ControllersLogic/Contacts/ListContactLogic.php new file mode 100644 index 0000000..3d08c8d --- /dev/null +++ b/app/Classes/Modules/ControllersLogic/Contacts/ListContactLogic.php @@ -0,0 +1,42 @@ +listsContacts = $listsContacts; + } + + + /** + * @param $type + * @param $referenceId + * @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection + */ + public function execute($type, $referenceId){ + + return $this->listsContacts->execute(Constants::MODULE_TYPES[$type], $referenceId); + + } + + +} \ No newline at end of file diff --git a/app/Classes/Modules/ControllersLogic/Contacts/UpdateContactLogic.php b/app/Classes/Modules/ControllersLogic/Contacts/UpdateContactLogic.php new file mode 100644 index 0000000..45b7e5e --- /dev/null +++ b/app/Classes/Modules/ControllersLogic/Contacts/UpdateContactLogic.php @@ -0,0 +1,37 @@ +updateContact = $updateContact; + } + + + public function execute(int $id, Request $request) + { + $object = new ContactObject($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/CreateContactController.php b/app/Http/Controllers/Contacts/CreateContactController.php new file mode 100644 index 0000000..afa06f5 --- /dev/null +++ b/app/Http/Controllers/Contacts/CreateContactController.php @@ -0,0 +1,15 @@ +execute(Constants::MODULE_TYPES[$type], $referencesId, $request); + } + +} \ No newline at end of file diff --git a/app/Http/Controllers/Contacts/ListContactController.php b/app/Http/Controllers/Contacts/ListContactController.php new file mode 100644 index 0000000..50281df --- /dev/null +++ b/app/Http/Controllers/Contacts/ListContactController.php @@ -0,0 +1,22 @@ +execute($type,$referenceId); + } +} \ No newline at end of file diff --git a/app/Http/Controllers/Contacts/UpdateContactController.php b/app/Http/Controllers/Contacts/UpdateContactController.php new file mode 100644 index 0000000..5456a76 --- /dev/null +++ b/app/Http/Controllers/Contacts/UpdateContactController.php @@ -0,0 +1,21 @@ +execute($id, $request); + } + +} \ No newline at end of file diff --git a/app/Http/Resources/Contact.php b/app/Http/Resources/Contact.php index 08ee0df..e22a859 100644 --- a/app/Http/Resources/Contact.php +++ b/app/Http/Resources/Contact.php @@ -14,14 +14,16 @@ class Contact extends JsonResource */ public function toArray($request) { + $comapny = \App\Models\Company::findOrFail($this->reference_id); return [ 'id' => $this->id, 'type' => $this->type, 'reference_id' => $this->reference_id, - 'name' => $this->name, + 'name' => $this->first_name.' '. $this->last_name, 'designation' => $this->designation, 'contact' => $this->contact, - 'email' => $this->email + 'email' => $this->email, + 'company' => Company::make($comapny) ]; } } diff --git a/resources/views/pages/Orders/profile.blade.php b/resources/views/pages/Orders/profile.blade.php new file mode 100644 index 0000000..0673c23 --- /dev/null +++ b/resources/views/pages/Orders/profile.blade.php @@ -0,0 +1,81 @@ +@extends('layouts.base_portal') + +@section('inner_content') +
+
+
+
+
+
+
+
+ Order #216718963296916 +
+
+
+
+ Placed on 05 Mar 2019 16:40:08 +
+
+
+
+
+
+ Status: + Complete +
+
+
+
+ Order has been delivered +
+
+
+
+
+
+
+ 05-03-2019 17:05 +
+ Your Order has successfully been delivered +
+ 05-03-2019 17:05 +
+ Your Order is out for delivery +
+ 05-03-2019 17:05 +
+ Your Order has been unpacked and being prepared for delivery +
+ 05-03-2019 17:05 +
+ Your Order has arrived at Malaysia's sorting centre +
+ 05-03-2019 17:05 +
+ Your Order has been shipped successfully +
+ 05-03-2019 17:05 +
+ Your Order is packed and is ready to ship +
+ 05-03-2019 17:05 +
+ Your Order has arrived at China's sorting centre +
+ 05-03-2019 17:04 +
+ Order has been confirmed +
+ 05-03-2019 17:02 +
+ Order created +
+
+
+
+
+
+
+
+@endsection \ No newline at end of file diff --git a/routes/api.php b/routes/api.php index b549ffc..8b4f91c 100644 --- a/routes/api.php +++ b/routes/api.php @@ -67,10 +67,10 @@ 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}', 'ContactController@index')->name('list'); // Create new contact - Route::post('{type}/{reference}', 'ContactController@store')->name('create'); + //Route::post('{type}/{reference}', 'ContactController@store')->name('create'); // Update contact Route::put('{type}/{reference}/{id}', 'ContactController@store')->name('update'); @@ -78,6 +78,15 @@ Route::group(['middleware' => 'api', 'prefix' => 'v1', 'as' => 'api.'], function // Delete contact Route::delete('{id}', 'ContactController@destroy')->name('delete'); + // Test Create Contact + Route::post('/create/{type}/{reference}','CreateContactController@create')->name('create.contact'); + + // Test Update Contact + Route::post('/update/{id}','UpdateContactController@update')->name('update.contact'); + + // Test List Contact + Route::get('/list/{type}/{reference}','ListContactController@list')->name('list.contact'); + }); // Warehouses Routes diff --git a/routes/web.php b/routes/web.php index 21df1e3..ffcba5a 100644 --- a/routes/web.php +++ b/routes/web.php @@ -40,6 +40,10 @@ Route::group(['middleware' => ['secure', 'clearcache']], function (): void { Route::get('/warehouse/{warehouseId}/profile', function($warehouseId){ return view('pages.warehouses.profile', ['warehouseId' => $warehouseId]); })->name('warehouses.profile'); + + Route::get('/order/profile', function(){ + return view('pages.orders.profile'); + })->name('order.profile'); });