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 new file mode 100644 index 0000000..48c568d --- /dev/null +++ b/migrations/2024_01_12_195732_add_default_to_filing_purposes_table.php @@ -0,0 +1,34 @@ +boolean('default')->default(false); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down(): void + { + Schema::table('filing_purposes', function (Blueprint $table) { + $table->dropColumn('default'); + }); + } +} 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..7b46a82 100644 --- a/src/App/Http/Controllers/AttachmentController.php +++ b/src/App/Http/Controllers/AttachmentController.php @@ -59,6 +59,20 @@ 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..9c058a3 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']) { + $this->filingPurpose::query() + ->where('default', true) + ->update(['default' => 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'] && !$filingPurpose->default_purpose) { + $this->filingPurpose::query() + ->where('default', true) + ->update(['default' => 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..f562ba3 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' => 'required|boolean', ]; } } 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(), ];