Ejemplo n.º 1
0
        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;
        }
Ejemplo n.º 2
0
        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);
        }