From db4d3b0891cf0a3c504c9386da5968e31026690c Mon Sep 17 00:00:00 2001
From: Zahdi <ZahdiMohcine20@gmail.com>
Date: Mon, 31 Mar 2025 15:07:24 +0200
Subject: [PATCH] [Patch] fixed the wishlist delete bug

---
 docker/docker-compose.yml             | 2 +-
 src/Controller/WishlistController.php | 7 ++++---
 src/Entity/User.php                   | 2 +-
 src/Entity/Wishlist.php               | 4 ++--
 templates/wishlist/index.html.twig    | 6 +++++-
 5 files changed, 13 insertions(+), 8 deletions(-)

diff --git a/docker/docker-compose.yml b/docker/docker-compose.yml
index fef96537..1df2e142 100644
--- a/docker/docker-compose.yml
+++ b/docker/docker-compose.yml
@@ -6,7 +6,7 @@ services:
         container_name: db_docker_symfony
         restart: always
         volumes:
-          - ./server/mysql_data:/var/lib/mysql    #this line maps the content of ./server/mysql_data in your pc to the /var/lib/mysql of the container
+          - ./server/mysql_data:/var/lib/mysql   
         ports:
             - 3306:3306
         environment:
diff --git a/src/Controller/WishlistController.php b/src/Controller/WishlistController.php
index 84db524a..bc504620 100644
--- a/src/Controller/WishlistController.php
+++ b/src/Controller/WishlistController.php
@@ -47,6 +47,8 @@ final class WishlistController extends AbstractController
         ]);
     }
 
+
+// Method to display a wishlist items 
     #[Route('/{id}', name: 'app_wishlist_show', methods: ['GET'])]
 public function show(Wishlist $wishlist, Request $request): Response
     {
@@ -87,7 +89,6 @@ public function show(Wishlist $wishlist, Request $request): Response
     public function edit(Request $request, Wishlist $wishlist, EntityManagerInterface $entityManager): Response
     {
        
-        
         $form = $this->createForm(WishlistType::class, $wishlist);
         $form->handleRequest($request);
 
@@ -105,12 +106,12 @@ public function show(Wishlist $wishlist, Request $request): Response
     }
 
     // Method to delete a wishlist
-    #[Route('/{id}', name: 'app_wishlist_delete', methods: ['POST'])]
+    #[Route('/{id}', name: 'app_wishlist_delete', methods: ['DELETE'])]
     public function delete(Request $request, Wishlist $wishlist, EntityManagerInterface $entityManager): Response
     {
         // Validate the CSRF token before deleting the wishlist
         if ($this->isCsrfTokenValid('delete'.$wishlist->getId(), $request->getPayload()->getString('_token'))) {
-            $entityManager->delete($wishlist); // Remove the wishlist from the database
+            $entityManager->remove($wishlist); // Remove the wishlist from the database
             $entityManager->flush(); // Save changes to the database
         }
 
diff --git a/src/Entity/User.php b/src/Entity/User.php
index 8cd12256..5d5149d8 100644
--- a/src/Entity/User.php
+++ b/src/Entity/User.php
@@ -56,7 +56,7 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface
     #[ORM\Column(length: 255, nullable: true)]
     private ?string $image = null;
 
-    #[ORM\OneToMany(mappedBy: 'owner', targetEntity: Wishlist::class)]
+    #[ORM\OneToMany(mappedBy: 'owner', targetEntity: Wishlist::class , cascade: ['remove'])]
     private Collection $wishlists;
 
     // #[ORM\OneToMany(mappedBy: 'invitedUser', targetEntity: Item::class)]
diff --git a/src/Entity/Wishlist.php b/src/Entity/Wishlist.php
index 871bfe5b..b6434e9e 100644
--- a/src/Entity/Wishlist.php
+++ b/src/Entity/Wishlist.php
@@ -34,8 +34,8 @@ class Wishlist
 
     private Collection $items;
 
-    #[ORM\ManyToOne(targetEntity: User::class, inversedBy: 'wishlists', cascade: ['remove'] )]
-    #[ORM\JoinColumn(nullable: false, onDelete: 'CASCADE')]
+    #[ORM\ManyToOne(targetEntity: User::class, inversedBy: 'wishlists' )]
+    #[ORM\JoinColumn(nullable: false)]
     private ?User $owner = null;
 
     public function getOwner(): ?User
diff --git a/templates/wishlist/index.html.twig b/templates/wishlist/index.html.twig
index cc841372..2f584c23 100644
--- a/templates/wishlist/index.html.twig
+++ b/templates/wishlist/index.html.twig
@@ -59,7 +59,11 @@
         <button type="button" class="share-btn" data-wishlist-id="{{ wishlist.id }}">↗</button>
 
         <button title="Edit title"><a href="{{ path('app_wishlist_edit', { 'id': wishlist.id }) }}">✏</a></button>
-        <button title="Delete wishlist"> <a href="{{path('app_wishlist_delete', {'id':wishlist.id} ) }}">🗑</a>  </button>
+        <form action="{{ path('app_wishlist_delete', {'id': wishlist.id}) }}" method="POST" style="display: inline-block">
+            <input type="hidden" name="_method" value="DELETE"> 
+            <input type="hidden" name="_token" value="{{ csrf_token('delete' ~ wishlist.id) }}"> 
+            <button type="submit" title="Delete wishlist">🗑</button>
+        </form>
     </div>
     {% endfor %}
 </div>
-- 
GitLab