From c20f3bc933383ea6bae1deef7709d0ded99b4b89 Mon Sep 17 00:00:00 2001 From: Fabienmcll Date: Wed, 29 Jan 2025 15:18:01 +0100 Subject: [PATCH] - Ajout de la table Favoris. - Relation entre user et favoris en onetomany. --- src/Entity/Favoris.php | 50 ++++++++++++++++++++++++++++ src/Entity/User.php | 40 ++++++++++++++++++++++ src/Repository/FavorisRepository.php | 48 ++++++++++++++++++++++++++ templates/apiSearch/index.html.twig | 7 ++-- templates/base.html.twig | 2 ++ 5 files changed, 145 insertions(+), 2 deletions(-) create mode 100644 src/Entity/Favoris.php create mode 100644 src/Repository/FavorisRepository.php diff --git a/src/Entity/Favoris.php b/src/Entity/Favoris.php new file mode 100644 index 0000000..1df684a --- /dev/null +++ b/src/Entity/Favoris.php @@ -0,0 +1,50 @@ +id; + } + + public function getIdGoogle(): ?string + { + return $this->id_google; + } + + public function setIdGoogle(string $id_google): static + { + $this->id_google = $id_google; + + return $this; + } + + public function getUser(): ?User + { + return $this->user; + } + + public function setUser(?User $user): static + { + $this->user = $user; + + return $this; + } +} diff --git a/src/Entity/User.php b/src/Entity/User.php index 62c5fba..c296f9f 100644 --- a/src/Entity/User.php +++ b/src/Entity/User.php @@ -3,6 +3,8 @@ namespace App\Entity; use App\Repository\UserRepository; +use Doctrine\Common\Collections\ArrayCollection; +use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface; use Symfony\Component\Security\Core\User\UserInterface; @@ -33,6 +35,14 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface #[ORM\Column(type: "json")] private array $roles = []; + + #[ORM\OneToMany(mappedBy: 'user', targetEntity: Favoris::class)] + private Collection $favoris; + + public function __construct() + { + $this->favoris = new ArrayCollection(); + } public function getId(): ?int @@ -129,4 +139,34 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface return $this->email; // Ou $this->pseudo si tu préfères utiliser le pseudo } + + /** + * @return Collection + */ + public function getFavoris(): Collection + { + return $this->favoris; + } + + public function addFavori(Favoris $favori): static + { + if (!$this->favoris->contains($favori)) { + $this->favoris->add($favori); + $favori->setUser($this); + } + + return $this; + } + + public function removeFavori(Favoris $favori): static + { + if ($this->favoris->removeElement($favori)) { + // set the owning side to null (unless already changed) + if ($favori->getUser() === $this) { + $favori->setUser(null); + } + } + + return $this; + } } diff --git a/src/Repository/FavorisRepository.php b/src/Repository/FavorisRepository.php new file mode 100644 index 0000000..61b647e --- /dev/null +++ b/src/Repository/FavorisRepository.php @@ -0,0 +1,48 @@ + + * + * @method Favoris|null find($id, $lockMode = null, $lockVersion = null) + * @method Favoris|null findOneBy(array $criteria, array $orderBy = null) + * @method Favoris[] findAll() + * @method Favoris[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null) + */ +class FavorisRepository extends ServiceEntityRepository +{ + public function __construct(ManagerRegistry $registry) + { + parent::__construct($registry, Favoris::class); + } + +// /** +// * @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 aea06b1..057246e 100644 --- a/templates/apiSearch/index.html.twig +++ b/templates/apiSearch/index.html.twig @@ -4,7 +4,6 @@ {% block body %} -

Recherche de livre pour : {{ query }}

@@ -41,7 +40,11 @@
+ class="absolute top-2 right-2 w-6 h-6 cursor-pointer transition-colors duration-300" + x-data="{ liked: false }" + :fill="liked ? 'red' : 'none'" + :stroke="liked ? 'red' : 'currentColor'" + @click="liked = !liked"> diff --git a/templates/base.html.twig b/templates/base.html.twig index dda0e46..004d67c 100644 --- a/templates/base.html.twig +++ b/templates/base.html.twig @@ -23,5 +23,7 @@ {% endblock %} {% block body %}{% endblock %}
+ +