public async Task <IActionResult> DeleteItem([FromBody] ItemToShoppingBoxDTO itemFromShoppingBox) { if (!(User.IsInRole("Customer") || User.IsInRole("Admin"))) { return(RedirectToAction("Index", "Home")); } await shoppingCardService.DeleteItemFromBox(itemFromShoppingBox.Id, itemFromShoppingBox.Category, userManager.GetUserId(HttpContext.User)); return(Ok()); }
public async Task <IActionResult> SaveItemToBox([FromBody] ItemToShoppingBoxDTO itemToShoppingBoxDTO) { if (!(User.IsInRole("Customer") || User.IsInRole("Admin"))) { return(RedirectToAction("Index", "Home")); } string nameOfItem; decimal priceOfItem; if (itemToShoppingBoxDTO.Category == "pizza") { var pizzas = await menuService.GetPizzas(); var item = pizzas.Where(x => x.Id == itemToShoppingBoxDTO.Id).FirstOrDefault(); nameOfItem = item.Name; priceOfItem = item.Price; var items = await shoppingCardService.GetItemsFromBox(userManager.GetUserId(HttpContext.User)); var existInBox = items.Where(x => x.MenuId == itemToShoppingBoxDTO.Id && x.Category == itemToShoppingBoxDTO.Category && x.Status == 1).FirstOrDefault(); if (existInBox != null) { existInBox.Quantity += 1; await shoppingCardService.UpdateItem(existInBox); return(Ok()); } ShoppingCard shoppingCard = new ShoppingCard() { DatetimeAdd = DateTime.UtcNow.ToLocalTime(), Category = itemToShoppingBoxDTO.Category, MenuId = itemToShoppingBoxDTO.Id, Status = 1, Name = nameOfItem, Price = priceOfItem, Quantity = 1, UserId = userManager.GetUserId(HttpContext.User) }; await shoppingCardService.AddItemToBox(shoppingCard); } else if (itemToShoppingBoxDTO.Category == "drink") { var drinks = await menuService.GetDrinks(); var item = drinks.Where(x => x.Id == itemToShoppingBoxDTO.Id).FirstOrDefault(); nameOfItem = item.Name; priceOfItem = item.Price; var items = await shoppingCardService.GetItemsFromBox(userManager.GetUserId(HttpContext.User)); var existInBox = items.Where(x => x.MenuId == itemToShoppingBoxDTO.Id && x.Category == itemToShoppingBoxDTO.Category && x.Status == 1).FirstOrDefault(); if (existInBox != null) { existInBox.Quantity += 1; await shoppingCardService.UpdateItem(existInBox); return(Ok()); } ShoppingCard shoppingCard = new ShoppingCard() { DatetimeAdd = DateTime.UtcNow.ToLocalTime(), Category = itemToShoppingBoxDTO.Category, MenuId = itemToShoppingBoxDTO.Id, Status = 1, Name = nameOfItem, Price = priceOfItem, Quantity = 1, UserId = userManager.GetUserId(HttpContext.User) }; await shoppingCardService.AddItemToBox(shoppingCard); } else if (itemToShoppingBoxDTO.Category == "dessert") { var dessert = await menuService.GetDesserts(); var item = dessert.Where(x => x.Id == itemToShoppingBoxDTO.Id).FirstOrDefault(); nameOfItem = item.Name; priceOfItem = item.Price; var items = await shoppingCardService.GetItemsFromBox(userManager.GetUserId(HttpContext.User)); var existInBox = items.Where(x => x.MenuId == itemToShoppingBoxDTO.Id && x.Category == itemToShoppingBoxDTO.Category && x.Status == 1).FirstOrDefault(); if (existInBox != null) { existInBox.Quantity += 1; await shoppingCardService.UpdateItem(existInBox); return(Ok()); } ShoppingCard shoppingCard = new ShoppingCard() { DatetimeAdd = DateTime.UtcNow.ToLocalTime(), Category = itemToShoppingBoxDTO.Category, MenuId = itemToShoppingBoxDTO.Id, Status = 1, Name = nameOfItem, Price = priceOfItem, Quantity = 1, UserId = userManager.GetUserId(HttpContext.User) }; await shoppingCardService.AddItemToBox(shoppingCard); } else if (itemToShoppingBoxDTO.Category == "pasta") { var pasta = await menuService.GetPastas(); var item = pasta.Where(x => x.Id == itemToShoppingBoxDTO.Id).FirstOrDefault(); nameOfItem = item.Name; priceOfItem = item.Price; var items = await shoppingCardService.GetItemsFromBox(userManager.GetUserId(HttpContext.User)); var existInBox = items.Where(x => x.MenuId == itemToShoppingBoxDTO.Id && x.Category == itemToShoppingBoxDTO.Category && x.Status == 1).FirstOrDefault(); if (existInBox != null) { existInBox.Quantity += 1; await shoppingCardService.UpdateItem(existInBox); return(Ok()); } ShoppingCard shoppingCard = new ShoppingCard() { DatetimeAdd = DateTime.UtcNow.ToLocalTime(), Category = itemToShoppingBoxDTO.Category, MenuId = itemToShoppingBoxDTO.Id, Status = 1, Name = nameOfItem, Price = priceOfItem, Quantity = 1, UserId = userManager.GetUserId(HttpContext.User) }; await shoppingCardService.AddItemToBox(shoppingCard); } else { return(NotFound()); } return(Ok()); }