| 1 | using KernelRecordsMVC.Application.ViewModels;
|
|---|
| 2 | using KernelRecordsMVC.Domain.Enums;
|
|---|
| 3 | using KernelRecordsMVC.Infrastructure.Data;
|
|---|
| 4 | using KernelRecordsMVC.Models;
|
|---|
| 5 | using Microsoft.AspNetCore.Mvc;
|
|---|
| 6 | using Microsoft.EntityFrameworkCore;
|
|---|
| 7 |
|
|---|
| 8 | namespace KernelRecordsMVC.Web.Controllers;
|
|---|
| 9 |
|
|---|
| 10 | public class OrderController : Controller
|
|---|
| 11 | {
|
|---|
| 12 | private readonly KernelRecordsContext _context;
|
|---|
| 13 |
|
|---|
| 14 | public OrderController(KernelRecordsContext context)
|
|---|
| 15 | {
|
|---|
| 16 | _context = context;
|
|---|
| 17 | }
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 | // ==========================================
|
|---|
| 21 | // ADD PRODUCT TO ORDER
|
|---|
| 22 | // UC007
|
|---|
| 23 | // ==========================================
|
|---|
| 24 |
|
|---|
| 25 | [HttpGet]
|
|---|
| 26 | public IActionResult AddProduct(long id)
|
|---|
| 27 | {
|
|---|
| 28 | var userId = GetUserId();
|
|---|
| 29 |
|
|---|
| 30 | if (userId == null)
|
|---|
| 31 | return RedirectToAction(
|
|---|
| 32 | "Login",
|
|---|
| 33 | "Account");
|
|---|
| 34 |
|
|---|
| 35 | var product = _context.Products
|
|---|
| 36 | .Include(p => p.Release)
|
|---|
| 37 | .FirstOrDefault(p => p.ProductId == id);
|
|---|
| 38 |
|
|---|
| 39 | if (product == null)
|
|---|
| 40 | return NotFound();
|
|---|
| 41 |
|
|---|
| 42 | if (product.Stock <= 0)
|
|---|
| 43 | {
|
|---|
| 44 | TempData["Error"] =
|
|---|
| 45 | "This product is currently out of stock.";
|
|---|
| 46 |
|
|---|
| 47 | return RedirectToAction(
|
|---|
| 48 | "Details",
|
|---|
| 49 | "Release",
|
|---|
| 50 | new { id = product.ReleaseId });
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 | // Find an existing pending order.
|
|---|
| 55 | var order = _context.Orders
|
|---|
| 56 | .Include(o => o.OrderProducts)
|
|---|
| 57 | .FirstOrDefault(o =>
|
|---|
| 58 | o.UserId == userId.Value &&
|
|---|
| 59 | o.Status == OrderStatusType.PENDING);
|
|---|
| 60 |
|
|---|
| 61 |
|
|---|
| 62 | // If there is no pending order, create one.
|
|---|
| 63 | if (order == null)
|
|---|
| 64 | {
|
|---|
| 65 | order = new Order
|
|---|
| 66 | {
|
|---|
| 67 | UserId = userId.Value,
|
|---|
| 68 | PaymentMethod = PaymentMethodType.CARD,
|
|---|
| 69 | PurchaseDate = DateTime.Today,
|
|---|
| 70 | PointsEarned = 0,
|
|---|
| 71 | PointsUsed = null,
|
|---|
| 72 | Status = OrderStatusType.PENDING
|
|---|
| 73 | };
|
|---|
| 74 |
|
|---|
| 75 | _context.Orders.Add(order);
|
|---|
| 76 |
|
|---|
| 77 | _context.SaveChanges();
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 |
|
|---|
| 81 | // Check whether product is already in cart.
|
|---|
| 82 | var existingItem = order.OrderProducts
|
|---|
| 83 | .FirstOrDefault(x =>
|
|---|
| 84 | x.ProductId == product.ProductId);
|
|---|
| 85 |
|
|---|
| 86 |
|
|---|
| 87 | if (existingItem != null)
|
|---|
| 88 | {
|
|---|
| 89 | if (existingItem.Quantity + 1 >
|
|---|
| 90 | product.Stock)
|
|---|
| 91 | {
|
|---|
| 92 | TempData["Error"] =
|
|---|
| 93 | "There is not enough stock available.";
|
|---|
| 94 |
|
|---|
| 95 | return RedirectToAction(
|
|---|
| 96 | "Details",
|
|---|
| 97 | "Release",
|
|---|
| 98 | new { id = product.ReleaseId });
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | existingItem.Quantity++;
|
|---|
| 102 | }
|
|---|
| 103 | else
|
|---|
| 104 | {
|
|---|
| 105 | var discount = _context.ModificationProducts
|
|---|
| 106 | .Include(mp => mp.Modification)
|
|---|
| 107 | .Where(mp =>
|
|---|
| 108 | mp.ProductId == product.ProductId &&
|
|---|
| 109 | mp.Modification.TypeOfModification ==
|
|---|
| 110 | ModificationType.DISCOUNT &&
|
|---|
| 111 | mp.Modification.Discount.HasValue &&
|
|---|
| 112 | mp.Modification.Discount.Value > 0)
|
|---|
| 113 | .Select(mp => mp.Modification)
|
|---|
| 114 | .OrderByDescending(m => m.DateModified)
|
|---|
| 115 | .FirstOrDefault();
|
|---|
| 116 |
|
|---|
| 117 | var priceAtPurchase = product.Price;
|
|---|
| 118 |
|
|---|
| 119 | if (discount != null)
|
|---|
| 120 | {
|
|---|
| 121 | priceAtPurchase =
|
|---|
| 122 | product.Price -
|
|---|
| 123 | (product.Price * discount.Discount!.Value / 100m);
|
|---|
| 124 |
|
|---|
| 125 | priceAtPurchase = Math.Round(
|
|---|
| 126 | priceAtPurchase,
|
|---|
| 127 | 2);
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | var orderProduct = new OrderProduct
|
|---|
| 131 | {
|
|---|
| 132 | OrderId = order.OrderId,
|
|---|
| 133 |
|
|---|
| 134 | ProductId = product.ProductId,
|
|---|
| 135 |
|
|---|
| 136 | PriceAtPurchase = product.Price,
|
|---|
| 137 |
|
|---|
| 138 | Quantity = 1
|
|---|
| 139 | };
|
|---|
| 140 |
|
|---|
| 141 | _context.OrderProducts.Add(orderProduct);
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | _context.SaveChanges();
|
|---|
| 145 |
|
|---|
| 146 |
|
|---|
| 147 | return RedirectToAction(nameof(Cart));
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 |
|
|---|
| 151 | // ==========================================
|
|---|
| 152 | // CART
|
|---|
| 153 | // ==========================================
|
|---|
| 154 |
|
|---|
| 155 | [HttpGet]
|
|---|
| 156 | public IActionResult Cart()
|
|---|
| 157 | {
|
|---|
| 158 | var userId = GetUserId();
|
|---|
| 159 |
|
|---|
| 160 | if (userId == null)
|
|---|
| 161 | {
|
|---|
| 162 | return RedirectToAction(
|
|---|
| 163 | "Login",
|
|---|
| 164 | "Account");
|
|---|
| 165 | }
|
|---|
| 166 |
|
|---|
| 167 |
|
|---|
| 168 | var order = _context.Orders
|
|---|
| 169 | .Include(o => o.OrderProducts)
|
|---|
| 170 | .ThenInclude(op => op.Product)
|
|---|
| 171 | .ThenInclude(p => p.Release)
|
|---|
| 172 | .FirstOrDefault(o =>
|
|---|
| 173 | o.UserId == userId.Value &&
|
|---|
| 174 | o.Status == OrderStatusType.PENDING);
|
|---|
| 175 |
|
|---|
| 176 |
|
|---|
| 177 | var viewModel = new CartViewModel();
|
|---|
| 178 |
|
|---|
| 179 |
|
|---|
| 180 | if (order != null)
|
|---|
| 181 | {
|
|---|
| 182 | viewModel.Items = order.OrderProducts
|
|---|
| 183 | .Select(op => new CartItemViewModel
|
|---|
| 184 | {
|
|---|
| 185 | ProductId = op.ProductId,
|
|---|
| 186 |
|
|---|
| 187 | ReleaseId =
|
|---|
| 188 | op.Product.ReleaseId,
|
|---|
| 189 |
|
|---|
| 190 | ReleaseTitle =
|
|---|
| 191 | op.Product.Release.Title,
|
|---|
| 192 |
|
|---|
| 193 | Format =
|
|---|
| 194 | op.Product.Format.ToString(),
|
|---|
| 195 |
|
|---|
| 196 | Price =
|
|---|
| 197 | op.PriceAtPurchase,
|
|---|
| 198 |
|
|---|
| 199 | Quantity =
|
|---|
| 200 | op.Quantity,
|
|---|
| 201 |
|
|---|
| 202 | Stock =
|
|---|
| 203 | op.Product.Stock,
|
|---|
| 204 |
|
|---|
| 205 | CoverPhoto = op.Product.Release.CoverPhoto
|
|---|
| 206 | })
|
|---|
| 207 | .ToList();
|
|---|
| 208 | }
|
|---|
| 209 |
|
|---|
| 210 |
|
|---|
| 211 | return View(viewModel);
|
|---|
| 212 | }
|
|---|
| 213 |
|
|---|
| 214 |
|
|---|
| 215 | // ==========================================
|
|---|
| 216 | // REMOVE PRODUCT
|
|---|
| 217 | // ==========================================
|
|---|
| 218 |
|
|---|
| 219 | [HttpPost]
|
|---|
| 220 | [ValidateAntiForgeryToken]
|
|---|
| 221 | public IActionResult RemoveProduct(long id)
|
|---|
| 222 | {
|
|---|
| 223 | var userId = GetUserId();
|
|---|
| 224 |
|
|---|
| 225 | if (userId == null)
|
|---|
| 226 | return RedirectToAction(
|
|---|
| 227 | "Login",
|
|---|
| 228 | "Account");
|
|---|
| 229 |
|
|---|
| 230 |
|
|---|
| 231 | var item = _context.OrderProducts
|
|---|
| 232 | .Include(x => x.Order)
|
|---|
| 233 | .FirstOrDefault(x =>
|
|---|
| 234 | x.ProductId == id &&
|
|---|
| 235 | x.Order.UserId == userId.Value &&
|
|---|
| 236 | x.Order.Status ==
|
|---|
| 237 | OrderStatusType.PENDING);
|
|---|
| 238 |
|
|---|
| 239 |
|
|---|
| 240 | if (item != null)
|
|---|
| 241 | {
|
|---|
| 242 | _context.OrderProducts.Remove(item);
|
|---|
| 243 | _context.SaveChanges();
|
|---|
| 244 | }
|
|---|
| 245 |
|
|---|
| 246 |
|
|---|
| 247 | return RedirectToAction(nameof(Cart));
|
|---|
| 248 | }
|
|---|
| 249 |
|
|---|
| 250 |
|
|---|
| 251 | // ==========================================
|
|---|
| 252 | // UPDATE QUANTITY
|
|---|
| 253 | // ==========================================
|
|---|
| 254 |
|
|---|
| 255 | [HttpPost]
|
|---|
| 256 | [ValidateAntiForgeryToken]
|
|---|
| 257 | public IActionResult UpdateQuantity(
|
|---|
| 258 | long id,
|
|---|
| 259 | long quantity)
|
|---|
| 260 | {
|
|---|
| 261 | var userId = GetUserId();
|
|---|
| 262 |
|
|---|
| 263 | if (userId == null)
|
|---|
| 264 | return RedirectToAction(
|
|---|
| 265 | "Login",
|
|---|
| 266 | "Account");
|
|---|
| 267 |
|
|---|
| 268 |
|
|---|
| 269 | if (quantity <= 0)
|
|---|
| 270 | {
|
|---|
| 271 | return RemoveProduct(id);
|
|---|
| 272 | }
|
|---|
| 273 |
|
|---|
| 274 |
|
|---|
| 275 | var item = _context.OrderProducts
|
|---|
| 276 | .Include(x => x.Order)
|
|---|
| 277 | .Include(x => x.Product)
|
|---|
| 278 | .FirstOrDefault(x =>
|
|---|
| 279 | x.ProductId == id &&
|
|---|
| 280 | x.Order.UserId == userId.Value &&
|
|---|
| 281 | x.Order.Status ==
|
|---|
| 282 | OrderStatusType.PENDING);
|
|---|
| 283 |
|
|---|
| 284 |
|
|---|
| 285 | if (item == null)
|
|---|
| 286 | return NotFound();
|
|---|
| 287 |
|
|---|
| 288 |
|
|---|
| 289 | if (quantity > item.Product.Stock)
|
|---|
| 290 | {
|
|---|
| 291 | TempData["Error"] =
|
|---|
| 292 | "The requested quantity exceeds available stock.";
|
|---|
| 293 |
|
|---|
| 294 | return RedirectToAction(nameof(Cart));
|
|---|
| 295 | }
|
|---|
| 296 |
|
|---|
| 297 |
|
|---|
| 298 | item.Quantity = quantity;
|
|---|
| 299 |
|
|---|
| 300 | _context.SaveChanges();
|
|---|
| 301 |
|
|---|
| 302 |
|
|---|
| 303 | return RedirectToAction(nameof(Cart));
|
|---|
| 304 | }
|
|---|
| 305 |
|
|---|
| 306 |
|
|---|
| 307 | // ==========================================
|
|---|
| 308 | // USER ID
|
|---|
| 309 | // ==========================================
|
|---|
| 310 |
|
|---|
| 311 | private long? GetUserId()
|
|---|
| 312 | {
|
|---|
| 313 | var userId = HttpContext.Session.GetInt32("UserId");
|
|---|
| 314 |
|
|---|
| 315 | if (userId.HasValue)
|
|---|
| 316 | return userId.Value;
|
|---|
| 317 |
|
|---|
| 318 | return null;
|
|---|
| 319 | }
|
|---|
| 320 | } |
|---|