Skip to content

Commit

Permalink
Suggestions only on demand
Browse files Browse the repository at this point in the history
  • Loading branch information
SergioMendolia committed Sep 9, 2023
1 parent 6378989 commit 9fe6cf4
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
8 changes: 5 additions & 3 deletions src/Controller/BookController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
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;

#[Route('/books')]
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', [
Expand All @@ -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);
}

Expand Down
18 changes: 10 additions & 8 deletions src/Service/BookSuggestions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
}
Expand All @@ -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;
Expand Down
1 change: 1 addition & 0 deletions templates/book/index.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
{% endif %}

<a target="_blank" href="https://www.google.com/search?q={{ book.title~" "~book.authors|join(" ")|url_encode }}&tbm=isch&source=lnms&sa=X&dpr=1">Search cover on google images</a>
<a href="{{ path('app_book', {'book':book.id, 'slug':book.slug, 'refresh':true}) }}">Get Suggestions from Books API</a>
</div>
{% endif %}
</div>
Expand Down

0 comments on commit 9fe6cf4

Please sign in to comment.