-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
109 additions
and
26 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
// | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = []; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
|
||
namespace App\Traits\Imports; | ||
|
||
use App\Models\Author; | ||
use App\Models\Item; | ||
use App\Models\User; | ||
use App\Traits\CommonLibraryTrait; | ||
|
@@ -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
37
src/database/migrations/2024_01_17_101551_create_authors_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
}; |