public IActionResult UpdatedCart(int userId, LineItems changedLineItem) { var openCart = _repository.FindOpenCarts(userId); bool cartDoesExist = openCart != null; if (cartDoesExist) { var shoppingCartItems = _repository.GetLineItem(changedLineItem.InvoiceId); var findCartItems = shoppingCartItems.ToList().Find(x => x.LineItemId == changedLineItem.LineItemId); if (changedLineItem.Quantity == 0) { // if line item quantity is zero then delete the line item var multiplyQuantity = findCartItems.Price * findCartItems.Quantity; _repository.DeleteLineItem(changedLineItem.LineItemId); openCart = _repository.UpdateTotalCost(multiplyQuantity, changedLineItem.InvoiceId); } else { // if the line item exists then modify it var cartDif = findCartItems.Quantity - changedLineItem.Quantity; var multiDiff = cartDif * findCartItems.Price; var updatedLineItem = _repository.ChangeLineItemQty(changedLineItem.Quantity, changedLineItem.LineItemId); openCart = _repository.UpdateTotalCost(multiDiff, changedLineItem.InvoiceId); } } return(Ok(openCart)); }