From f15204bf4ada0dc670c50e9b3404085643d8263a Mon Sep 17 00:00:00 2001 From: David Date: Mon, 15 Jan 2024 15:32:57 +0100 Subject: [PATCH 1/5] Added update() to attachment controller and added default purpose column --- ...2_add_default_to_filing_purposes_table.php | 35 +++++++++++++++++++ routes/api.php | 2 +- .../Http/Controllers/AttachmentController.php | 13 +++++++ .../Controllers/FilingPurposeController.php | 18 ++++++++-- .../Http/Requests/FilingPurposeRequest.php | 1 + 5 files changed, 66 insertions(+), 3 deletions(-) create mode 100644 migrations/2024_01_12_195732_add_default_to_filing_purposes_table.php diff --git a/migrations/2024_01_12_195732_add_default_to_filing_purposes_table.php b/migrations/2024_01_12_195732_add_default_to_filing_purposes_table.php new file mode 100644 index 0000000..9b45c53 --- /dev/null +++ b/migrations/2024_01_12_195732_add_default_to_filing_purposes_table.php @@ -0,0 +1,35 @@ +boolean('default_purpose')->default(false); + + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down(): void + { + Schema::table('filing_purposes', function (Blueprint $table) { + $table->dropColumn('default_purpose'); + }); + } +} diff --git a/routes/api.php b/routes/api.php index 08836ca..52716a0 100644 --- a/routes/api.php +++ b/routes/api.php @@ -18,7 +18,7 @@ Route::prefix(config('asseco-attachments.routes.prefix')) ->middleware(config('asseco-attachments.routes.middleware')) ->group(function () { - Route::apiResource('attachments', AttachmentController::class)->except(['update']); + Route::apiResource('attachments', AttachmentController::class); Route::get('attachments/{attachment}/download', [AttachmentController::class, 'download'])->name('attachments.download'); diff --git a/src/App/Http/Controllers/AttachmentController.php b/src/App/Http/Controllers/AttachmentController.php index f009487..d2f6392 100644 --- a/src/App/Http/Controllers/AttachmentController.php +++ b/src/App/Http/Controllers/AttachmentController.php @@ -59,6 +59,19 @@ public function show(Attachment $attachment): JsonResponse return response()->json($attachment); } + /** + * Update the specified resource. + * + * @param Attachment $attachment + * @param AttachmentRequest $request + * @return JsonResponse + */ + public function update(Attachment $attachment, AttachmentRequest $request): JsonResponse + { + $attachment->update($request->validated()); + return response()->json($attachment->refresh()); + } + /** * Remove the specified resource from storage. * diff --git a/src/App/Http/Controllers/FilingPurposeController.php b/src/App/Http/Controllers/FilingPurposeController.php index 209b04f..2abd49d 100644 --- a/src/App/Http/Controllers/FilingPurposeController.php +++ b/src/App/Http/Controllers/FilingPurposeController.php @@ -42,7 +42,14 @@ public function index(FilingPurposeIndexRequest $request): JsonResponse */ public function store(FilingPurposeRequest $request): JsonResponse { - $filingPurpose = $this->filingPurpose::query()->create($request->validated()); + $validated = $request->validated(); + + if($validated['default_purpose']){ + $this->filingPurpose::query() + ->where('default_purpose', true) + ->update(['default_purpose' => false]); + } + $filingPurpose = $this->filingPurpose::query()->create($validated); return response()->json($filingPurpose); } @@ -67,7 +74,14 @@ public function show(FilingPurpose $filingPurpose): JsonResponse */ public function update(FilingPurpose $filingPurpose, FilingPurposeRequest $request): JsonResponse { - $filingPurpose->update($request->validated()); + $validated = $request->validated(); + + if($validated['default_purpose']){ + $this->filingPurpose::query() + ->where('default_purpose', true) + ->update(['default_purpose' => false]); + } + $filingPurpose->update($validated); return response()->json($filingPurpose->refresh()); } diff --git a/src/App/Http/Requests/FilingPurposeRequest.php b/src/App/Http/Requests/FilingPurposeRequest.php index fd20f71..6c3a985 100644 --- a/src/App/Http/Requests/FilingPurposeRequest.php +++ b/src/App/Http/Requests/FilingPurposeRequest.php @@ -29,6 +29,7 @@ public function rules() 'name' => 'required|string', 'label' => 'required|string', 'module' => 'required|string', + 'default_purpose' => 'required|boolean', ]; } } From 345e9b9c86da412f17199c796d8e0eb77ff5f0f3 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Mon, 15 Jan 2024 14:35:10 +0000 Subject: [PATCH 2/5] Apply fixes from StyleCI --- config/asseco-attachments.php | 6 +++--- ..._12_195732_add_default_to_filing_purposes_table.php | 1 - src/App/Http/Controllers/AttachmentController.php | 5 +++-- src/App/Http/Controllers/FilingPurposeController.php | 4 ++-- src/App/Models/Attachment.php | 8 ++++---- src/Database/Factories/AttachmentFactory.php | 10 +++++----- 6 files changed, 17 insertions(+), 17 deletions(-) diff --git a/config/asseco-attachments.php b/config/asseco-attachments.php index b8279cf..b531360 100644 --- a/config/asseco-attachments.php +++ b/config/asseco-attachments.php @@ -19,7 +19,7 @@ /** * UUIDs as primary keys. */ - 'uuid' => false, + 'uuid' => false, /** * Timestamp types. @@ -32,11 +32,11 @@ * Should the package run the migrations. Set to false if you're publishing * and changing default migrations. */ - 'run' => true, + 'run' => true, ], 'routes' => [ - 'prefix' => 'api', + 'prefix' => 'api', 'middleware' => ['api'], ], ]; diff --git a/migrations/2024_01_12_195732_add_default_to_filing_purposes_table.php b/migrations/2024_01_12_195732_add_default_to_filing_purposes_table.php index 9b45c53..c212eb2 100644 --- a/migrations/2024_01_12_195732_add_default_to_filing_purposes_table.php +++ b/migrations/2024_01_12_195732_add_default_to_filing_purposes_table.php @@ -17,7 +17,6 @@ public function up(): void { Schema::table('filing_purposes', function (Blueprint $table) { $table->boolean('default_purpose')->default(false); - }); } diff --git a/src/App/Http/Controllers/AttachmentController.php b/src/App/Http/Controllers/AttachmentController.php index d2f6392..7b46a82 100644 --- a/src/App/Http/Controllers/AttachmentController.php +++ b/src/App/Http/Controllers/AttachmentController.php @@ -63,12 +63,13 @@ public function show(Attachment $attachment): JsonResponse * Update the specified resource. * * @param Attachment $attachment - * @param AttachmentRequest $request + * @param AttachmentRequest $request * @return JsonResponse */ - public function update(Attachment $attachment, AttachmentRequest $request): JsonResponse + public function update(Attachment $attachment, AttachmentRequest $request): JsonResponse { $attachment->update($request->validated()); + return response()->json($attachment->refresh()); } diff --git a/src/App/Http/Controllers/FilingPurposeController.php b/src/App/Http/Controllers/FilingPurposeController.php index 2abd49d..74f6e13 100644 --- a/src/App/Http/Controllers/FilingPurposeController.php +++ b/src/App/Http/Controllers/FilingPurposeController.php @@ -44,7 +44,7 @@ public function store(FilingPurposeRequest $request): JsonResponse { $validated = $request->validated(); - if($validated['default_purpose']){ + if ($validated['default_purpose']) { $this->filingPurpose::query() ->where('default_purpose', true) ->update(['default_purpose' => false]); @@ -76,7 +76,7 @@ public function update(FilingPurpose $filingPurpose, FilingPurposeRequest $reque { $validated = $request->validated(); - if($validated['default_purpose']){ + if ($validated['default_purpose']) { $this->filingPurpose::query() ->where('default_purpose', true) ->update(['default_purpose' => false]); diff --git a/src/App/Models/Attachment.php b/src/App/Models/Attachment.php index 1c17230..8387451 100644 --- a/src/App/Models/Attachment.php +++ b/src/App/Models/Attachment.php @@ -39,11 +39,11 @@ public static function createFrom(UploadedFile $file, $filingPurposeId = null) $path = $file->storeAs('attachments', date('U') . '_' . $file->getClientOriginalName()); $data = [ - 'name' => $file->getClientOriginalName(), + 'name' => $file->getClientOriginalName(), 'mime_type' => $file->getClientMimeType(), - 'size' => $file->getSize(), - 'path' => $path, - 'hash' => $fileHash, + 'size' => $file->getSize(), + 'path' => $path, + 'hash' => $fileHash, ]; if ($filingPurposeId) { diff --git a/src/Database/Factories/AttachmentFactory.php b/src/Database/Factories/AttachmentFactory.php index ec24827..711e172 100644 --- a/src/Database/Factories/AttachmentFactory.php +++ b/src/Database/Factories/AttachmentFactory.php @@ -21,11 +21,11 @@ public function modelName() public function definition() { return [ - 'name' => $this->faker->word, - 'mime_type' => $this->faker->mimeType, - 'size' => $this->faker->numberBetween(10, 10000), - 'path' => $this->faker->url, - 'hash' => $this->faker->sha1, + 'name' => $this->faker->word, + 'mime_type' => $this->faker->mimeType, + 'size' => $this->faker->numberBetween(10, 10000), + 'path' => $this->faker->url, + 'hash' => $this->faker->sha1, 'created_at' => now(), 'updated_at' => now(), ]; From 40aa65fd3ff0a266fcf6d4bc0e48575213d9a9c0 Mon Sep 17 00:00:00 2001 From: David Date: Tue, 16 Jan 2024 13:21:30 +0100 Subject: [PATCH 3/5] Dodana provjera za update metodu --- config/asseco-attachments.php | 6 +++--- ..._12_195732_add_default_to_filing_purposes_table.php | 1 + src/App/Http/Controllers/AttachmentController.php | 5 ++--- src/App/Http/Controllers/FilingPurposeController.php | 4 ++-- src/App/Models/Attachment.php | 8 ++++---- src/Database/Factories/AttachmentFactory.php | 10 +++++----- 6 files changed, 17 insertions(+), 17 deletions(-) diff --git a/config/asseco-attachments.php b/config/asseco-attachments.php index b531360..b8279cf 100644 --- a/config/asseco-attachments.php +++ b/config/asseco-attachments.php @@ -19,7 +19,7 @@ /** * UUIDs as primary keys. */ - 'uuid' => false, + 'uuid' => false, /** * Timestamp types. @@ -32,11 +32,11 @@ * Should the package run the migrations. Set to false if you're publishing * and changing default migrations. */ - 'run' => true, + 'run' => true, ], 'routes' => [ - 'prefix' => 'api', + 'prefix' => 'api', 'middleware' => ['api'], ], ]; diff --git a/migrations/2024_01_12_195732_add_default_to_filing_purposes_table.php b/migrations/2024_01_12_195732_add_default_to_filing_purposes_table.php index c212eb2..9b45c53 100644 --- a/migrations/2024_01_12_195732_add_default_to_filing_purposes_table.php +++ b/migrations/2024_01_12_195732_add_default_to_filing_purposes_table.php @@ -17,6 +17,7 @@ public function up(): void { Schema::table('filing_purposes', function (Blueprint $table) { $table->boolean('default_purpose')->default(false); + }); } diff --git a/src/App/Http/Controllers/AttachmentController.php b/src/App/Http/Controllers/AttachmentController.php index 7b46a82..d2f6392 100644 --- a/src/App/Http/Controllers/AttachmentController.php +++ b/src/App/Http/Controllers/AttachmentController.php @@ -63,13 +63,12 @@ public function show(Attachment $attachment): JsonResponse * Update the specified resource. * * @param Attachment $attachment - * @param AttachmentRequest $request + * @param AttachmentRequest $request * @return JsonResponse */ - public function update(Attachment $attachment, AttachmentRequest $request): JsonResponse + public function update(Attachment $attachment, AttachmentRequest $request): JsonResponse { $attachment->update($request->validated()); - return response()->json($attachment->refresh()); } diff --git a/src/App/Http/Controllers/FilingPurposeController.php b/src/App/Http/Controllers/FilingPurposeController.php index 74f6e13..ea72833 100644 --- a/src/App/Http/Controllers/FilingPurposeController.php +++ b/src/App/Http/Controllers/FilingPurposeController.php @@ -44,7 +44,7 @@ public function store(FilingPurposeRequest $request): JsonResponse { $validated = $request->validated(); - if ($validated['default_purpose']) { + if($validated['default_purpose']){ $this->filingPurpose::query() ->where('default_purpose', true) ->update(['default_purpose' => false]); @@ -76,7 +76,7 @@ public function update(FilingPurpose $filingPurpose, FilingPurposeRequest $reque { $validated = $request->validated(); - if ($validated['default_purpose']) { + if($validated['default_purpose'] && !$filingPurpose->default_purpose){ $this->filingPurpose::query() ->where('default_purpose', true) ->update(['default_purpose' => false]); diff --git a/src/App/Models/Attachment.php b/src/App/Models/Attachment.php index 8387451..1c17230 100644 --- a/src/App/Models/Attachment.php +++ b/src/App/Models/Attachment.php @@ -39,11 +39,11 @@ public static function createFrom(UploadedFile $file, $filingPurposeId = null) $path = $file->storeAs('attachments', date('U') . '_' . $file->getClientOriginalName()); $data = [ - 'name' => $file->getClientOriginalName(), + 'name' => $file->getClientOriginalName(), 'mime_type' => $file->getClientMimeType(), - 'size' => $file->getSize(), - 'path' => $path, - 'hash' => $fileHash, + 'size' => $file->getSize(), + 'path' => $path, + 'hash' => $fileHash, ]; if ($filingPurposeId) { diff --git a/src/Database/Factories/AttachmentFactory.php b/src/Database/Factories/AttachmentFactory.php index 711e172..ec24827 100644 --- a/src/Database/Factories/AttachmentFactory.php +++ b/src/Database/Factories/AttachmentFactory.php @@ -21,11 +21,11 @@ public function modelName() public function definition() { return [ - 'name' => $this->faker->word, - 'mime_type' => $this->faker->mimeType, - 'size' => $this->faker->numberBetween(10, 10000), - 'path' => $this->faker->url, - 'hash' => $this->faker->sha1, + 'name' => $this->faker->word, + 'mime_type' => $this->faker->mimeType, + 'size' => $this->faker->numberBetween(10, 10000), + 'path' => $this->faker->url, + 'hash' => $this->faker->sha1, 'created_at' => now(), 'updated_at' => now(), ]; From 97c38aac044f8ca34d8a05295cbb1afd0b0dc6fc Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Tue, 16 Jan 2024 12:21:44 +0000 Subject: [PATCH 4/5] Apply fixes from StyleCI --- config/asseco-attachments.php | 6 +++--- ..._12_195732_add_default_to_filing_purposes_table.php | 1 - src/App/Http/Controllers/AttachmentController.php | 5 +++-- src/App/Http/Controllers/FilingPurposeController.php | 4 ++-- src/App/Models/Attachment.php | 8 ++++---- src/Database/Factories/AttachmentFactory.php | 10 +++++----- 6 files changed, 17 insertions(+), 17 deletions(-) diff --git a/config/asseco-attachments.php b/config/asseco-attachments.php index b8279cf..b531360 100644 --- a/config/asseco-attachments.php +++ b/config/asseco-attachments.php @@ -19,7 +19,7 @@ /** * UUIDs as primary keys. */ - 'uuid' => false, + 'uuid' => false, /** * Timestamp types. @@ -32,11 +32,11 @@ * Should the package run the migrations. Set to false if you're publishing * and changing default migrations. */ - 'run' => true, + 'run' => true, ], 'routes' => [ - 'prefix' => 'api', + 'prefix' => 'api', 'middleware' => ['api'], ], ]; diff --git a/migrations/2024_01_12_195732_add_default_to_filing_purposes_table.php b/migrations/2024_01_12_195732_add_default_to_filing_purposes_table.php index 9b45c53..c212eb2 100644 --- a/migrations/2024_01_12_195732_add_default_to_filing_purposes_table.php +++ b/migrations/2024_01_12_195732_add_default_to_filing_purposes_table.php @@ -17,7 +17,6 @@ public function up(): void { Schema::table('filing_purposes', function (Blueprint $table) { $table->boolean('default_purpose')->default(false); - }); } diff --git a/src/App/Http/Controllers/AttachmentController.php b/src/App/Http/Controllers/AttachmentController.php index d2f6392..7b46a82 100644 --- a/src/App/Http/Controllers/AttachmentController.php +++ b/src/App/Http/Controllers/AttachmentController.php @@ -63,12 +63,13 @@ public function show(Attachment $attachment): JsonResponse * Update the specified resource. * * @param Attachment $attachment - * @param AttachmentRequest $request + * @param AttachmentRequest $request * @return JsonResponse */ - public function update(Attachment $attachment, AttachmentRequest $request): JsonResponse + public function update(Attachment $attachment, AttachmentRequest $request): JsonResponse { $attachment->update($request->validated()); + return response()->json($attachment->refresh()); } diff --git a/src/App/Http/Controllers/FilingPurposeController.php b/src/App/Http/Controllers/FilingPurposeController.php index ea72833..e7cdfc2 100644 --- a/src/App/Http/Controllers/FilingPurposeController.php +++ b/src/App/Http/Controllers/FilingPurposeController.php @@ -44,7 +44,7 @@ public function store(FilingPurposeRequest $request): JsonResponse { $validated = $request->validated(); - if($validated['default_purpose']){ + if ($validated['default_purpose']) { $this->filingPurpose::query() ->where('default_purpose', true) ->update(['default_purpose' => false]); @@ -76,7 +76,7 @@ public function update(FilingPurpose $filingPurpose, FilingPurposeRequest $reque { $validated = $request->validated(); - if($validated['default_purpose'] && !$filingPurpose->default_purpose){ + if ($validated['default_purpose'] && !$filingPurpose->default_purpose) { $this->filingPurpose::query() ->where('default_purpose', true) ->update(['default_purpose' => false]); diff --git a/src/App/Models/Attachment.php b/src/App/Models/Attachment.php index 1c17230..8387451 100644 --- a/src/App/Models/Attachment.php +++ b/src/App/Models/Attachment.php @@ -39,11 +39,11 @@ public static function createFrom(UploadedFile $file, $filingPurposeId = null) $path = $file->storeAs('attachments', date('U') . '_' . $file->getClientOriginalName()); $data = [ - 'name' => $file->getClientOriginalName(), + 'name' => $file->getClientOriginalName(), 'mime_type' => $file->getClientMimeType(), - 'size' => $file->getSize(), - 'path' => $path, - 'hash' => $fileHash, + 'size' => $file->getSize(), + 'path' => $path, + 'hash' => $fileHash, ]; if ($filingPurposeId) { diff --git a/src/Database/Factories/AttachmentFactory.php b/src/Database/Factories/AttachmentFactory.php index ec24827..711e172 100644 --- a/src/Database/Factories/AttachmentFactory.php +++ b/src/Database/Factories/AttachmentFactory.php @@ -21,11 +21,11 @@ public function modelName() public function definition() { return [ - 'name' => $this->faker->word, - 'mime_type' => $this->faker->mimeType, - 'size' => $this->faker->numberBetween(10, 10000), - 'path' => $this->faker->url, - 'hash' => $this->faker->sha1, + 'name' => $this->faker->word, + 'mime_type' => $this->faker->mimeType, + 'size' => $this->faker->numberBetween(10, 10000), + 'path' => $this->faker->url, + 'hash' => $this->faker->sha1, 'created_at' => now(), 'updated_at' => now(), ]; From 03b0ffde9a50dad057dab0043f3c6a2001e31873 Mon Sep 17 00:00:00 2001 From: David Date: Tue, 16 Jan 2024 14:39:32 +0100 Subject: [PATCH 5/5] Changed default_purpose to default --- ...2_195732_add_default_to_filing_purposes_table.php | 4 ++-- src/App/Http/Controllers/FilingPurposeController.php | 12 ++++++------ src/App/Http/Requests/FilingPurposeRequest.php | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/migrations/2024_01_12_195732_add_default_to_filing_purposes_table.php b/migrations/2024_01_12_195732_add_default_to_filing_purposes_table.php index c212eb2..48c568d 100644 --- a/migrations/2024_01_12_195732_add_default_to_filing_purposes_table.php +++ b/migrations/2024_01_12_195732_add_default_to_filing_purposes_table.php @@ -16,7 +16,7 @@ class AddDefaultToFilingPurposesTable extends Migration public function up(): void { Schema::table('filing_purposes', function (Blueprint $table) { - $table->boolean('default_purpose')->default(false); + $table->boolean('default')->default(false); }); } @@ -28,7 +28,7 @@ public function up(): void public function down(): void { Schema::table('filing_purposes', function (Blueprint $table) { - $table->dropColumn('default_purpose'); + $table->dropColumn('default'); }); } } diff --git a/src/App/Http/Controllers/FilingPurposeController.php b/src/App/Http/Controllers/FilingPurposeController.php index e7cdfc2..9c058a3 100644 --- a/src/App/Http/Controllers/FilingPurposeController.php +++ b/src/App/Http/Controllers/FilingPurposeController.php @@ -44,10 +44,10 @@ public function store(FilingPurposeRequest $request): JsonResponse { $validated = $request->validated(); - if ($validated['default_purpose']) { + if ($validated['default']) { $this->filingPurpose::query() - ->where('default_purpose', true) - ->update(['default_purpose' => false]); + ->where('default', true) + ->update(['default' => false]); } $filingPurpose = $this->filingPurpose::query()->create($validated); @@ -76,10 +76,10 @@ public function update(FilingPurpose $filingPurpose, FilingPurposeRequest $reque { $validated = $request->validated(); - if ($validated['default_purpose'] && !$filingPurpose->default_purpose) { + if ($validated['default'] && !$filingPurpose->default_purpose) { $this->filingPurpose::query() - ->where('default_purpose', true) - ->update(['default_purpose' => false]); + ->where('default', true) + ->update(['default' => false]); } $filingPurpose->update($validated); diff --git a/src/App/Http/Requests/FilingPurposeRequest.php b/src/App/Http/Requests/FilingPurposeRequest.php index 6c3a985..f562ba3 100644 --- a/src/App/Http/Requests/FilingPurposeRequest.php +++ b/src/App/Http/Requests/FilingPurposeRequest.php @@ -29,7 +29,7 @@ public function rules() 'name' => 'required|string', 'label' => 'required|string', 'module' => 'required|string', - 'default_purpose' => 'required|boolean', + 'default' => 'required|boolean', ]; } }