From d7c4dbfa320b74c992974c8364d587693bdcbda3 Mon Sep 17 00:00:00 2001 From: David Katalinic <85836822+DaveXo9@users.noreply.github.com> Date: Wed, 13 Dec 2023 12:31:49 +0100 Subject: [PATCH] Vsliv30 2342 attachment filing purpose (#33) * Added support for adding filing purpose to attachments * Adding filingPurpose to config and service provider * Error fixes * Apply fixes from StyleCI * Added doctorine dbal and label --------- Co-authored-by: David Co-authored-by: StyleCI Bot --- composer.json | 3 +- composer.lock | 395 +++++++++++++++++- config/asseco-attachments.php | 2 + ...08_121632_create_filing_purposes_table.php | 41 ++ ...d_filing_purposes_to_attachments_table.php | 38 ++ routes/api.php | 3 + src/App/Contracts/FilingPurpose.php | 12 + .../Http/Controllers/AttachmentController.php | 7 +- .../Controllers/FilingPurposeController.php | 89 ++++ src/App/Http/Requests/AttachmentRequest.php | 1 + .../Requests/FilingPurposeIndexRequest.php | 32 ++ .../Http/Requests/FilingPurposeRequest.php | 34 ++ src/App/Models/Attachment.php | 12 +- src/App/Models/FilingPurpose.php | 21 + src/AttachmentsServiceProvider.php | 5 + 15 files changed, 689 insertions(+), 6 deletions(-) create mode 100644 migrations/2023_12_08_121632_create_filing_purposes_table.php create mode 100644 migrations/2023_12_08_155732_add_filing_purposes_to_attachments_table.php create mode 100644 src/App/Contracts/FilingPurpose.php create mode 100644 src/App/Http/Controllers/FilingPurposeController.php create mode 100644 src/App/Http/Requests/FilingPurposeIndexRequest.php create mode 100644 src/App/Http/Requests/FilingPurposeRequest.php create mode 100644 src/App/Models/FilingPurpose.php diff --git a/composer.json b/composer.json index 5aafe23..a15f482 100755 --- a/composer.json +++ b/composer.json @@ -19,7 +19,8 @@ "phpunit/phpunit": "^10.0", "mockery/mockery": "^1.4.4", "fakerphp/faker": "^1.9.1", - "orchestra/testbench": "^8.5" + "orchestra/testbench": "^8.5", + "doctrine/dbal": "3.5" }, "autoload": { "psr-4": { diff --git a/composer.lock b/composer.lock index 51eb851..02a9400 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "5732d3ca9c67fef07a646c5d00a880ad", + "content-hash": "fc14d58703c1254ba0570383bf468e14", "packages": [ { "name": "asseco-voice/laravel-blueprint-audit", @@ -4875,6 +4875,348 @@ } ], "packages-dev": [ + { + "name": "doctrine/cache", + "version": "2.2.0", + "source": { + "type": "git", + "url": "https://github.com/doctrine/cache.git", + "reference": "1ca8f21980e770095a31456042471a57bc4c68fb" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/doctrine/cache/zipball/1ca8f21980e770095a31456042471a57bc4c68fb", + "reference": "1ca8f21980e770095a31456042471a57bc4c68fb", + "shasum": "" + }, + "require": { + "php": "~7.1 || ^8.0" + }, + "conflict": { + "doctrine/common": ">2.2,<2.4" + }, + "require-dev": { + "cache/integration-tests": "dev-master", + "doctrine/coding-standard": "^9", + "phpunit/phpunit": "^7.5 || ^8.5 || ^9.5", + "psr/cache": "^1.0 || ^2.0 || ^3.0", + "symfony/cache": "^4.4 || ^5.4 || ^6", + "symfony/var-exporter": "^4.4 || ^5.4 || ^6" + }, + "type": "library", + "autoload": { + "psr-4": { + "Doctrine\\Common\\Cache\\": "lib/Doctrine/Common/Cache" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Guilherme Blanco", + "email": "guilhermeblanco@gmail.com" + }, + { + "name": "Roman Borschel", + "email": "roman@code-factory.org" + }, + { + "name": "Benjamin Eberlei", + "email": "kontakt@beberlei.de" + }, + { + "name": "Jonathan Wage", + "email": "jonwage@gmail.com" + }, + { + "name": "Johannes Schmitt", + "email": "schmittjoh@gmail.com" + } + ], + "description": "PHP Doctrine Cache library is a popular cache implementation that supports many different drivers such as redis, memcache, apc, mongodb and others.", + "homepage": "https://www.doctrine-project.org/projects/cache.html", + "keywords": [ + "abstraction", + "apcu", + "cache", + "caching", + "couchdb", + "memcached", + "php", + "redis", + "xcache" + ], + "support": { + "issues": "https://github.com/doctrine/cache/issues", + "source": "https://github.com/doctrine/cache/tree/2.2.0" + }, + "funding": [ + { + "url": "https://www.doctrine-project.org/sponsorship.html", + "type": "custom" + }, + { + "url": "https://www.patreon.com/phpdoctrine", + "type": "patreon" + }, + { + "url": "https://tidelift.com/funding/github/packagist/doctrine%2Fcache", + "type": "tidelift" + } + ], + "time": "2022-05-20T20:07:39+00:00" + }, + { + "name": "doctrine/dbal", + "version": "3.5.0", + "source": { + "type": "git", + "url": "https://github.com/doctrine/dbal.git", + "reference": "b66f55c7037402d9f522f19d86841e71c09f0195" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/doctrine/dbal/zipball/b66f55c7037402d9f522f19d86841e71c09f0195", + "reference": "b66f55c7037402d9f522f19d86841e71c09f0195", + "shasum": "" + }, + "require": { + "composer-runtime-api": "^2", + "doctrine/cache": "^1.11|^2.0", + "doctrine/deprecations": "^0.5.3|^1", + "doctrine/event-manager": "^1|^2", + "php": "^7.4 || ^8.0", + "psr/cache": "^1|^2|^3", + "psr/log": "^1|^2|^3" + }, + "require-dev": { + "doctrine/coding-standard": "10.0.0", + "jetbrains/phpstorm-stubs": "2022.2", + "phpstan/phpstan": "1.8.10", + "phpstan/phpstan-strict-rules": "^1.4", + "phpunit/phpunit": "9.5.25", + "psalm/plugin-phpunit": "0.17.0", + "squizlabs/php_codesniffer": "3.7.1", + "symfony/cache": "^5.4|^6.0", + "symfony/console": "^4.4|^5.4|^6.0", + "vimeo/psalm": "4.29.0" + }, + "suggest": { + "symfony/console": "For helpful console commands such as SQL execution and import of files." + }, + "bin": [ + "bin/doctrine-dbal" + ], + "type": "library", + "autoload": { + "psr-4": { + "Doctrine\\DBAL\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Guilherme Blanco", + "email": "guilhermeblanco@gmail.com" + }, + { + "name": "Roman Borschel", + "email": "roman@code-factory.org" + }, + { + "name": "Benjamin Eberlei", + "email": "kontakt@beberlei.de" + }, + { + "name": "Jonathan Wage", + "email": "jonwage@gmail.com" + } + ], + "description": "Powerful PHP database abstraction layer (DBAL) with many features for database schema introspection and management.", + "homepage": "https://www.doctrine-project.org/projects/dbal.html", + "keywords": [ + "abstraction", + "database", + "db2", + "dbal", + "mariadb", + "mssql", + "mysql", + "oci8", + "oracle", + "pdo", + "pgsql", + "postgresql", + "queryobject", + "sasql", + "sql", + "sqlite", + "sqlserver", + "sqlsrv" + ], + "support": { + "issues": "https://github.com/doctrine/dbal/issues", + "source": "https://github.com/doctrine/dbal/tree/3.5.0" + }, + "funding": [ + { + "url": "https://www.doctrine-project.org/sponsorship.html", + "type": "custom" + }, + { + "url": "https://www.patreon.com/phpdoctrine", + "type": "patreon" + }, + { + "url": "https://tidelift.com/funding/github/packagist/doctrine%2Fdbal", + "type": "tidelift" + } + ], + "time": "2022-10-22T14:33:42+00:00" + }, + { + "name": "doctrine/deprecations", + "version": "1.1.2", + "source": { + "type": "git", + "url": "https://github.com/doctrine/deprecations.git", + "reference": "4f2d4f2836e7ec4e7a8625e75c6aa916004db931" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/doctrine/deprecations/zipball/4f2d4f2836e7ec4e7a8625e75c6aa916004db931", + "reference": "4f2d4f2836e7ec4e7a8625e75c6aa916004db931", + "shasum": "" + }, + "require": { + "php": "^7.1 || ^8.0" + }, + "require-dev": { + "doctrine/coding-standard": "^9", + "phpstan/phpstan": "1.4.10 || 1.10.15", + "phpstan/phpstan-phpunit": "^1.0", + "phpunit/phpunit": "^7.5 || ^8.5 || ^9.5", + "psalm/plugin-phpunit": "0.18.4", + "psr/log": "^1 || ^2 || ^3", + "vimeo/psalm": "4.30.0 || 5.12.0" + }, + "suggest": { + "psr/log": "Allows logging deprecations via PSR-3 logger implementation" + }, + "type": "library", + "autoload": { + "psr-4": { + "Doctrine\\Deprecations\\": "lib/Doctrine/Deprecations" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "A small layer on top of trigger_error(E_USER_DEPRECATED) or PSR-3 logging with options to disable all deprecations or selectively for packages.", + "homepage": "https://www.doctrine-project.org/", + "support": { + "issues": "https://github.com/doctrine/deprecations/issues", + "source": "https://github.com/doctrine/deprecations/tree/1.1.2" + }, + "time": "2023-09-27T20:04:15+00:00" + }, + { + "name": "doctrine/event-manager", + "version": "2.0.0", + "source": { + "type": "git", + "url": "https://github.com/doctrine/event-manager.git", + "reference": "750671534e0241a7c50ea5b43f67e23eb5c96f32" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/doctrine/event-manager/zipball/750671534e0241a7c50ea5b43f67e23eb5c96f32", + "reference": "750671534e0241a7c50ea5b43f67e23eb5c96f32", + "shasum": "" + }, + "require": { + "php": "^8.1" + }, + "conflict": { + "doctrine/common": "<2.9" + }, + "require-dev": { + "doctrine/coding-standard": "^10", + "phpstan/phpstan": "^1.8.8", + "phpunit/phpunit": "^9.5", + "vimeo/psalm": "^4.28" + }, + "type": "library", + "autoload": { + "psr-4": { + "Doctrine\\Common\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Guilherme Blanco", + "email": "guilhermeblanco@gmail.com" + }, + { + "name": "Roman Borschel", + "email": "roman@code-factory.org" + }, + { + "name": "Benjamin Eberlei", + "email": "kontakt@beberlei.de" + }, + { + "name": "Jonathan Wage", + "email": "jonwage@gmail.com" + }, + { + "name": "Johannes Schmitt", + "email": "schmittjoh@gmail.com" + }, + { + "name": "Marco Pivetta", + "email": "ocramius@gmail.com" + } + ], + "description": "The Doctrine Event Manager is a simple PHP event system that was built to be used with the various Doctrine projects.", + "homepage": "https://www.doctrine-project.org/projects/event-manager.html", + "keywords": [ + "event", + "event dispatcher", + "event manager", + "event system", + "events" + ], + "support": { + "issues": "https://github.com/doctrine/event-manager/issues", + "source": "https://github.com/doctrine/event-manager/tree/2.0.0" + }, + "funding": [ + { + "url": "https://www.doctrine-project.org/sponsorship.html", + "type": "custom" + }, + { + "url": "https://www.patreon.com/phpdoctrine", + "type": "patreon" + }, + { + "url": "https://tidelift.com/funding/github/packagist/doctrine%2Fevent-manager", + "type": "tidelift" + } + ], + "time": "2022-10-12T20:59:15+00:00" + }, { "name": "fakerphp/faker", "version": "v1.23.0", @@ -6031,6 +6373,55 @@ }, "time": "2021-10-28T11:13:42+00:00" }, + { + "name": "psr/cache", + "version": "3.0.0", + "source": { + "type": "git", + "url": "https://github.com/php-fig/cache.git", + "reference": "aa5030cfa5405eccfdcb1083ce040c2cb8d253bf" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-fig/cache/zipball/aa5030cfa5405eccfdcb1083ce040c2cb8d253bf", + "reference": "aa5030cfa5405eccfdcb1083ce040c2cb8d253bf", + "shasum": "" + }, + "require": { + "php": ">=8.0.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "Psr\\Cache\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "PHP-FIG", + "homepage": "https://www.php-fig.org/" + } + ], + "description": "Common interface for caching libraries", + "keywords": [ + "cache", + "psr", + "psr-6" + ], + "support": { + "source": "https://github.com/php-fig/cache/tree/3.0.0" + }, + "time": "2021-02-03T23:26:27+00:00" + }, { "name": "psr/http-factory", "version": "1.0.2", @@ -7851,5 +8242,5 @@ "php": "^8.1" }, "platform-dev": [], - "plugin-api-version": "2.3.0" + "plugin-api-version": "2.6.0" } diff --git a/config/asseco-attachments.php b/config/asseco-attachments.php index 1cb65b8..b8279cf 100644 --- a/config/asseco-attachments.php +++ b/config/asseco-attachments.php @@ -1,6 +1,7 @@ [ 'attachment' => Attachment::class, + 'filingPurpose' => FilingPurpose::class, ], 'migrations' => [ diff --git a/migrations/2023_12_08_121632_create_filing_purposes_table.php b/migrations/2023_12_08_121632_create_filing_purposes_table.php new file mode 100644 index 0000000..fd7eae6 --- /dev/null +++ b/migrations/2023_12_08_121632_create_filing_purposes_table.php @@ -0,0 +1,41 @@ +uuid('id')->primary(); + } else { + $table->id(); + } + $table->string('module'); + $table->string('name'); + $table->string('label'); + MigrationMethodPicker::pick($table, config('asseco-attachments.migrations.timestamps')); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down(): void + { + Schema::dropIfExists('filing_purposes'); + } +} diff --git a/migrations/2023_12_08_155732_add_filing_purposes_to_attachments_table.php b/migrations/2023_12_08_155732_add_filing_purposes_to_attachments_table.php new file mode 100644 index 0000000..f07d443 --- /dev/null +++ b/migrations/2023_12_08_155732_add_filing_purposes_to_attachments_table.php @@ -0,0 +1,38 @@ +foreignUuid('filing_purpose_id')->nullable()->constrained(); + } else { + $table->foreignId('filing_purpose_id')->nullable()->constrained(); + } + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down(): void + { + Schema::table('attachments', function (Blueprint $table) { + $table->dropColumn('filing_purpose_id'); + }); + } +} diff --git a/routes/api.php b/routes/api.php index ece4742..08836ca 100644 --- a/routes/api.php +++ b/routes/api.php @@ -1,6 +1,7 @@ except(['update']); Route::get('attachments/{attachment}/download', [AttachmentController::class, 'download'])->name('attachments.download'); + + Route::apiResource('filing-purpose', FilingPurposeController::class); }); diff --git a/src/App/Contracts/FilingPurpose.php b/src/App/Contracts/FilingPurpose.php new file mode 100644 index 0000000..9951a6e --- /dev/null +++ b/src/App/Contracts/FilingPurpose.php @@ -0,0 +1,12 @@ +file('attachment'); + $validated = $request->validated(); + $file = Arr::get($validated, 'attachment'); + $filingPurposeId = Arr::get($validated, 'filing_purpose_id'); - $attachment = $this->attachment::createFrom($file); + $attachment = $this->attachment::createFrom($file, $filingPurposeId); return response()->json($attachment->refresh()); } diff --git a/src/App/Http/Controllers/FilingPurposeController.php b/src/App/Http/Controllers/FilingPurposeController.php new file mode 100644 index 0000000..209b04f --- /dev/null +++ b/src/App/Http/Controllers/FilingPurposeController.php @@ -0,0 +1,89 @@ +filingPurpose = $filingPurpose; + } + + /** + * Display a listing of the resource. + * + * @param FilingPurposeIndexRequest $request + * @return JsonResponse + */ + public function index(FilingPurposeIndexRequest $request): JsonResponse + { + $filingPurposes = $this->filingPurpose::where('module', $request->validated())->get(); + + return response()->json($filingPurposes); + } + + /** + * Store a newly created resource in storage. + * + * @param FilingPurposeRequest $request + * @return JsonResponse + */ + public function store(FilingPurposeRequest $request): JsonResponse + { + $filingPurpose = $this->filingPurpose::query()->create($request->validated()); + + return response()->json($filingPurpose); + } + + /** + * Display the specified resource. + * + * @param FilingPurpose $filingPurpose + * @return JsonResponse + */ + public function show(FilingPurpose $filingPurpose): JsonResponse + { + return response()->json($filingPurpose); + } + + /** + * Display the specified resource. + * + * @param FilingPurpose $filingPurpose + * @param FilingPurposeRequest $request + * @return JsonResponse + */ + public function update(FilingPurpose $filingPurpose, FilingPurposeRequest $request): JsonResponse + { + $filingPurpose->update($request->validated()); + + return response()->json($filingPurpose->refresh()); + } + + /** + * Remove the specified resource from storage. + * + * @param FilingPurpose $filingPurpose + * @return JsonResponse + * + * @throws Exception + */ + public function destroy(FilingPurpose $filingPurpose): JsonResponse + { + $isDeleted = $filingPurpose->delete(); + + return response()->json($isDeleted ? 'true' : 'false'); + } +} diff --git a/src/App/Http/Requests/AttachmentRequest.php b/src/App/Http/Requests/AttachmentRequest.php index 938cbf0..1a1d109 100644 --- a/src/App/Http/Requests/AttachmentRequest.php +++ b/src/App/Http/Requests/AttachmentRequest.php @@ -27,6 +27,7 @@ public function rules() { return [ 'attachment' => 'required|file', + 'filing_purpose_id' => 'sometimes|string|exists:filing_purposes,id', ]; } } diff --git a/src/App/Http/Requests/FilingPurposeIndexRequest.php b/src/App/Http/Requests/FilingPurposeIndexRequest.php new file mode 100644 index 0000000..c3ba53f --- /dev/null +++ b/src/App/Http/Requests/FilingPurposeIndexRequest.php @@ -0,0 +1,32 @@ + 'required|string', + ]; + } +} diff --git a/src/App/Http/Requests/FilingPurposeRequest.php b/src/App/Http/Requests/FilingPurposeRequest.php new file mode 100644 index 0000000..fd20f71 --- /dev/null +++ b/src/App/Http/Requests/FilingPurposeRequest.php @@ -0,0 +1,34 @@ + 'required|string', + 'label' => 'required|string', + 'module' => 'required|string', + ]; + } +} diff --git a/src/App/Models/Attachment.php b/src/App/Models/Attachment.php index ac73206..1c17230 100644 --- a/src/App/Models/Attachment.php +++ b/src/App/Models/Attachment.php @@ -7,6 +7,7 @@ use Asseco\Attachments\Database\Factories\AttachmentFactory; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Http\UploadedFile; @@ -26,7 +27,12 @@ public function attachables(): HasMany return $this->hasMany(Attachable::class); } - public static function createFrom(UploadedFile $file) + public function filingPurpose(): BelongsTo + { + return $this->belongsTo(FilingPurpose::class); + } + + public static function createFrom(UploadedFile $file, $filingPurposeId = null) { $fileHash = sha1_file($file->path()); @@ -40,6 +46,10 @@ public static function createFrom(UploadedFile $file) 'hash' => $fileHash, ]; + if ($filingPurposeId) { + $data['filing_purpose_id'] = $filingPurposeId; + } + return self::query()->create($data); } } diff --git a/src/App/Models/FilingPurpose.php b/src/App/Models/FilingPurpose.php new file mode 100644 index 0000000..76a7655 --- /dev/null +++ b/src/App/Models/FilingPurpose.php @@ -0,0 +1,21 @@ +hasMany(Attachment::class); + } +} diff --git a/src/AttachmentsServiceProvider.php b/src/AttachmentsServiceProvider.php index 0552297..58f9572 100644 --- a/src/AttachmentsServiceProvider.php +++ b/src/AttachmentsServiceProvider.php @@ -5,6 +5,7 @@ namespace Asseco\Attachments; use Asseco\Attachments\App\Contracts\Attachment; +use Asseco\Attachments\App\Contracts\FilingPurpose; use Illuminate\Support\Facades\Route; use Illuminate\Support\ServiceProvider; @@ -39,5 +40,9 @@ public function boot(): void $this->app->bind(Attachment::class, config('asseco-attachments.models.attachment')); Route::model('attachment', get_class(app(Attachment::class))); + + $this->app->bind(FilingPurpose::class, config('asseco-attachments.models.filingPurpose')); + + Route::model('filingPurpose', get_class(app(FilingPurpose::class))); } }