Jacek Kowalski
4 hours ago 6b9278a5d57091fce636dbb640b380cfb990db6c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?php
 
declare(strict_types=1);
 
namespace App\Controller;
 
use App\Lookup\BookLookupInterface;
use App\Lookup\LookupFailedException;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
 
class LookupController extends AbstractController {
    #[Route('/add/lookup', name: 'book_lookup', methods: ['GET'])]
    public function lookup(Request $request, BookLookupInterface $lookup): Response {
        $query = $request->query;
        $number = trim($query->getString('isn'));
        $title = trim($query->getString('title'));
        $author = trim($query->getString('author'));
        $publisher = trim($query->getString('publisher'));
 
        if ($number === '' && $title === '' && $author === '' && $publisher === '') {
            return $this->redirectToRoute('book_add');
        }
 
        try {
            $candidates = $number !== ''
                ? $lookup->findByStandardNumber($number)
                : $lookup->findByDescription($title, $author, $publisher);
        } catch (LookupFailedException) {
            $this->addFlash('error', 'lookup_failed');
 
            return $this->redirectToRoute('book_add');
        }
 
        return $this->render('book/lookup.html.twig', [
            'candidates' => $candidates,
            'book_id' => $query->getString('id'),
        ]);
    }
}