| 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 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);
|
|---|
| 74 |
|
|---|
| 75 | if (product == null)
|
|---|
| 76 | return NotFound();
|
|---|
| 77 |
|
|---|
| 78 |
|
|---|
| 79 | using var transaction =
|
|---|
| 80 | _context.Database.BeginTransaction();
|
|---|
| 81 |
|
|---|
| 82 | try
|
|---|
| 83 | {
|
|---|
| 84 | var wishlist =
|
|---|
| 85 | _context.Wishlists
|
|---|
| 86 | .FirstOrDefault(w =>
|
|---|
| 87 | w.UserId == userId);
|
|---|
| 88 |
|
|---|
| 89 |
|
|---|
| 90 | if (wishlist == null)
|
|---|
| 91 | {
|
|---|
| 92 | wishlist = new Wishlist
|
|---|
| 93 | {
|
|---|
| 94 | WishlistId =
|
|---|
| 95 | GetNextWishlistId(),
|
|---|
| 96 |
|
|---|
| 97 | UserId =
|
|---|
| 98 | userId
|
|---|
| 99 | };
|
|---|
| 100 |
|
|---|
| 101 |
|
|---|
| 102 | _context.Wishlists.Add(
|
|---|
| 103 | wishlist);
|
|---|
| 104 |
|
|---|
| 105 | _context.SaveChanges();
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 |
|
|---|
| 109 | var alreadyExists =
|
|---|
| 110 | _context.WishlistProducts
|
|---|
| 111 | .Any(wp =>
|
|---|
| 112 | wp.WishlistId ==
|
|---|
| 113 | wishlist.WishlistId &&
|
|---|
| 114 | wp.ProductId ==
|
|---|
| 115 | productId);
|
|---|
| 116 |
|
|---|
| 117 |
|
|---|
| 118 | if (!alreadyExists)
|
|---|
| 119 | {
|
|---|
| 120 | var wishlistProduct =
|
|---|
| 121 | new WishlistProduct
|
|---|
| 122 | {
|
|---|
| 123 | WishlistId =
|
|---|
| 124 | wishlist.WishlistId,
|
|---|
| 125 |
|
|---|
| 126 | ProductId =
|
|---|
| 127 | productId
|
|---|
| 128 | };
|
|---|
| 129 |
|
|---|
| 130 |
|
|---|
| 131 | _context.WishlistProducts.Add(
|
|---|
| 132 | wishlistProduct);
|
|---|
| 133 |
|
|---|
| 134 | _context.SaveChanges();
|
|---|
| 135 | }
|
|---|
| 136 |
|
|---|
| 137 |
|
|---|
| 138 | transaction.Commit();
|
|---|
| 139 |
|
|---|
| 140 |
|
|---|
| 141 | TempData["Success"] =
|
|---|
| 142 | "Product added to your wishlist.";
|
|---|
| 143 |
|
|---|
| 144 |
|
|---|
| 145 | return RedirectToAction(
|
|---|
| 146 | nameof(Index));
|
|---|
| 147 | }
|
|---|
| 148 | catch
|
|---|
| 149 | {
|
|---|
| 150 | transaction.Rollback();
|
|---|
| 151 |
|
|---|
| 152 | throw;
|
|---|
| 153 | }
|
|---|
| 154 | }
|
|---|
| 155 |
|
|---|
| 156 |
|
|---|
| 157 | // ==========================================
|
|---|
| 158 | // REMOVE FROM WISHLIST
|
|---|
| 159 | // ==========================================
|
|---|
| 160 |
|
|---|
| 161 | [HttpPost]
|
|---|
| 162 | [ValidateAntiForgeryToken]
|
|---|
| 163 | public IActionResult Remove(long productId)
|
|---|
| 164 | {
|
|---|
| 165 | var userId = GetUserId();
|
|---|
| 166 |
|
|---|
| 167 | if (userId == null)
|
|---|
| 168 | {
|
|---|
| 169 | return RedirectToAction(
|
|---|
| 170 | "Login",
|
|---|
| 171 | "Account");
|
|---|
| 172 | }
|
|---|
| 173 |
|
|---|
| 174 | var item = _context.WishlistProducts
|
|---|
| 175 | .Include(x => x.Wishlist)
|
|---|
| 176 | .FirstOrDefault(x =>
|
|---|
| 177 | x.ProductId == productId &&
|
|---|
| 178 | x.Wishlist.UserId == userId.Value);
|
|---|
| 179 |
|
|---|
| 180 | if (item != null)
|
|---|
| 181 | {
|
|---|
| 182 | _context.WishlistProducts.Remove(item);
|
|---|
| 183 | _context.SaveChanges();
|
|---|
| 184 |
|
|---|
| 185 | TempData["Success"] =
|
|---|
| 186 | "Product removed from your wishlist.";
|
|---|
| 187 | }
|
|---|
| 188 |
|
|---|
| 189 | return RedirectToAction(nameof(Index));
|
|---|
| 190 | }
|
|---|
| 191 |
|
|---|
| 192 |
|
|---|
| 193 | // ==========================================
|
|---|
| 194 | // MOVE WISHLIST PRODUCT TO CART
|
|---|
| 195 | // ==========================================
|
|---|
| 196 |
|
|---|
| 197 | [HttpPost]
|
|---|
| 198 | [ValidateAntiForgeryToken]
|
|---|
| 199 | public IActionResult AddToCart(long productId)
|
|---|
| 200 | {
|
|---|
| 201 | var userId = GetUserId();
|
|---|
| 202 |
|
|---|
| 203 | if (userId == null)
|
|---|
| 204 | {
|
|---|
| 205 | return RedirectToAction(
|
|---|
| 206 | "Login",
|
|---|
| 207 | "Account");
|
|---|
| 208 | }
|
|---|
| 209 |
|
|---|
| 210 | var product = _context.Products
|
|---|
| 211 | .Include(p => p.Release)
|
|---|
| 212 | .FirstOrDefault(p => p.ProductId == productId);
|
|---|
| 213 |
|
|---|
| 214 | if (product == null)
|
|---|
| 215 | return NotFound();
|
|---|
| 216 |
|
|---|
| 217 | if (product.Stock <= 0)
|
|---|
| 218 | {
|
|---|
| 219 | TempData["Error"] =
|
|---|
| 220 | "This product is currently out of stock.";
|
|---|
| 221 |
|
|---|
| 222 | return RedirectToAction(nameof(Index));
|
|---|
| 223 | }
|
|---|
| 224 |
|
|---|
| 225 | return RedirectToAction(
|
|---|
| 226 | "AddProduct",
|
|---|
| 227 | "Order",
|
|---|
| 228 | new { id = productId });
|
|---|
| 229 | }
|
|---|
| 230 |
|
|---|
| 231 |
|
|---|
| 232 | // ==========================================
|
|---|
| 233 | // USER ID
|
|---|
| 234 | // ==========================================
|
|---|
| 235 |
|
|---|
| 236 | private long? GetUserId()
|
|---|
| 237 | {
|
|---|
| 238 | var userId = HttpContext.Session.GetInt32("UserId");
|
|---|
| 239 |
|
|---|
| 240 | if (userId.HasValue)
|
|---|
| 241 | return userId.Value;
|
|---|
| 242 |
|
|---|
| 243 | return null;
|
|---|
| 244 | }
|
|---|
| 245 |
|
|---|
| 246 |
|
|---|
| 247 | // ==========================================
|
|---|
| 248 | // WISHLIST ID
|
|---|
| 249 | // ==========================================
|
|---|
| 250 |
|
|---|
| 251 | private long GetNextWishlistId()
|
|---|
| 252 | {
|
|---|
| 253 | var maxId = _context.Wishlists
|
|---|
| 254 | .Select(x => (long?)x.WishlistId)
|
|---|
| 255 | .Max();
|
|---|
| 256 |
|
|---|
| 257 | return (maxId ?? 0) + 1;
|
|---|
| 258 | }
|
|---|
| 259 | } |
|---|