Ejemplo n.º 1
0
        public async Task <IHttpActionResult> UpdateOrder(string paymentMethod)
        {
            try
            {
                var db = ApplicationDbContext.Create();

                var user = await db.Users
                           .Include("Orders")
                           .Include(i => i.Orders.Select(o => o.OrderedProducts))
                           .Include(i => i.Orders.Select(o => o.OrderedProducts.Select(op => op.Coffee))).FirstOrDefaultAsync(u => u.Email.Equals(User.Identity.Name));

                if (user == null)
                {
                    return(NotFound());
                }

                var order = user.Orders.Where(o => o.Status.Equals("draft")).FirstOrDefault();
                order.PaymentMethod = paymentMethod;

                order.Payed = paymentMethod.Equals("online") ? true : false;

                order.Status = "placed";
                ServiceStatusHub.PlacedOrdersNotification("Please check status of Orders!");
                var orderedProducts = order.OrderedProducts;

                var totalPrice = 0;
                foreach (var product in orderedProducts)
                {
                    totalPrice += int.Parse(product.Coffee.Price) * product.Quantity;
                }

                order.TotalPrice = totalPrice;

                var updateStatus = await db.SaveChangesAsync();

                if (updateStatus == 0)
                {
                    return(BadRequest("Cannot complete order"));
                }

                return(Ok("Order Placed"));
            }
            catch (Exception e)
            {
                return(BadRequest(e.ToString()));
            }
        }