public async Task <IActionResult> UpdateQuantity(int id, UpdateBottleCountViewModel updatedWine) { var wine = await _context.Wine.FindAsync(id); wine.Quantity += updatedWine.newWine.Wine.Quantity; await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); }
public async Task <IActionResult> Create(CreateWineViewModel newWine) { var user = await GetCurrentUserAsync(); // If the user is already in the ModelState // Remove user from model state ModelState.Remove("wine.User"); ModelState.Remove("newWine.Wine.User"); //Checks to see if the newly added wine is already in the database List <Wine> ExistingWine = await _context.Wine .Include(w => w.Variety) .Include(w => w.Winery) .Where(w => w.Name == newWine.Wine.Name && w.WineryId == newWine.Wine.WineryId && w.VarietyId == newWine.Wine.VarietyId && w.Year == newWine.Wine.Year) .Where(w => w.ApplicationUserId == user.Id) .ToListAsync(); //If the wine already exists, this code allows the user to to update their current inventory with the amount of bottles they were going to add if (ExistingWine.Count > 0) { UpdateBottleCountViewModel viewModel = new UpdateBottleCountViewModel(); viewModel.existingWine = ExistingWine; viewModel.newWine = newWine; return(View("WineExists", viewModel)); } //If the wine doesn't exist, add it to the database if (ExistingWine.Count == 0) { // If model state is valid if (ModelState.IsValid) { // Add the user back newWine.Wine.ApplicationUserId = user.Id; // Add the wine _context.Add(newWine.Wine); // Save changes to database await _context.SaveChangesAsync(); // Redirect to details view with id of product made using new object return(RedirectToAction(nameof(Details), new { id = newWine.Wine.WineId.ToString() })); } } return(View(newWine)); }