Skip to content

Commit

Permalink
feat: Added author to items #30
Browse files Browse the repository at this point in the history
  • Loading branch information
Chrinsieboy authored Jan 17, 2024
2 parents 8445272 + 427f463 commit 0568af0
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 26 deletions.
5 changes: 0 additions & 5 deletions docs/ERD_V-1_1.svg

This file was deleted.

12 changes: 12 additions & 0 deletions docs/ERD_V-1_2.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions src/app/Http/Controllers/AuthorController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class AuthorController extends Controller
{
//
}
1 change: 0 additions & 1 deletion src/app/Http/Controllers/IndexController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ class IndexController extends Controller
{
use OpenLibraryTrait;
public function index() {
$this->importOpenLibrary();
return Inertia::render('index');
}
}
12 changes: 12 additions & 0 deletions src/app/Models/Author.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Author extends Model
{
use HasFactory;
protected $guarded = [];
}
58 changes: 38 additions & 20 deletions src/app/Traits/Imports/OpenLibraryTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Traits\Imports;

use App\Models\Author;
use App\Models\Item;
use App\Models\User;
use App\Traits\CommonLibraryTrait;
Expand Down Expand Up @@ -34,30 +35,47 @@ private function import($offset) {
}

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', '[email protected]')->first()->id,
$authorName = !isset($book['author_name'][0]) ? $book['publisher'][0] : $book['author_name'][0];

if (!Author::where('name', $authorName)->exists()) {
$author = Author::create([
'name' => $authorName,
'modified_kind' => 'I',
'modified_user' => User::where('email', '[email protected]')->first()->id,
]);
} else {
$author = Author::where('name', $authorName)->first();
}

Item::create([
'identifier' => $this->generateValidItemIdentifier(),
'type' => 'book',
'name' => $book['title'],
'author_id' => $author->id,
'description' => "Dit boek is geschreven door {$authorName} en is uitgegeven in {$book['first_publish_year']}.",
'category' => 1,
'ISBN' => $book['isbn'][0] ?? "N/A",
'rating' => 1,
'borrowing_time' => 30,
'modified_kind' => 'I',
'modified_user' => User::where('email', '[email protected]')->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'];
$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'];
}
}
37 changes: 37 additions & 0 deletions src/database/migrations/2024_01_17_101551_create_authors_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

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

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('authors', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->char('modified_kind' ,1)->default('I');
$table->unsignedBigInteger('modified_user');
$table->foreign('modified_user')->references('id')->on('users')->onDelete("cascade");
$table->timestamps();
});

Schema::table('items', function (Blueprint $table) {
$table->unsignedBigInteger('author_id')->nullable()->after('name');
$table->foreign('author_id')->references('id')->on('authors');
$table->dropColumn('author');
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('authors');
}
};

0 comments on commit 0568af0

Please sign in to comment.