protected Decimal CalculateTotalAmount(Cart cart) { Decimal totalAmount = (Decimal)0.0; foreach (Cart_Product cp in cart.ProductsInTheCart) { Product p = db.Products.Single(a => a.Id == cp.ProductId); totalAmount += p.Price * cp.Amount; } return totalAmount; }
public async Task<IHttpActionResult> PostCart(Cart cart) { if (cart.Id == Guid.Empty) // if no guid is provided cart.Id = Guid.NewGuid(); // generates an ID for the newly created Cart object // set dates to now cart.CreatedOn = DateTime.Now; cart.LastModified = DateTime.Now; if (!ModelState.IsValid) { return BadRequest(ModelState); } db.Carts.Add(cart); try { await db.SaveChangesAsync(); } catch (DbUpdateException dbUpdateEx) { Console.WriteLine(dbUpdateEx.Message); if (CartExists(cart.Id)) { return Conflict(); } else { throw; } } return CreatedAtRoute("DefaultApi", new { id = cart.Id }, cart); }