- Timestamp:
- 09/01/26 07:17:16 (4 weeks ago)
- Branches:
- main
- Children:
- 595d2e5
- Parents:
- d2eccb3
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
KernelRecordsMVC.Web/Controllers/WishlistController.cs
rd2eccb3 rd89d25b 54 54 public IActionResult Add(long productId) 55 55 { 56 var userId = GetUserId(); 57 58 if (userId == null) 59 { 60 return RedirectToAction( 61 "Login", 62 "Account"); 63 } 64 65 var product = _context.Products 66 .Include(p => p.Release) 67 .FirstOrDefault(p => p.ProductId == productId); 56 var sessionUserId = 57 HttpContext.Session.GetInt32("UserId"); 58 59 if (!sessionUserId.HasValue) 60 { 61 return RedirectToAction( 62 "Login", 63 "Account"); 64 } 65 66 var userId = 67 (long)sessionUserId.Value; 68 69 70 var product = 71 _context.Products 72 .FirstOrDefault(p => 73 p.ProductId == productId); 68 74 69 75 if (product == null) … … 71 77 72 78 73 // Find existing wishlist 74 var wishlist = _context.Wishlists 75 .Include(w => w.WishlistProducts) 76 .FirstOrDefault(w => 77 w.UserId == userId.Value); 78 79 80 // Create wishlist if user doesn't have one 79 var wishlist = 80 _context.Wishlists 81 .FirstOrDefault(w => 82 w.UserId == userId); 83 84 85 // Create wishlist if user doesn't have one yet. 81 86 if (wishlist == null) 82 87 { 83 88 wishlist = new Wishlist 84 89 { 85 WishlistId = GetNextWishlistId(), 86 UserId = userId.Value 90 WishlistId = 91 GetNextWishlistId(), 92 93 UserId = 94 userId 87 95 }; 88 96 89 _context.Wishlists.Add(wishlist); 97 98 _context.Wishlists.Add( 99 wishlist); 100 90 101 _context.SaveChanges(); 91 102 } 92 103 93 104 94 // Check if product is already there 95 var alreadyExists = wishlist.WishlistProducts 96 .Any(x => x.ProductId == productId); 105 // Prevent duplicate product. 106 var alreadyExists = 107 _context.WishlistProducts 108 .Any(wp => 109 wp.WishlistId == 110 wishlist.WishlistId && 111 wp.ProductId == 112 productId); 113 97 114 98 115 if (!alreadyExists) 99 116 { 100 var wishlistProduct = new WishlistProduct 101 { 102 WishlistId = wishlist.WishlistId, 103 ProductId = productId, 104 AddedAt = DateTime.Today 105 }; 106 107 _context.WishlistProducts.Add(wishlistProduct); 117 var wishlistProduct = 118 new WishlistProduct 119 { 120 WishlistId = 121 wishlist.WishlistId, 122 123 ProductId = 124 productId 125 }; 126 127 128 _context.WishlistProducts.Add( 129 wishlistProduct); 130 108 131 _context.SaveChanges(); 109 110 TempData["Success"] = 111 "Product added to your wishlist."; 112 } 113 else 114 { 115 TempData["Info"] = 116 "This product is already in your wishlist."; 117 } 132 } 133 134 135 TempData["Success"] = 136 "Product added to your wishlist."; 137 118 138 119 139 return RedirectToAction( 120 "Details", 121 "Release", 122 new { id = product.ReleaseId }); 140 nameof(Index)); 123 141 } 124 142
Note:
See TracChangeset
for help on using the changeset viewer.
