From 31f64b9e5ad9476242d71c7cc03bb7063f53899f Mon Sep 17 00:00:00 2001 From: Kilian Beraud Date: Thu, 30 Jan 2025 09:19:46 +0100 Subject: [PATCH] =?UTF-8?q?-=20Ajout=20d'une=20pagination=20-=20R=C3=A9sul?= =?UTF-8?q?tats=20plu=20pertinent,=20affichage=20des=20livres=20seulement?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Controller/APISearchController.php | 4 +- src/services/GoogleService.php | 6 ++- templates/apiSearch/index.html.twig | 52 +++++++++++++++++++++++++- 3 files changed, 58 insertions(+), 4 deletions(-) diff --git a/src/Controller/APISearchController.php b/src/Controller/APISearchController.php index 4cf58e9..2567c11 100644 --- a/src/Controller/APISearchController.php +++ b/src/Controller/APISearchController.php @@ -23,9 +23,11 @@ class APISearchController extends AbstractController { // Récupérer le paramètre "q" depuis la requête $query = $request->query->get('q'); + $nb_pages = $request->query->get('page') ?? '1'; + // Appeler le service GoogleBooks avec la requête - return $this->googleBooksService->searchBooks($query); + return $this->googleBooksService->searchBooks($query, 'fr', ($nb_pages - 1) *10); } #[Route('/toggleLike/{idGoogle}', name: 'like', methods: "POST")] diff --git a/src/services/GoogleService.php b/src/services/GoogleService.php index 07b634a..761449f 100644 --- a/src/services/GoogleService.php +++ b/src/services/GoogleService.php @@ -13,20 +13,22 @@ class GoogleBooksService $this->client = $client; } - public function searchBooks(string $query, string $lang = 'fr'): array + public function searchBooks(string $query, string $lang = 'fr', string $nb_pages): array { $url = 'https://www.googleapis.com/books/v1/volumes'; $response = $this->client->request('GET', $url, [ 'query' => [ 'q' => $query, 'langRestrict' => $lang, + 'maxResults' => 10, + 'startIndex' => $nb_pages, + 'printType' => 'books', ], ]); // Convertir la réponse JSON en tableau PHP $dataArray = $response->toArray(); - return $dataArray; } } diff --git a/templates/apiSearch/index.html.twig b/templates/apiSearch/index.html.twig index 312fb66..4012959 100644 --- a/templates/apiSearch/index.html.twig +++ b/templates/apiSearch/index.html.twig @@ -3,6 +3,7 @@ {% block title %}{% endblock %} {% block body %} +

Résultats pour : @@ -28,9 +29,12 @@

Aux éditions : {{ book.volumeInfo.publisher }}

-

Date de publication : + {% if book.volumeInfo.publishedDate is defined %} +

Date de publication : {{ book.volumeInfo.publishedDate }}

+ {% endif %} + {% if book.volumeInfo.categories is defined %} {% for categorie in book.volumeInfo.categories %}

Catégorie : @@ -72,12 +76,58 @@ {% endfor %} + {% if favoris is empty %} + + + + {% endif %} {% endif %}

{% endfor %} + {% set nb_items = datas.totalItems %} +{% set nb_pages = nb_items / 10 %} +{% set current_page = app.request.get('page') ? app.request.get('page') : 1 %} + +{% if nb_pages > 1 %} +
+
    + {# Flèche "Précédent" #} + {% if current_page > 1 %} +
  • + ← Précédent +
  • + {% else %} +
  • + ← Précédent +
  • + {% endif %} + + {# Liens des pages proches de la page actuelle #} + {% for i in current_page - 3..current_page + 3 %} + {% if i > 0 and i <= nb_pages %} +
  • + {{ i }} +
  • + {% endif %} + {% endfor %} + + {# Flèche "Suivant" #} + {% if current_page < nb_pages %} +
  • + Suivant → +
  • + {% else %} +
  • + Suivant → +
  • + {% endif %} +
+
+{% endif %} +