bookinventory/app/Http/Controllers/MainController.php

121 lines
3.3 KiB
PHP
Raw Normal View History

2020-12-21 07:09:34 +00:00
<?php
namespace App\Http\Controllers;
2020-12-26 10:46:23 +00:00
use App\Models\Author;
use App\Models\Book;
use App\Models\Location;
2020-12-27 00:34:07 +00:00
use App\Services\BookInformation\BookLookupService;
use App\Services\BookInformation\GoogleBooks;
use Carbon\Carbon;
2020-12-27 00:34:07 +00:00
use GuzzleHttp\Client;
2020-12-26 10:46:23 +00:00
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
2020-12-21 07:09:34 +00:00
class MainController extends Controller {
public function index() {
2020-12-26 10:46:23 +00:00
$rows = Book::paginate(15);
return view('index', [ 'rows' => $rows ]);
}
public function search(Request $request) {
2020-12-27 00:34:07 +00:00
$rows = Book::search($request->get('query'))->paginate(15);
2020-12-26 10:46:23 +00:00
return view('index', [ 'rows' => $rows ]);
}
public function add(Request $request) {
return view('add', [
2020-12-27 00:34:07 +00:00
'locations' => Location::get(),
2020-12-26 10:46:23 +00:00
'old' => array_filter($request->old('books', []), function($item) {
return !empty($item['barcode']);
})
]);
2020-12-21 07:09:34 +00:00
}
2020-12-27 00:34:07 +00:00
public function save(Request $request, BookLookupService $service) {
2020-12-26 10:46:23 +00:00
$this->validate($request, [
'location' => [ 'required' ]
]);
$input = array_filter($request->get('books'), function($item) {
return !empty($item['barcode']) && !empty($item['name']);
});
$this->getValidationFactory()->make($input, [
'*.barcode' => [ 'required' ],
'*.name' => [ 'required' ]
]);
$location = Location::firstOrCreate([
'name' => trim($request->get('location'))
]);
foreach ($input as $item) {
$book = Book::create([
'location_id' => $location->id,
'barcode' => $item['barcode'],
'name' => $item['name']
]);
$authors = [];
foreach (Arr::get($item, 'authors') as $author) {
$authors[] = Author::firstOrCreate([
'name' => $author
]);
}
$authors = array_map(function($author) { return $author->id; }, $authors);
$book->authors()->attach($authors);
2020-12-27 00:34:07 +00:00
// Lookup info from cache
$res = $service->lookup($item['barcode']);
if (!empty($res)) {
if ($thumbnail = data_get($res, 'images.thumbnail')) {
2020-12-27 00:34:07 +00:00
$file = $this->downloadFile($thumbnail);
$attachment = $book->attach($file, [
'key' => 'thumbnail'
]);
$attachment->filename = 'thumbnail.jpg';
$attachment->save();
2020-12-27 00:34:07 +00:00
}
$book->description = $res->description;
$book->published_date = Carbon::parse($res->published_date);
$book->pages = $res->pages;
$book->save();
2020-12-27 00:34:07 +00:00
}
2020-12-26 10:46:23 +00:00
}
return redirect()->route('locations.show', $location->id);
2020-12-21 07:09:34 +00:00
}
2020-12-27 00:34:07 +00:00
/**
* Download a file and return the local temp path.
*
* @param $url
* @return string|null
*/
private function downloadFile($url) {
$client = new Client();
$path = tempnam(storage_path('app/temp'), 'image');
$res = $client->get($url, [
'sink' => $path
]);
if ($res->getStatusCode() != 200) {
return null;
}
return $path;
}
2020-12-21 07:09:34 +00:00
}