From 26950905d66afee583985fff8dfd78a8c6313da3 Mon Sep 17 00:00:00 2001 From: Kilian Beraud Date: Thu, 30 Jan 2025 14:58:25 +0100 Subject: [PATCH 1/2] - Modification des Favoris bdd + code --- src/Controller/APISearchController.php | 32 +++++-- src/Entity/Favoris.php | 120 +++++++++++++++++++++++++ templates/apiSearch/index.html.twig | 118 +++++++++++++----------- 3 files changed, 211 insertions(+), 59 deletions(-) diff --git a/src/Controller/APISearchController.php b/src/Controller/APISearchController.php index 2567c11..9a0a959 100644 --- a/src/Controller/APISearchController.php +++ b/src/Controller/APISearchController.php @@ -9,6 +9,7 @@ use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; use App\Repository\FavorisRepository; use App\Entity\Favoris; +use Symfony\Component\HttpFoundation\JsonResponse; class APISearchController extends AbstractController { @@ -31,27 +32,46 @@ class APISearchController extends AbstractController } #[Route('/toggleLike/{idGoogle}', name: 'like', methods: "POST")] - public function addFavoris(FavorisRepository $favorisRepository, String $idGoogle) + public function addFavoris(Request $request, FavorisRepository $favorisRepository, String $idGoogle) { + $content = $request->getContent(); + $data = json_decode($content, true); + + if (json_last_error() !== JSON_ERROR_NONE || empty($data)) { + return new JsonResponse(['status' => 'error', 'message' => 'Invalid data'], 400); + } + + $title = $data['title'] ?? ''; + $authors = $data['authors'] ?? ''; + $images = $data['images'] ?? ''; + $description = $data['description'] ?? ''; + $date = $data['date'] ?? ''; + $pages = $data['pages'] ?? ''; + $favoris = new Favoris(); $user = $this->getUser(); $favoris->setUser($user); $favoris->setIdGoogle($idGoogle); + $favoris->setTitle($title); + $favoris->setAuthors($authors); + $favoris->setImage($images); + $favoris->setDescription($description); + $favoris->setPublication($date); + $favoris->setPages($pages); + $favoris->setPublication($date); + $favorisRepository->addFavoris($favoris); - return $this->json(['success' => true, 'message' => 'Favoris ajouté']); + return $this->json(['success' => true, 'message' => 'Favoris ajouté', "status" => "added"]); } #[Route('/untoggleLike/{idGoogle}', name: 'unlike', methods: "POST")] 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(['success' => true, 'message' => 'Favoris supprimé']); + return $this->json(['success' => true, 'message' => 'Favoris supprimé', "status" => "removed"]); } #[Route('/api/search', name: 'api_search')] diff --git a/src/Entity/Favoris.php b/src/Entity/Favoris.php index 1df684a..184e7dc 100644 --- a/src/Entity/Favoris.php +++ b/src/Entity/Favoris.php @@ -19,6 +19,30 @@ class Favoris #[ORM\ManyToOne(inversedBy: 'favoris')] private ?User $user = null; + #[ORM\Column(length: 255)] + private ?string $title = null; + + #[ORM\Column(length: 255, nullable: true)] + private ?string $authors = null; + + #[ORM\Column(length: 255, nullable: true)] + private ?string $edition = null; + + #[ORM\Column(length: 255, nullable: true)] + private ?string $publication = null; + + #[ORM\Column(length: 255, nullable: true)] + private ?string $categorie = null; + + #[ORM\Column(length: 255, nullable: true)] + private ?string $pages = null; + + #[ORM\Column(length: 2555, nullable: true)] + private ?string $description = null; + + #[ORM\Column(length: 255, nullable: true)] + private ?string $image = null; + public function getId(): ?int { return $this->id; @@ -47,4 +71,100 @@ class Favoris return $this; } + + public function getTitle(): ?string + { + return $this->title; + } + + public function setTitle(string $title): static + { + $this->title = $title; + + return $this; + } + + public function getAuthors(): ?string + { + return $this->authors; + } + + public function setAuthors(?string $authors): static + { + $this->authors = $authors; + + return $this; + } + + public function getEdition(): ?string + { + return $this->edition; + } + + public function setEdition(?string $edition): static + { + $this->edition = $edition; + + return $this; + } + + public function getPublication(): ?string + { + return $this->publication; + } + + public function setPublication(?string $publication): static + { + $this->publication = $publication; + + return $this; + } + + public function getCategorie(): ?string + { + return $this->categorie; + } + + public function setCategorie(?string $categorie): static + { + $this->categorie = $categorie; + + return $this; + } + + public function getPages(): ?string + { + return $this->pages; + } + + public function setPages(?string $pages): static + { + $this->pages = $pages; + + return $this; + } + + public function getDescription(): ?string + { + return $this->description; + } + + public function setDescription(string $description): static + { + $this->description = $description; + + return $this; + } + + public function getImage(): ?string + { + return $this->image; + } + + public function setImage(?string $image): static + { + $this->image = $image; + + return $this; + } } diff --git a/templates/apiSearch/index.html.twig b/templates/apiSearch/index.html.twig index 722d5b9..9a71816 100644 --- a/templates/apiSearch/index.html.twig +++ b/templates/apiSearch/index.html.twig @@ -69,19 +69,27 @@ {% if app.user %} {% set isLiked = book.id in favoris|map(f => f.getIdGoogle()) %} - - - + + + + + + {% endif %} @@ -181,47 +189,51 @@