public async Task <IActionResult> Edit(int id, [Bind("HotShotId,ProductId,NewPrice,Quantity,ItemsSold,StartDate,EndDate,HasEnded")] HotShot hotShot) { if (id != hotShot.HotShotId) { return(NotFound()); } if (ModelState.IsValid) { try { _context.Update(hotShot); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!HotShotExists(hotShot.HotShotId)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } ViewData["ProductId"] = new SelectList(_context.Products, "ProductId", "Name", hotShot.ProductId); return(View(hotShot)); }
public async Task <IActionResult> Create([Bind("HotShotId,ProductId,NewPrice,Quantity,ItemsSold,StartDate,EndDate,HasEnded")] HotShot hotShot) { if (ModelState.IsValid) { _context.Add(hotShot); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } ViewData["ProductId"] = new SelectList(_context.Products, "ProductId", "Name", hotShot.ProductId); return(View(hotShot)); }
//public async Task<IActionResult> Details(int? id) public async Task <IActionResult> Details(int?id, bool disableHotShot) { if (id == null) { return(NotFound()); } var product = await _context.Products .Include(p => p.Brand) .Include(p => p.Category) .ThenInclude(d => d.Section) .Include(p => p.Gallery) .FirstOrDefaultAsync(m => m.ProductId == id); var isHotShot = _context.HotShots.Any(d => d.StartDate <DateTime.Now && d.EndDate> DateTime.Now && d.Quantity > d.ItemsSold && d.ProductId == product.ProductId); if (disableHotShot) { isHotShot = false; } HotShot hotShot = null; if (isHotShot) { hotShot = await _context.HotShots.Include(d => d.Product.Gallery.ImageGalleries) .ThenInclude(d => d.Image) .FirstOrDefaultAsync(d => d.StartDate <DateTime.Now && d.EndDate> DateTime.Now); } if (product == null) { return(NotFound()); } var ratings = _context.Ratings.Where(d => d.ProductId == product.ProductId); var sum = await ratings.SumAsync(d => d.Value); var ratingCount = await ratings.CountAsync(); var avg = 0; if (ratingCount > 0) { avg = sum / ratingCount; } //formattedEndDate = hotShot.EndDate.ToString("yyyy-MM-dd HH:mm:ss"); var model = new ProductDetailsViewModel() { Product = product, Tags = await TagManager.GetTagNameWithValues(_context, product), Images = await ImageManager.GetImagesForProduct(_context, (int)id), IsHotShot = isHotShot, HotShot = hotShot, FormattedDate = hotShot?.EndDate.ToString("yyyy-MM-dd HH:mm:ss"), Ratings = avg, RatingsCount = ratingCount }; return(View(model)); }
public async Task <ActionResult> AddToCart(Product product, bool isHotShot, int quantity = 1) { var productIsExcludedFromSale = await _context.Products .AnyAsync(d => d.ProductId == product.ProductId && d.IsActive == false); if (quantity < 0 || productIsExcludedFromSale || (isHotShot && quantity > 1)) { return(NotFound()); } var carts = _context.ShoppingCarts; _userId = User.FindFirstValue(ClaimTypes.NameIdentifier); var hotShotAlreadyBought = false; HotShot hotShot = null; if (isHotShot) { hotShot = await _context.HotShots.Include(d => d.Product .Gallery.ImageGalleries).ThenInclude(d => d.Image) .FirstOrDefaultAsync(d => d.StartDate <DateTime.Now && d.EndDate> DateTime.Now); hotShotAlreadyBought = _context.AccountHotShots.Where(d => d.IdentityUserId == _userId) .Any(d => d.HotShotId == hotShot.HotShotId.ToString()); } if (isHotShot || !carts.Where(d => d.IdentityUserId == _userId) .Where(d => d.HotShotId == null).Any(d => d.ProductId == product.ProductId)) { if ((isHotShot && carts.Any(d => d.HotShotId != null)) || hotShotAlreadyBought) { return(RedirectToAction("Index", new { error = AppErrorMessage.HotShotAlreadyBought })); } if (hotShot != null && hotShot.Quantity <= hotShot.ItemsSold) { return(RedirectToAction("Index", new { error = AppErrorMessage.SaleHasEnded })); } carts.Add(new ShoppingCart() { Quantity = quantity, ProductId = product.ProductId, IdentityUserId = _userId, HotShotId = hotShot?.HotShotId }); } else { var cart = carts.Where(d => d.IdentityUserId == _userId) .FirstOrDefault(d => d.ProductId == product.ProductId && d.HotShotId == null); if (cart != null) { cart.Quantity += quantity; } } await _context.SaveChangesAsync(); return(RedirectToAction("Index")); }