From 267b0ea8436efe7fec69ea39474e16aeb85caa89 Mon Sep 17 00:00:00 2001 From: Fery Wardiyanto Date: Thu, 12 Oct 2023 03:09:30 +0700 Subject: [PATCH 1/3] refactor(api): change base http request abstract to interface Signed-off-by: Fery Wardiyanto --- src/Contracts/FormRequest.php | 11 +++++++++++ src/Http/Requests/Address/StoreRequest.php | 5 +++-- src/Http/Requests/Address/UpdateRequest.php | 5 +++-- src/Http/Requests/Company/StoreRequest.php | 5 +++-- src/Http/Requests/Company/UpdateRequest.php | 5 +++-- src/Http/Requests/Employee/StoreRequest.php | 5 +++-- src/Http/Requests/Employee/UpdateRequest.php | 5 +++-- src/Http/Requests/FileUpload/StoreRequest.php | 5 +++-- src/Http/Requests/FileUpload/UpdateRequest.php | 5 +++-- src/Http/Requests/FormRequest.php | 13 ------------- src/Http/Requests/ProfileRequest.php | 4 +++- src/Http/Requests/SettingRequest.php | 5 ++++- src/Http/Requests/Stakeholder/StoreRequest.php | 5 +++-- src/Http/Requests/Stakeholder/UpdateRequest.php | 5 +++-- 14 files changed, 48 insertions(+), 35 deletions(-) create mode 100644 src/Contracts/FormRequest.php delete mode 100644 src/Http/Requests/FormRequest.php diff --git a/src/Contracts/FormRequest.php b/src/Contracts/FormRequest.php new file mode 100644 index 0000000..29201b5 --- /dev/null +++ b/src/Contracts/FormRequest.php @@ -0,0 +1,11 @@ + diff --git a/src/Http/Requests/Address/UpdateRequest.php b/src/Http/Requests/Address/UpdateRequest.php index b2dc0af..57622b3 100644 --- a/src/Http/Requests/Address/UpdateRequest.php +++ b/src/Http/Requests/Address/UpdateRequest.php @@ -2,10 +2,11 @@ namespace Creasi\Base\Http\Requests\Address; -use Creasi\Base\Http\Requests\FormRequest; +use Creasi\Base\Contracts\FormRequest as FormRequestContract; +use Illuminate\Foundation\Http\FormRequest; use Illuminate\Validation\Rule; -class UpdateRequest extends FormRequest +class UpdateRequest extends FormRequest implements FormRequestContract { /** * @return array diff --git a/src/Http/Requests/Company/StoreRequest.php b/src/Http/Requests/Company/StoreRequest.php index fa85e05..4762888 100644 --- a/src/Http/Requests/Company/StoreRequest.php +++ b/src/Http/Requests/Company/StoreRequest.php @@ -2,10 +2,11 @@ namespace Creasi\Base\Http\Requests\Company; -use Creasi\Base\Http\Requests\FormRequest; +use Creasi\Base\Contracts\FormRequest as FormRequestContract; +use Illuminate\Foundation\Http\FormRequest; use Illuminate\Validation\Rule; -class StoreRequest extends FormRequest +class StoreRequest extends FormRequest implements FormRequestContract { /** * @return array diff --git a/src/Http/Requests/Company/UpdateRequest.php b/src/Http/Requests/Company/UpdateRequest.php index eac8881..5147b3a 100644 --- a/src/Http/Requests/Company/UpdateRequest.php +++ b/src/Http/Requests/Company/UpdateRequest.php @@ -2,9 +2,10 @@ namespace Creasi\Base\Http\Requests\Company; -use Creasi\Base\Http\Requests\FormRequest; +use Creasi\Base\Contracts\FormRequest as FormRequestContract; +use Illuminate\Foundation\Http\FormRequest; -class UpdateRequest extends FormRequest +class UpdateRequest extends FormRequest implements FormRequestContract { /** * @return array diff --git a/src/Http/Requests/Employee/StoreRequest.php b/src/Http/Requests/Employee/StoreRequest.php index 0cb2f38..c0473a2 100644 --- a/src/Http/Requests/Employee/StoreRequest.php +++ b/src/Http/Requests/Employee/StoreRequest.php @@ -2,11 +2,12 @@ namespace Creasi\Base\Http\Requests\Employee; -use Creasi\Base\Http\Requests\FormRequest; +use Creasi\Base\Contracts\FormRequest as FormRequestContract; use Creasi\Base\Models\Enums\Gender; +use Illuminate\Foundation\Http\FormRequest; use Illuminate\Validation\Rule; -class StoreRequest extends FormRequest +class StoreRequest extends FormRequest implements FormRequestContract { /** * @return array diff --git a/src/Http/Requests/Employee/UpdateRequest.php b/src/Http/Requests/Employee/UpdateRequest.php index 0aa9d24..5048d1e 100644 --- a/src/Http/Requests/Employee/UpdateRequest.php +++ b/src/Http/Requests/Employee/UpdateRequest.php @@ -2,9 +2,10 @@ namespace Creasi\Base\Http\Requests\Employee; -use Creasi\Base\Http\Requests\FormRequest; +use Creasi\Base\Contracts\FormRequest as FormRequestContract; +use Illuminate\Foundation\Http\FormRequest; -class UpdateRequest extends FormRequest +class UpdateRequest extends FormRequest implements FormRequestContract { /** * @return array diff --git a/src/Http/Requests/FileUpload/StoreRequest.php b/src/Http/Requests/FileUpload/StoreRequest.php index b899f7f..e785f53 100644 --- a/src/Http/Requests/FileUpload/StoreRequest.php +++ b/src/Http/Requests/FileUpload/StoreRequest.php @@ -2,12 +2,13 @@ namespace Creasi\Base\Http\Requests\FileUpload; +use Creasi\Base\Contracts\FormRequest as FormRequestContract; use Creasi\Base\Contracts\HasFileUploads; -use Creasi\Base\Http\Requests\FormRequest; use Creasi\Base\Models\Enums\FileUploadType; +use Illuminate\Foundation\Http\FormRequest; use Illuminate\Validation\Rule; -class StoreRequest extends FormRequest +class StoreRequest extends FormRequest implements FormRequestContract { /** * @return array diff --git a/src/Http/Requests/FileUpload/UpdateRequest.php b/src/Http/Requests/FileUpload/UpdateRequest.php index 04a8fcf..90a154c 100644 --- a/src/Http/Requests/FileUpload/UpdateRequest.php +++ b/src/Http/Requests/FileUpload/UpdateRequest.php @@ -2,9 +2,10 @@ namespace Creasi\Base\Http\Requests\FileUpload; -use Creasi\Base\Http\Requests\FormRequest; +use Creasi\Base\Contracts\FormRequest as FormRequestContract; +use Illuminate\Foundation\Http\FormRequest; -class UpdateRequest extends FormRequest +class UpdateRequest extends FormRequest implements FormRequestContract { /** * @return array diff --git a/src/Http/Requests/FormRequest.php b/src/Http/Requests/FormRequest.php deleted file mode 100644 index 308a7fe..0000000 --- a/src/Http/Requests/FormRequest.php +++ /dev/null @@ -1,13 +0,0 @@ - diff --git a/src/Http/Requests/SettingRequest.php b/src/Http/Requests/SettingRequest.php index 164dd74..b9c3b3a 100644 --- a/src/Http/Requests/SettingRequest.php +++ b/src/Http/Requests/SettingRequest.php @@ -2,7 +2,10 @@ namespace Creasi\Base\Http\Requests; -class SettingRequest extends FormRequest +use Creasi\Base\Contracts\FormRequest as FormRequestContract; +use Illuminate\Foundation\Http\FormRequest; + +class SettingRequest extends FormRequest implements FormRequestContract { /** * @return array diff --git a/src/Http/Requests/Stakeholder/StoreRequest.php b/src/Http/Requests/Stakeholder/StoreRequest.php index 2d9a90a..b67f2d7 100644 --- a/src/Http/Requests/Stakeholder/StoreRequest.php +++ b/src/Http/Requests/Stakeholder/StoreRequest.php @@ -3,11 +3,12 @@ namespace Creasi\Base\Http\Requests\Stakeholder; use Creasi\Base\Contracts\Company; -use Creasi\Base\Http\Requests\FormRequest; +use Creasi\Base\Contracts\FormRequest as FormRequestContract; use Creasi\Base\Models\Enums\BusinessRelativeType; +use Illuminate\Foundation\Http\FormRequest; use Illuminate\Validation\Rule; -class StoreRequest extends FormRequest +class StoreRequest extends FormRequest implements FormRequestContract { /** * @return array diff --git a/src/Http/Requests/Stakeholder/UpdateRequest.php b/src/Http/Requests/Stakeholder/UpdateRequest.php index b5f3a0b..ad4839c 100644 --- a/src/Http/Requests/Stakeholder/UpdateRequest.php +++ b/src/Http/Requests/Stakeholder/UpdateRequest.php @@ -2,9 +2,10 @@ namespace Creasi\Base\Http\Requests\Stakeholder; -use Creasi\Base\Http\Requests\FormRequest; +use Creasi\Base\Contracts\FormRequest as FormRequestContract; +use Illuminate\Foundation\Http\FormRequest; -class UpdateRequest extends FormRequest +class UpdateRequest extends FormRequest implements FormRequestContract { /** * @return array From 1c8c1700f3c29e45e742bb4742e72a381f9f699c Mon Sep 17 00:00:00 2001 From: Fery Wardiyanto Date: Thu, 12 Oct 2023 04:06:36 +0700 Subject: [PATCH 2/3] feat(api): add `fulfill` method to all http request classes This way we try to ensure either ui and api controller has similar way to store the request payload to model. Say we have ```php // Api\CompanyController public function store(StoreRequest $request, Company $company) { $item = $request->fulfill($company); return CompanyResource::make($item); } ``` and in the UI controller should looks like this ```php // CompanyController public function store(StoreRequest $request, Company $company) { $item = $request->fulfill(); return back()->with(['message' => __('company.data.stored')]); } ``` But we has to make sure that the way UI works the same way as the API Signed-off-by: Fery Wardiyanto --- src/Http/Controllers/AddressController.php | 8 ++++---- src/Http/Controllers/CompanyController.php | 5 ++--- src/Http/Controllers/EmployeeController.php | 4 ++-- src/Http/Controllers/FileUploadController.php | 8 ++++---- src/Http/Controllers/StakeholderController.php | 2 +- src/Http/Requests/Address/StoreRequest.php | 2 +- src/Http/Requests/Address/UpdateRequest.php | 6 ++++++ src/Http/Requests/Company/StoreRequest.php | 6 ++++++ src/Http/Requests/Company/UpdateRequest.php | 6 ++++++ src/Http/Requests/Employee/StoreRequest.php | 6 ++++++ src/Http/Requests/Employee/UpdateRequest.php | 6 ++++++ src/Http/Requests/FileUpload/StoreRequest.php | 2 +- src/Http/Requests/FileUpload/UpdateRequest.php | 6 ++++++ src/Http/Requests/Stakeholder/UpdateRequest.php | 6 ++++++ 14 files changed, 57 insertions(+), 16 deletions(-) diff --git a/src/Http/Controllers/AddressController.php b/src/Http/Controllers/AddressController.php index ec0053b..dd9f5d3 100644 --- a/src/Http/Controllers/AddressController.php +++ b/src/Http/Controllers/AddressController.php @@ -32,7 +32,7 @@ public function index(Entity $entity) */ public function store(StoreRequest $request, Entity $entity) { - $item = $request->storeFor($entity); + $item = $request->fulfill($entity); return $this->show($item, $request)->setStatusCode(201); } @@ -52,9 +52,9 @@ public function show(Address $model, Request $request, int $address = null) */ public function update(UpdateRequest $request, Address $model, int $address = null) { - $address = $model->newQuery()->findOrFail($address); - - $address->update($request->validated()); + $request->fulfill( + $address = $model->newQuery()->findOrFail($address) + ); return $this->show($address, $request); } diff --git a/src/Http/Controllers/CompanyController.php b/src/Http/Controllers/CompanyController.php index f6d7863..bed0f4d 100644 --- a/src/Http/Controllers/CompanyController.php +++ b/src/Http/Controllers/CompanyController.php @@ -33,8 +33,7 @@ public function index(Company $company) */ public function store(StoreRequest $request, Company $company) { - /** @var Company $item */ - $item = $company->create($request->validated()); + $item = $request->fulfill($company); return $this->show($item, $request)->setStatusCode(201); } @@ -52,7 +51,7 @@ public function show(Company $company, Request $request) */ public function update(UpdateRequest $request, Company $company) { - $company->update($request->validated()); + $request->fulfill($company); return $this->show($company, $request); } diff --git a/src/Http/Controllers/EmployeeController.php b/src/Http/Controllers/EmployeeController.php index a04f1d0..cac42d1 100644 --- a/src/Http/Controllers/EmployeeController.php +++ b/src/Http/Controllers/EmployeeController.php @@ -33,7 +33,7 @@ public function index(Company $company) public function store(StoreRequest $request, Company $company) { /** @var Employee */ - $item = $company->employees()->create($request->validated()); + $item = $request->fulfill($company); return $this->show($item, $request)->setStatusCode(201); } @@ -51,7 +51,7 @@ public function show(Employee $employee, Request $request) */ public function update(UpdateRequest $request, Employee $employee) { - $employee->update($request->validated()); + $request->fulfill($employee); return $this->show($employee, $request); } diff --git a/src/Http/Controllers/FileUploadController.php b/src/Http/Controllers/FileUploadController.php index 434a6f4..a35be73 100644 --- a/src/Http/Controllers/FileUploadController.php +++ b/src/Http/Controllers/FileUploadController.php @@ -32,7 +32,7 @@ public function index(Entity $entity) */ public function store(StoreRequest $request, Entity $entity) { - $item = $request->storeFor($entity); + $item = $request->fulfill($entity); return $this->show($item, $request)->setStatusCode(201); } @@ -52,9 +52,9 @@ public function show(FileUpload $model, Request $request, string $file = null) */ public function update(UpdateRequest $request, FileUpload $model, string $file = null) { - $file = $model->newQuery()->findOrFail($file); - - $file->update($request->validated()); + $request->fulfill( + $file = $model->newQuery()->findOrFail($file) + ); return $this->show($file, $request); } diff --git a/src/Http/Controllers/StakeholderController.php b/src/Http/Controllers/StakeholderController.php index eb99d73..a8dd7fa 100644 --- a/src/Http/Controllers/StakeholderController.php +++ b/src/Http/Controllers/StakeholderController.php @@ -55,7 +55,7 @@ public function show(Stakeholder $stakeholder, Request $request) */ public function update(UpdateRequest $request, Stakeholder $stakeholder) { - $stakeholder->update($request->validated()); + $request->fulfill($stakeholder); return $this->show($stakeholder, $request); } diff --git a/src/Http/Requests/Address/StoreRequest.php b/src/Http/Requests/Address/StoreRequest.php index 83d1e8d..bcc3023 100644 --- a/src/Http/Requests/Address/StoreRequest.php +++ b/src/Http/Requests/Address/StoreRequest.php @@ -32,7 +32,7 @@ public function rules(): array ]; } - public function storeFor(HasAddresses $entity) + public function fulfill(HasAddresses $entity) { $address = Address::query()->make($this->validated()); diff --git a/src/Http/Requests/Address/UpdateRequest.php b/src/Http/Requests/Address/UpdateRequest.php index 57622b3..f282c4e 100644 --- a/src/Http/Requests/Address/UpdateRequest.php +++ b/src/Http/Requests/Address/UpdateRequest.php @@ -3,6 +3,7 @@ namespace Creasi\Base\Http\Requests\Address; use Creasi\Base\Contracts\FormRequest as FormRequestContract; +use Creasi\Base\Models\Address; use Illuminate\Foundation\Http\FormRequest; use Illuminate\Validation\Rule; @@ -28,4 +29,9 @@ public function rules(): array 'summary' => ['nullable', 'string', 'max:200'], ]; } + + public function fulfill(Address $address) + { + return $address->update($this->validated()); + } } diff --git a/src/Http/Requests/Company/StoreRequest.php b/src/Http/Requests/Company/StoreRequest.php index 4762888..d16b01e 100644 --- a/src/Http/Requests/Company/StoreRequest.php +++ b/src/Http/Requests/Company/StoreRequest.php @@ -2,6 +2,7 @@ namespace Creasi\Base\Http\Requests\Company; +use Creasi\Base\Contracts\Company; use Creasi\Base\Contracts\FormRequest as FormRequestContract; use Illuminate\Foundation\Http\FormRequest; use Illuminate\Validation\Rule; @@ -21,4 +22,9 @@ public function rules(): array 'summary' => ['nullable', 'string', 'max:200'], ]; } + + public function fulfill(Company $company) + { + return $company->create($this->validated()); + } } diff --git a/src/Http/Requests/Company/UpdateRequest.php b/src/Http/Requests/Company/UpdateRequest.php index 5147b3a..858c42a 100644 --- a/src/Http/Requests/Company/UpdateRequest.php +++ b/src/Http/Requests/Company/UpdateRequest.php @@ -2,6 +2,7 @@ namespace Creasi\Base\Http\Requests\Company; +use Creasi\Base\Contracts\Company; use Creasi\Base\Contracts\FormRequest as FormRequestContract; use Illuminate\Foundation\Http\FormRequest; @@ -20,4 +21,9 @@ public function rules(): array 'summary' => ['nullable', 'string', 'max:200'], ]; } + + public function fulfill(Company $company) + { + return $company->update($this->validated()); + } } diff --git a/src/Http/Requests/Employee/StoreRequest.php b/src/Http/Requests/Employee/StoreRequest.php index c0473a2..1f54b72 100644 --- a/src/Http/Requests/Employee/StoreRequest.php +++ b/src/Http/Requests/Employee/StoreRequest.php @@ -2,6 +2,7 @@ namespace Creasi\Base\Http\Requests\Employee; +use Creasi\Base\Contracts\Company; use Creasi\Base\Contracts\FormRequest as FormRequestContract; use Creasi\Base\Models\Enums\Gender; use Illuminate\Foundation\Http\FormRequest; @@ -23,4 +24,9 @@ public function rules(): array 'summary' => ['nullable', 'string', 'max:200'], ]; } + + public function fulfill(Company $company) + { + return $company->employees()->create($this->validated()); + } } diff --git a/src/Http/Requests/Employee/UpdateRequest.php b/src/Http/Requests/Employee/UpdateRequest.php index 5048d1e..58d9ae2 100644 --- a/src/Http/Requests/Employee/UpdateRequest.php +++ b/src/Http/Requests/Employee/UpdateRequest.php @@ -2,6 +2,7 @@ namespace Creasi\Base\Http\Requests\Employee; +use Creasi\Base\Contracts\Employee; use Creasi\Base\Contracts\FormRequest as FormRequestContract; use Illuminate\Foundation\Http\FormRequest; @@ -20,4 +21,9 @@ public function rules(): array 'summary' => ['nullable', 'string', 'max:200'], ]; } + + public function fulfill(Employee $employee) + { + return $employee->update($this->validated()); + } } diff --git a/src/Http/Requests/FileUpload/StoreRequest.php b/src/Http/Requests/FileUpload/StoreRequest.php index e785f53..947286b 100644 --- a/src/Http/Requests/FileUpload/StoreRequest.php +++ b/src/Http/Requests/FileUpload/StoreRequest.php @@ -23,7 +23,7 @@ public function rules(): array ]; } - public function storeFor(HasFileUploads $entity) + public function fulfill(HasFileUploads $entity) { /** @var FileUploadType */ $type = $this->enum('type', FileUploadType::class); diff --git a/src/Http/Requests/FileUpload/UpdateRequest.php b/src/Http/Requests/FileUpload/UpdateRequest.php index 90a154c..99c14bd 100644 --- a/src/Http/Requests/FileUpload/UpdateRequest.php +++ b/src/Http/Requests/FileUpload/UpdateRequest.php @@ -3,6 +3,7 @@ namespace Creasi\Base\Http\Requests\FileUpload; use Creasi\Base\Contracts\FormRequest as FormRequestContract; +use Creasi\Base\Models\FileUpload; use Illuminate\Foundation\Http\FormRequest; class UpdateRequest extends FormRequest implements FormRequestContract @@ -19,4 +20,9 @@ public function rules(): array 'upload' => ['nullable', 'file'], ]; } + + public function fulfill(FileUpload $file) + { + return $file->update($this->validated()); + } } diff --git a/src/Http/Requests/Stakeholder/UpdateRequest.php b/src/Http/Requests/Stakeholder/UpdateRequest.php index ad4839c..e883894 100644 --- a/src/Http/Requests/Stakeholder/UpdateRequest.php +++ b/src/Http/Requests/Stakeholder/UpdateRequest.php @@ -3,6 +3,7 @@ namespace Creasi\Base\Http\Requests\Stakeholder; use Creasi\Base\Contracts\FormRequest as FormRequestContract; +use Creasi\Base\Contracts\Stakeholder; use Illuminate\Foundation\Http\FormRequest; class UpdateRequest extends FormRequest implements FormRequestContract @@ -20,4 +21,9 @@ public function rules(): array 'summary' => ['nullable', 'string', 'max:200'], ]; } + + public function fulfill(Stakeholder $stakeholder) + { + return $stakeholder->update($this->validated()); + } } From 304ccab72521f435ebc1362dca655b0872b641cb Mon Sep 17 00:00:00 2001 From: Fery Wardiyanto Date: Thu, 12 Oct 2023 04:39:53 +0700 Subject: [PATCH 3/3] =?UTF-8?q?test:=20fix=20test=20fails=20in=20ci,=20hop?= =?UTF-8?q?efully=20=F0=9F=A4=9E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Fery Wardiyanto --- src/Repository.php | 2 +- src/ServiceProvider.php | 5 ++++- tests/ServiceProviderTest.php | 5 ++--- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/Repository.php b/src/Repository.php index 9e6fb1d..f2fa23b 100644 --- a/src/Repository.php +++ b/src/Repository.php @@ -76,7 +76,7 @@ public function resolveBusinessRelativeType(): BusinessRelativeType private function currentRoutePrefix(string $prefix = null) { - $name = \explode('.', $this->router->currentRouteName())[0]; + $name = \explode('.', $this->router->currentRouteName())[1]; return $prefix ? $name === $prefix : $name; } diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index db944cb..6e8d723 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -94,7 +94,10 @@ protected function defineRoutes(): void return; } - Route::prefix(config('creasi.base.routes_prefix', 'base')) + $prefix = config('creasi.base.routes_prefix', 'base'); + + Route::prefix($prefix) + ->name("$prefix.") ->group(self::LIB_PATH.'/routes/base.php'); } diff --git a/tests/ServiceProviderTest.php b/tests/ServiceProviderTest.php index 669fcf1..896eeb0 100644 --- a/tests/ServiceProviderTest.php +++ b/tests/ServiceProviderTest.php @@ -29,9 +29,8 @@ protected function disableRoute($app) #[Group('routes')] public function should_able_to_disable_routes() { - /** @var \Countable */ - $routes = Route::getRoutes(); + $prefix = config('creasi.base.routes_prefix'); - $this->assertCount(1, $routes); + $this->assertFalse(Route::getRoutes()->hasNamedRoute("$prefix.*")); } }