From 5ad47bb6263e46c02874d4f45b9acca7cad9a8dc Mon Sep 17 00:00:00 2001 From: KilianBrd Date: Wed, 29 Jan 2025 18:58:04 +0100 Subject: [PATCH] - Favoris qui s'affiche dans les recherches --- src/Controller/APISearchController.php | 20 ++- src/Repository/FavorisRepository.php | 61 ++++--- templates/apiSearch/index.html.twig | 211 +++++++++++++------------ 3 files changed, 157 insertions(+), 135 deletions(-) diff --git a/src/Controller/APISearchController.php b/src/Controller/APISearchController.php index 360fb83..07f0c76 100644 --- a/src/Controller/APISearchController.php +++ b/src/Controller/APISearchController.php @@ -29,41 +29,47 @@ class APISearchController extends AbstractController } #[Route('/toggleLike/{idGoogle}', name: 'like', methods: "POST")] - public function addFavoris (FavorisRepository $favorisRepository, String $idGoogle) + public function addFavoris(FavorisRepository $favorisRepository, String $idGoogle) { $favoris = new Favoris(); $user = $this->getUser(); $favoris->setUser($user); $favoris->setIdGoogle($idGoogle); - return $this->json($favorisRepository->addFavoris($favoris)); - + return $this->json(['success' => true, 'message' => 'Favoris ajouté']); } #[Route('/untoggleLike/{idGoogle}', name: 'unlike', methods: "POST")] - public function removeFavoris (FavorisRepository $favorisRepository, String $idGoogle) + public function removeFavoris(FavorisRepository $favorisRepository, String $idGoogle) { $favoris = new Favoris(); $user = $this->getUser(); $favoris->setUser($user); $favoris->setIdGoogle($idGoogle); + $favorisRepository->removeFavoris($user, $idGoogle); - return $this->json($favorisRepository->removeFavoris($user, $idGoogle)); - + return $this->json(['success' => true, 'message' => 'Favoris supprimé']); } #[Route('/api/search', name: 'api_search')] - public function index(Request $request): Response + public function index(Request $request, FavorisRepository $favorisRepository): Response { + ini_set('memory_limit', '512M'); + // Appeler la méthode search et récupérer les résultats $datas = $this->search($request); $query = $request->query->get('q'); + $user = $this->getUser(); + + $favoris = $favorisRepository->getFavorisByUser($user); + // Afficher les résultats dans le template return $this->render('apiSearch/index.html.twig', [ 'controller_name' => 'APISearchController', 'datas' => $datas, 'query' => $query, + 'favoris' => $favoris, ]); } } diff --git a/src/Repository/FavorisRepository.php b/src/Repository/FavorisRepository.php index 49a186b..8ed1f37 100644 --- a/src/Repository/FavorisRepository.php +++ b/src/Repository/FavorisRepository.php @@ -43,33 +43,42 @@ class FavorisRepository extends ServiceEntityRepository throw new \Exception($idGoogle . ' n\'est pas dans les favoris de cet utilisateur' . $userId); } - // Supprimer l'entité favoris de la base de données - $entityManager->remove($favoris); - $entityManager->flush(); + // Supprimer l'entité favoris de la base de données + $entityManager->remove($favoris); + $entityManager->flush(); } -// /** -// * @return Favoris[] Returns an array of Favoris objects -// */ -// public function findByExampleField($value): array -// { -// return $this->createQueryBuilder('f') -// ->andWhere('f.exampleField = :val') -// ->setParameter('val', $value) -// ->orderBy('f.id', 'ASC') -// ->setMaxResults(10) -// ->getQuery() -// ->getResult() -// ; -// } + public function getFavorisByUser($userId): array + { + return $this->createQueryBuilder('f') + ->andWhere('f.user = :userId') + ->setParameter('userId', $userId) + ->getQuery() + ->getResult(); + } -// public function findOneBySomeField($value): ?Favoris -// { -// return $this->createQueryBuilder('f') -// ->andWhere('f.exampleField = :val') -// ->setParameter('val', $value) -// ->getQuery() -// ->getOneOrNullResult() -// ; -// } + // /** + // * @return Favoris[] Returns an array of Favoris objects + // */ + // public function findByExampleField($value): array + // { + // return $this->createQueryBuilder('f') + // ->andWhere('f.exampleField = :val') + // ->setParameter('val', $value) + // ->orderBy('f.id', 'ASC') + // ->setMaxResults(10) + // ->getQuery() + // ->getResult() + // ; + // } + + // public function findOneBySomeField($value): ?Favoris + // { + // return $this->createQueryBuilder('f') + // ->andWhere('f.exampleField = :val') + // ->setParameter('val', $value) + // ->getQuery() + // ->getOneOrNullResult() + // ; + // } } diff --git a/templates/apiSearch/index.html.twig b/templates/apiSearch/index.html.twig index 1f9c6d2..312fb66 100644 --- a/templates/apiSearch/index.html.twig +++ b/templates/apiSearch/index.html.twig @@ -3,112 +3,119 @@ {% block title %}{% endblock %} {% block body %} - -
-
-

Résultats pour : {{ query }}

-
- {% for book in datas.items %} -
-
-
-

{{ book.volumeInfo.title }}

- {% if book.volumeInfo.authors is defined %} - {{ book.volumeInfo.authors | join(", ") }} - {% endif %} -
-
-
- {% if book.volumeInfo.imageLinks is defined and book.volumeInfo.imageLinks.smallThumbnail is defined %} - - {% endif %} -
-
- {% if book.volumeInfo.publisher is defined %} -

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

-

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

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

Catégorie : {{ categorie }}

- {% endfor %} - {% endif %} -

Nombres de pages : {{ book.volumeInfo.pageCount }}

-

Description : {{ book.searchInfo.textSnippet | raw }}

- {% endif %} -
-
-
- -
-
+
+
+

Résultats pour : + {{ query }}

+
+ {% for book in datas.items %} +
+
+
+

{{ book.volumeInfo.title }}

+ {% if book.volumeInfo.authors is defined %} + {{ book.volumeInfo.authors | join(", ") }} + {% endif %} +
+
+
+ {% if book.volumeInfo.imageLinks is defined and book.volumeInfo.imageLinks.smallThumbnail is defined %} + + {% endif %} +
+
+ {% if book.volumeInfo.publisher is defined %} +

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

+

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

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

Catégorie : + {{ categorie }} +

+ {% endfor %} + {% endif %} +

Nombres de pages : + {{ book.volumeInfo.pageCount }} +

+

Description : + {% if book.searchInfo is defined %} + {{ book.searchInfo.textSnippet | raw }} + {% endif %} +

+ {% endif %} +
+
+
+ +
+
- {% if app.user %} - - - - {% endif %} + {% if app.user %} + {% for favori in favoris %} + {% if favori.getIdGoogle() == book.id %} + + + + {% endif %} + {% if favori.getIdGoogle() != book.id %} + + + + {% endif %} -
- {% endfor %} -
+ {% endfor %} - + {% endif %} + + +
+ {% endfor %} +
+ + {% endblock %}