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;
|
2020-12-27 09:29:52 +00:00
|
|
|
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)) {
|
2020-12-27 09:37:21 +00:00
|
|
|
if ($images = data_get($res, 'images')) {
|
|
|
|
foreach ($images as $key => $image) {
|
2020-12-27 09:45:36 +00:00
|
|
|
if (empty($image)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-12-27 09:37:21 +00:00
|
|
|
$file = $this->downloadFile($image);
|
2020-12-27 09:29:52 +00:00
|
|
|
|
2020-12-27 09:37:21 +00:00
|
|
|
$attachment = $book->attach($file, [
|
2020-12-27 09:45:36 +00:00
|
|
|
'key' => $key,
|
|
|
|
'group' => 'images'
|
2020-12-27 09:37:21 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
$attachment->filename = $key . '.jpg';
|
|
|
|
$attachment->save();
|
|
|
|
}
|
2020-12-27 00:34:07 +00:00
|
|
|
}
|
2020-12-27 09:29:52 +00:00
|
|
|
|
2020-12-27 09:37:21 +00:00
|
|
|
$book->description = data_get($res, 'description');
|
|
|
|
|
|
|
|
if ($date = data_get($res, 'published_date')) {
|
|
|
|
$book->published_date = Carbon::parse($date);
|
|
|
|
}
|
|
|
|
|
|
|
|
$book->pages = data_get($res, 'pages', 0);
|
|
|
|
|
2020-12-27 09:29:52 +00:00
|
|
|
$book->save();
|
2020-12-27 00:34:07 +00:00
|
|
|
}
|
2020-12-26 10:46:23 +00:00
|
|
|
}
|
2020-12-27 09:29:52 +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
|
|
|
}
|