From 9fe6cf4cc3bb08f8899ac939f1175a96b615a5e5 Mon Sep 17 00:00:00 2001 From: Sergio Mendolia Date: Sat, 9 Sep 2023 12:51:19 +0200 Subject: [PATCH] Suggestions only on demand --- src/Controller/BookController.php | 8 +++++--- src/Service/BookSuggestions.php | 18 ++++++++++-------- templates/book/index.html.twig | 1 + 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/src/Controller/BookController.php b/src/Controller/BookController.php index 4a149817..1f025b09 100644 --- a/src/Controller/BookController.php +++ b/src/Controller/BookController.php @@ -7,6 +7,7 @@ use App\Service\BookSuggestions; use Doctrine\ORM\EntityManagerInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; +use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; @@ -14,7 +15,7 @@ class BookController extends AbstractController { #[Route('/{book}/{slug}', name: 'app_book')] - public function index(Book $book, string $slug, BookSuggestions $bookSuggestions): Response + public function index(Request $request, Book $book, string $slug, BookSuggestions $bookSuggestions): Response { if ($slug !== $book->getSlug()) { return $this->redirectToRoute('app_book', [ @@ -23,8 +24,9 @@ public function index(Book $book, string $slug, BookSuggestions $bookSuggestions ], 301); } - $suggestions = []; - if (!$book->isVerified()) { + $suggestions = BookSuggestions::EMPTY_SUGGESTIONS; + $forceSuggestions = (bool) $request->get('refresh', false); + if (!$book->isVerified() && $forceSuggestions === true) { $suggestions = $bookSuggestions->getSuggestions($book); } diff --git a/src/Service/BookSuggestions.php b/src/Service/BookSuggestions.php index 33026bae..2340bc99 100644 --- a/src/Service/BookSuggestions.php +++ b/src/Service/BookSuggestions.php @@ -12,6 +12,15 @@ class BookSuggestions { + public const EMPTY_SUGGESTIONS = [ + 'image' => [], + 'title' => [], + 'authors' => [], + 'publisher' => [], + 'tags' => [], + 'summary' => [], + ]; + public function __construct(private ParameterBagInterface $parameterBag, private SluggerInterface $slugger) { } @@ -23,14 +32,7 @@ public function getSuggestions(Book $book): array { $key = $this->parameterBag->get('GOOGLE_API_KEY'); - $suggestions = [ - 'image' => [], - 'title' => [], - 'authors' => [], - 'publisher' => [], - 'tags' => [], - 'summary' => [], - ]; + $suggestions = self::EMPTY_SUGGESTIONS; if ('' === $key || !is_string($key)) { return $suggestions; diff --git a/templates/book/index.html.twig b/templates/book/index.html.twig index 2b762794..3bc331f1 100644 --- a/templates/book/index.html.twig +++ b/templates/book/index.html.twig @@ -99,6 +99,7 @@ {% endif %} Search cover on google images + Get Suggestions from Books API {% endif %}