| 1 | using KernelRecordsMVC.Infrastructure.Data;
|
|---|
| 2 | using KernelRecordsMVC.Models;
|
|---|
| 3 | using Microsoft.AspNetCore.Mvc;
|
|---|
| 4 | using Microsoft.EntityFrameworkCore;
|
|---|
| 5 |
|
|---|
| 6 | namespace KernelRecordsMVC.Web.Controllers;
|
|---|
| 7 |
|
|---|
| 8 | public class WishlistController : Controller
|
|---|
| 9 | {
|
|---|
| 10 | private readonly KernelRecordsContext _context;
|
|---|
| 11 |
|
|---|
| 12 | public WishlistController(KernelRecordsContext context)
|
|---|
| 13 | {
|
|---|
| 14 | _context = context;
|
|---|
| 15 | }
|
|---|
| 16 |
|
|---|
| 17 | // ==========================================
|
|---|
| 18 | // WISHLIST
|
|---|
| 19 | // ==========================================
|
|---|
| 20 |
|
|---|
| 21 | [HttpGet]
|
|---|
| 22 | public IActionResult Index()
|
|---|
| 23 | {
|
|---|
| 24 | var userId = GetUserId();
|
|---|
| 25 |
|
|---|
| 26 | if (userId == null)
|
|---|
| 27 | {
|
|---|
| 28 | return RedirectToAction(
|
|---|
| 29 | "Login",
|
|---|
| 30 | "Account");
|
|---|
| 31 | }
|
|---|
| 32 |
|
|---|
| 33 | var wishlist = _context.Wishlists
|
|---|
| 34 | .Include(w => w.WishlistProducts)
|
|---|
| 35 | .ThenInclude(wp => wp.Product)
|
|---|
| 36 | .ThenInclude(p => p.Release)
|
|---|
| 37 | .FirstOrDefault(w => w.UserId == userId.Value);
|
|---|
| 38 |
|
|---|
| 39 | if (wishlist == null)
|
|---|
| 40 | {
|
|---|
| 41 | return View(new Wishlist());
|
|---|
| 42 | }
|
|---|
| 43 |
|
|---|
| 44 | return View(wishlist);
|
|---|
| 45 | }
|
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 | // ==========================================
|
|---|
| 49 | // ADD TO WISHLIST
|
|---|
| 50 | // ==========================================
|
|---|
| 51 |
|
|---|
| 52 | [HttpPost]
|
|---|
| 53 | [ValidateAntiForgeryToken]
|
|---|
| 54 | public IActionResult Add(long productId)
|
|---|
| 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);
|
|---|
| 68 |
|
|---|
| 69 | if (product == null)
|
|---|
| 70 | return NotFound();
|
|---|
| 71 |
|
|---|
| 72 |
|
|---|
| 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
|
|---|
| 81 | if (wishlist == null)
|
|---|
| 82 | {
|
|---|
| 83 | wishlist = new Wishlist
|
|---|
| 84 | {
|
|---|
| 85 | WishlistId = GetNextWishlistId(),
|
|---|
| 86 | UserId = userId.Value
|
|---|
| 87 | };
|
|---|
| 88 |
|
|---|
| 89 | _context.Wishlists.Add(wishlist);
|
|---|
| 90 | _context.SaveChanges();
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 |
|
|---|
| 94 | // Check if product is already there
|
|---|
| 95 | var alreadyExists = wishlist.WishlistProducts
|
|---|
| 96 | .Any(x => x.ProductId == productId);
|
|---|
| 97 |
|
|---|
| 98 | if (!alreadyExists)
|
|---|
| 99 | {
|
|---|
| 100 | var wishlistProduct = new WishlistProduct
|
|---|
| 101 | {
|
|---|
| 102 | WishlistId = wishlist.WishlistId,
|
|---|
| 103 | ProductId = productId,
|
|---|
| 104 | AddedAt = DateTime.Today
|
|---|
| 105 | };
|
|---|
| 106 |
|
|---|
| 107 | _context.WishlistProducts.Add(wishlistProduct);
|
|---|
| 108 | _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 | }
|
|---|
| 118 |
|
|---|
| 119 | return RedirectToAction(
|
|---|
| 120 | "Details",
|
|---|
| 121 | "Release",
|
|---|
| 122 | new { id = product.ReleaseId });
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 |
|
|---|
| 126 | // ==========================================
|
|---|
| 127 | // REMOVE FROM WISHLIST
|
|---|
| 128 | // ==========================================
|
|---|
| 129 |
|
|---|
| 130 | [HttpPost]
|
|---|
| 131 | [ValidateAntiForgeryToken]
|
|---|
| 132 | public IActionResult Remove(long productId)
|
|---|
| 133 | {
|
|---|
| 134 | var userId = GetUserId();
|
|---|
| 135 |
|
|---|
| 136 | if (userId == null)
|
|---|
| 137 | {
|
|---|
| 138 | return RedirectToAction(
|
|---|
| 139 | "Login",
|
|---|
| 140 | "Account");
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | var item = _context.WishlistProducts
|
|---|
| 144 | .Include(x => x.Wishlist)
|
|---|
| 145 | .FirstOrDefault(x =>
|
|---|
| 146 | x.ProductId == productId &&
|
|---|
| 147 | x.Wishlist.UserId == userId.Value);
|
|---|
| 148 |
|
|---|
| 149 | if (item != null)
|
|---|
| 150 | {
|
|---|
| 151 | _context.WishlistProducts.Remove(item);
|
|---|
| 152 | _context.SaveChanges();
|
|---|
| 153 |
|
|---|
| 154 | TempData["Success"] =
|
|---|
| 155 | "Product removed from your wishlist.";
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| 158 | return RedirectToAction(nameof(Index));
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 |
|
|---|
| 162 | // ==========================================
|
|---|
| 163 | // MOVE WISHLIST PRODUCT TO CART
|
|---|
| 164 | // ==========================================
|
|---|
| 165 |
|
|---|
| 166 | [HttpPost]
|
|---|
| 167 | [ValidateAntiForgeryToken]
|
|---|
| 168 | public IActionResult AddToCart(long productId)
|
|---|
| 169 | {
|
|---|
| 170 | var userId = GetUserId();
|
|---|
| 171 |
|
|---|
| 172 | if (userId == null)
|
|---|
| 173 | {
|
|---|
| 174 | return RedirectToAction(
|
|---|
| 175 | "Login",
|
|---|
| 176 | "Account");
|
|---|
| 177 | }
|
|---|
| 178 |
|
|---|
| 179 | var product = _context.Products
|
|---|
| 180 | .Include(p => p.Release)
|
|---|
| 181 | .FirstOrDefault(p => p.ProductId == productId);
|
|---|
| 182 |
|
|---|
| 183 | if (product == null)
|
|---|
| 184 | return NotFound();
|
|---|
| 185 |
|
|---|
| 186 | if (product.Stock <= 0)
|
|---|
| 187 | {
|
|---|
| 188 | TempData["Error"] =
|
|---|
| 189 | "This product is currently out of stock.";
|
|---|
| 190 |
|
|---|
| 191 | return RedirectToAction(nameof(Index));
|
|---|
| 192 | }
|
|---|
| 193 |
|
|---|
| 194 | return RedirectToAction(
|
|---|
| 195 | "AddProduct",
|
|---|
| 196 | "Order",
|
|---|
| 197 | new { id = productId });
|
|---|
| 198 | }
|
|---|
| 199 |
|
|---|
| 200 |
|
|---|
| 201 | // ==========================================
|
|---|
| 202 | // USER ID
|
|---|
| 203 | // ==========================================
|
|---|
| 204 |
|
|---|
| 205 | private long? GetUserId()
|
|---|
| 206 | {
|
|---|
| 207 | var userId = HttpContext.Session.GetInt32("UserId");
|
|---|
| 208 |
|
|---|
| 209 | if (userId.HasValue)
|
|---|
| 210 | return userId.Value;
|
|---|
| 211 |
|
|---|
| 212 | return null;
|
|---|
| 213 | }
|
|---|
| 214 |
|
|---|
| 215 |
|
|---|
| 216 | // ==========================================
|
|---|
| 217 | // WISHLIST ID
|
|---|
| 218 | // ==========================================
|
|---|
| 219 |
|
|---|
| 220 | private long GetNextWishlistId()
|
|---|
| 221 | {
|
|---|
| 222 | var maxId = _context.Wishlists
|
|---|
| 223 | .Select(x => (long?)x.WishlistId)
|
|---|
| 224 | .Max();
|
|---|
| 225 |
|
|---|
| 226 | return (maxId ?? 0) + 1;
|
|---|
| 227 | }
|
|---|
| 228 | } |
|---|