Skip to content

Commit

Permalink
Vsliv30 2342 default filing purpose option (#35)
Browse files Browse the repository at this point in the history
* Added update() to attachment controller and added default purpose column

* Apply fixes from StyleCI

* Dodana provjera za update metodu

* Apply fixes from StyleCI

* Changed default_purpose to default

---------

Co-authored-by: David <david.katalinic@asseco-see.hr>
Co-authored-by: StyleCI Bot <bot@styleci.io>
  • Loading branch information
3 people authored Jan 19, 2024
1 parent 77a2bef commit 0853147
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 15 deletions.
6 changes: 3 additions & 3 deletions config/asseco-attachments.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
/**
* UUIDs as primary keys.
*/
'uuid' => false,
'uuid' => false,

/**
* Timestamp types.
Expand All @@ -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'],
],
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class AddDefaultToFilingPurposesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up(): void
{
Schema::table('filing_purposes', function (Blueprint $table) {
$table->boolean('default')->default(false);
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down(): void
{
Schema::table('filing_purposes', function (Blueprint $table) {
$table->dropColumn('default');
});
}
}
2 changes: 1 addition & 1 deletion routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down
14 changes: 14 additions & 0 deletions src/App/Http/Controllers/AttachmentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
18 changes: 16 additions & 2 deletions src/App/Http/Controllers/FilingPurposeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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());
}
Expand Down
1 change: 1 addition & 0 deletions src/App/Http/Requests/FilingPurposeRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public function rules()
'name' => 'required|string',
'label' => 'required|string',
'module' => 'required|string',
'default' => 'required|boolean',
];
}
}
8 changes: 4 additions & 4 deletions src/App/Models/Attachment.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
10 changes: 5 additions & 5 deletions src/Database/Factories/AttachmentFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
];
Expand Down

0 comments on commit 0853147

Please sign in to comment.