diff --git a/src/.env.example b/src/.env.example index dbae05b..8f3e2af 100644 --- a/src/.env.example +++ b/src/.env.example @@ -18,7 +18,7 @@ DB_PASSWORD= BROADCAST_DRIVER=log CACHE_DRIVER=file FILESYSTEM_DISK=local -QUEUE_CONNECTION=sync +QUEUE_CONNECTION=database SESSION_DRIVER=file SESSION_LIFETIME=120 diff --git a/src/app/Enums/Imports/ImportSources.php b/src/app/Enums/Imports/ImportSources.php new file mode 100644 index 0000000..f8a38e5 --- /dev/null +++ b/src/app/Enums/Imports/ImportSources.php @@ -0,0 +1,8 @@ +importOpenLibrary(); return Inertia::render('index'); } } diff --git a/src/app/Jobs/ImportFromApiJob.php b/src/app/Jobs/ImportFromApiJob.php new file mode 100644 index 0000000..a4efddf --- /dev/null +++ b/src/app/Jobs/ImportFromApiJob.php @@ -0,0 +1,47 @@ +query as $key => $value) { + if (is_string($key)) { + throw new Exception('Query array must not be key-value pair'); + } + } + + // replace with switch statement once more sources are added + if ($this->source == ImportSources::OPEN_LIBRARY) { + $this->importOpenLibrary($this->query); + } + } +} diff --git a/src/app/Traits/CommonLibraryTrait.php b/src/app/Traits/CommonLibraryTrait.php index e87f4ec..92cd569 100644 --- a/src/app/Traits/CommonLibraryTrait.php +++ b/src/app/Traits/CommonLibraryTrait.php @@ -2,6 +2,7 @@ namespace App\Traits; +use App\Models\Item; use App\Models\LibraryPass; use Illuminate\Support\Env; @@ -49,5 +50,26 @@ private function isLibraryPassBarCodeAlreadyInUse($barcode): bool { return LibraryPass::where('barcode', $barcode)->exists(); } + + + public function generateValidItemIdentifier(): string + { + $identifier = $this->generateItemIdentifier(); + while($this->isItemIdentifierAlreadyInUse($identifier)) { + $identifier = $this->generateItemIdentifier(); + } + return $identifier; + } + + private function generateItemIdentifier(): string + { + $prefix = "ITM"; + return $prefix . $this->generateRandomNumber(16); + } + + private function isItemIdentifierAlreadyInUse($identifier): bool + { + return Item::where('identifier', $identifier)->exists(); + } } diff --git a/src/app/Traits/Imports/OpenLibraryTrait.php b/src/app/Traits/Imports/OpenLibraryTrait.php new file mode 100644 index 0000000..8459755 --- /dev/null +++ b/src/app/Traits/Imports/OpenLibraryTrait.php @@ -0,0 +1,63 @@ +import($offset); + if ($offset >= $maxOffset) { + break; + } + $offset += 100; + }; + } + + private function import($offset) { + $books = $this->getData($offset); + + foreach ($books as $book) { + $this->importBook($book); + } + } + + private function importBook($book) { + Item::create([ + 'identifier' => $this->generateValidItemIdentifier(), + 'type' => 'book', + 'name' => $book['title'], + 'description' => "Dit boek is geschreven door {$book['author_name'][0]} en is uitgegeven in {$book['first_publish_year']}.", + 'category' => 1, + 'ISBN' => $book['isbn'][0], + 'rating' => 1, + 'borrowing_time' => 30, + 'modified_kind' => 'I', + 'modified_user' => User::where('email', 'superadmin@biblio.nl')->first()->id, + ]); + } + + private function getData($offset) + { + $baseUrl = "https://openlibrary.org/search.json?q=language%3Adut&sort=new&lang=nl&offset=" . $offset; + $curl = curl_init(); + curl_setopt($curl, CURLOPT_URL, $baseUrl); + curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); + $response = curl_exec($curl); + curl_close($curl); + + $response = json_decode($response, true); + return $response['docs']; + } +} diff --git a/src/composer.json b/src/composer.json index 00efa1d..e6412d3 100644 --- a/src/composer.json +++ b/src/composer.json @@ -11,7 +11,9 @@ "laravel/framework": "^10.10", "laravel/sanctum": "^3.3", "laravel/tinker": "^2.8", - "spatie/laravel-permission": "^6.1" + "mxl/laravel-job": "^1.5", + "spatie/laravel-permission": "^6.1", + "ext-curl": "*" }, "require-dev": { "fakerphp/faker": "^1.9.1", diff --git a/src/composer.lock b/src/composer.lock index 32b7ec2..02ee2c4 100644 --- a/src/composer.lock +++ b/src/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": "36be957dfb2dce7ea88a43f3afa7aa37", + "content-hash": "c5094f626e3457f76f8e319d44869e29", "packages": [ { "name": "brick/math", @@ -1991,6 +1991,65 @@ ], "time": "2023-10-27T15:32:31+00:00" }, + { + "name": "mxl/laravel-job", + "version": "v1.5.0", + "source": { + "type": "git", + "url": "https://github.com/mxl/laravel-job.git", + "reference": "fbead0adb6c420396473a08582cbc1959d981ae7" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/mxl/laravel-job/zipball/fbead0adb6c420396473a08582cbc1959d981ae7", + "reference": "fbead0adb6c420396473a08582cbc1959d981ae7", + "shasum": "" + }, + "require": { + "laravel/framework": "5.5.*|5.6.*|5.7.*|5.8.*|^6.0|^7.0|^8.0|^9.0|^10.0", + "php": ">=7.0" + }, + "require-dev": { + "phpunit/phpunit": "^9.2" + }, + "type": "library", + "extra": { + "laravel": { + "providers": [ + "MichaelLedin\\LaravelJob\\LaravelJobServiceProvider" + ] + } + }, + "autoload": { + "psr-4": { + "MichaelLedin\\LaravelJob\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Michael Ledin", + "email": "mledin89@gmail.com" + } + ], + "description": "Laravel job tools", + "homepage": "https://github.com/mxl/laravel-job", + "keywords": [ + "command", + "dispatch", + "job", + "laravel", + "tools" + ], + "support": { + "issues": "https://github.com/mxl/laravel-job/issues", + "source": "https://github.com/mxl/laravel-job" + }, + "time": "2023-09-27T08:19:29+00:00" + }, { "name": "nesbot/carbon", "version": "2.71.0", diff --git a/src/database/migrations/2023_12_20_101128_create_jobs_table.php b/src/database/migrations/2023_12_20_101128_create_jobs_table.php new file mode 100644 index 0000000..6098d9b --- /dev/null +++ b/src/database/migrations/2023_12_20_101128_create_jobs_table.php @@ -0,0 +1,32 @@ +bigIncrements('id'); + $table->string('queue')->index(); + $table->longText('payload'); + $table->unsignedTinyInteger('attempts'); + $table->unsignedInteger('reserved_at')->nullable(); + $table->unsignedInteger('available_at'); + $table->unsignedInteger('created_at'); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('jobs'); + } +}; diff --git a/src/database/migrations/2023_12_21_105625_add_author_to_items.php b/src/database/migrations/2023_12_21_105625_add_author_to_items.php new file mode 100644 index 0000000..3b63a54 --- /dev/null +++ b/src/database/migrations/2023_12_21_105625_add_author_to_items.php @@ -0,0 +1,29 @@ +string('author')->nullable()->after('name'); + $table->string('publisher')->nullable()->after('author'); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('items', function (Blueprint $table) { + // + }); + } +};