public IActionResult CreateEditDishAvail(int?id, int dishId, DishAvailability avail) { if (id != avail.Id) { TempData["StatusMessage"] = "#E#:Invalid information to save the Dish Availability."; return(RedirectToAction(nameof(IndexDishAvail), new { dishId = dishId })); } if (ModelState.IsValid) { _repoAvail.Save(avail); TempData["StatusMessage"] = $"The Dish Availability information was saved."; return(RedirectToAction(nameof(IndexDishAvail), new { dishId = dishId })); } if ((ViewData["availDish"] = _repoDish.GetAll.FirstOrDefault(d => d.Id == dishId)) == null) { TempData["StatusMessage"] = "#E#:Unable to find the Dish to create/edit their Availability."; return(RedirectToAction(nameof(IndexDish))); } return(View(avail)); }
public async Task <IActionResult> CreateEditDishAvail(int?id, int dishId, DishAvailability avail) { var dish = await _dishRepo.GetAll.FirstOrDefaultAsync(d => d.Id == dishId); if (dish == null) { TempData["StatusMessage"] = "#E#:Unable to find the Dish to edit/create their Availability."; return(RedirectToAction(nameof(IndexDishAvail), new { dishId = dishId })); } var user = await _userManager.GetUserAsync(User); if (dish.Partner.Id != user?.Id) { TempData["StatusMessage"] = "#E#:You are not authorized to access this dish."; return(RedirectToAction(nameof(IndexDishAvail), new { dishId = dishId })); } if ((avail.Id != id) || (avail.DishId != dishId)) { TempData["StatusMessage"] = "#E#:Invalid information to save the Dish Availability."; return(RedirectToAction(nameof(IndexDishAvail), new { dishId = dishId })); } // If it is a edit, verify the quantity. Otherwise, set the quantity as the total if (id != 0) { var dbAvail = await _availRepo.GetAll.FirstOrDefaultAsync(d => d.Id == id); if (dbAvail == null) { TempData["StatusMessage"] = "#E#:Unable to find the Dish Availability to edit."; return(RedirectToAction(nameof(IndexDishAvail), new { dishId = dishId })); } var dishSold = dbAvail.QuantityTotal - dbAvail.Quantity; if (dishSold > avail.QuantityTotal) { ModelState.AddModelError("QuantityTotal", $"You cannot define the quantity as less then ${dishSold}, as they was already sold."); } else { avail.Quantity = avail.QuantityTotal - dishSold; } if ((dishSold > 0) && ((dbAvail.StartDate.CompareTo(avail.StartDate) != 0) || (dbAvail.StartDate.CompareTo(avail.StartDate) != 0))) { ModelState.AddModelError("StartDate", "You cannot change the Available from or Expire date when this dish was was already sold."); } } else { avail.Quantity = avail.QuantityTotal; } // Validate all the fields for both new and edit var interval = avail.EndDate.Subtract(avail.StartDate).Hours; if (avail.StartDate.CompareTo(avail.EndDate) >= 0) { ModelState.AddModelError("StartDate", $"The Available from must be less than Expire date field."); } else if (interval < 2) { ModelState.AddModelError("StartDate", $"The Dish Availability should have at least two hours of interval."); } else if (interval >= 20) { ModelState.AddModelError("StartDate", $"The Dish Availability cannot exceed twenty hours of interval."); } if (avail.StartDate.CompareTo(DateTime.Now) < 0) { ModelState.AddModelError("StartDate", $"The available from field must be greater than the current date."); } if ((avail.StartDate.Hour < 6) || (avail.StartDate.Hour >= 22)) { ModelState.AddModelError("StartDate", $"The Start Date must be between 6:00AM and 21:59PM."); } if ((avail.EndDate.Hour > 2) && (avail.EndDate.Hour <= 7)) { ModelState.AddModelError("EndDate", $"The End Date must be between 8:00AM and 2:59AM."); } // Validate if we have at least if (ModelState.IsValid) { _availRepo.Save(avail); TempData["StatusMessage"] = $"The Dish Availability information was saved."; return(RedirectToAction(nameof(IndexDishAvail), new { dishId = dishId })); } ViewData["availDish"] = dish; return(View(avail)); }