Add attachments, more pages
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Tyler
2020-12-26 19:34:07 -05:00
parent d086b29bcc
commit a1ac81a73a
29 changed files with 2349 additions and 17899 deletions

View File

@ -5,6 +5,9 @@ namespace App\Http\Controllers;
use App\Models\Author;
use App\Models\Book;
use App\Models\Location;
use App\Services\BookInformation\BookLookupService;
use App\Services\BookInformation\GoogleBooks;
use GuzzleHttp\Client;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
@ -16,24 +19,21 @@ class MainController extends Controller {
}
public function search(Request $request) {
$this->validate($request, [
'query' => [ 'required' ]
]);
$rows = Book::where('name', 'LIKE', '%' . $request->get('query') . '%')->paginate(15);
$rows = Book::search($request->get('query'))->paginate(15);
return view('index', [ 'rows' => $rows ]);
}
public function add(Request $request) {
return view('add', [
'locations' => Location::get(),
'old' => array_filter($request->old('books', []), function($item) {
return !empty($item['barcode']);
})
]);
}
public function save(Request $request) {
public function save(Request $request, BookLookupService $service) {
$this->validate($request, [
'location' => [ 'required' ]
]);
@ -69,6 +69,39 @@ class MainController extends Controller {
$authors = array_map(function($author) { return $author->id; }, $authors);
$book->authors()->attach($authors);
// Lookup info from cache
$res = $service->lookup($item['barcode']);
if (!empty($res)) {
if ($thumbnail = data_get('images.thumbnail', $res)) {
$file = $this->downloadFile($thumbnail);
$book->attach('thumbnail', $file);
}
}
}
}
/**
* 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;
}
}