public async Task <IActionResult> Edit(int itemId) { var model = new CartEditViewModel(); var order = await GetOrderAsync(); var lineItems = order.LineItems.Where(x => x.ItemID == itemId); var lineItem = lineItems.LastOrDefault(); if (lineItem != null) { model.ItemID = lineItem.ItemID; model.Quantity = lineItems.Count(); } return(PartialView("_Edit", model)); }
public async Task <IActionResult> Edit(CartEditViewModel model) { var success = true; var msg = string.Empty; try { var orderTask = GetOrderAsync(); var itemTask = _api.GetAsync <ItemModel>($"/items/{model.ItemID}"); await Task.WhenAll(orderTask, itemTask); var item = itemTask.Result; var order = orderTask.Result; var itemCount = order.LineItems.Count; if (itemCount > 0) { if (model.Quantity > itemCount) { // add for (int i = 0; i < model.Quantity - itemCount; i++) { success &= AddLineItem(item, order); } } else if (model.Quantity < itemCount) { // remove for (int i = 0; i < itemCount - model.Quantity && i < itemCount; i++) { _api.Delete($"/lineItems/{order.LineItems[i].ID}"); } } } } catch (Exception ex) { msg = ex.Message; success = false; } return(Json(new { success, msg })); }