Esempio n. 1
0
        protected void Page_Load(object sender, EventArgs e)
        {
            checkoutForm.Visible = true;
            checkoutMessage.Visible = false;

            if (IsPostBack) {
                Order myOrder = new Order();
                if (TryUpdateModel(myOrder,
                   new FormValueProvider(ModelBindingExecutionContext))) {

                    myOrder.OrderLines = new List<OrderLine>();

                    Cart myCart = SessionHelper.GetCart(Session);

                    foreach (CartLine line in myCart.Lines) {
                        myOrder.OrderLines.Add(new OrderLine {
                            Order = myOrder,
                            Product = line.Product,
                            Quantity = line.Quantity
                        });
                    }

                    new Repository().SaveOrder(myOrder);
                    myCart.Clear();

                    checkoutForm.Visible = false;
                    checkoutMessage.Visible = true;
                }
            }
        }
Esempio n. 2
0
 public async Task<int> SaveOrderAsync(Order order)
 {
     if (order.Id == 0) {
         context.Orders.Add(order);
     }
     return await context.SaveChangesAsync();
 }
Esempio n. 3
0
        public async Task<IHttpActionResult> CreateOrder(Order order)
        {
            if (ModelState.IsValid)
            {
                IDictionary<int, Product> products = Repository.Products.Where(p => order.Lines.Select(ol => ol.ProductId).Any(id => id == p.Id)).ToDictionary(p => p.Id);
                order.TotalCost = order.Lines.Sum(ol => ol.Count * products[ol.ProductId].Price);
                await Repository.SaveOrdersAsync(order);

                return Ok();
            }
            else
            {
                return BadRequest(ModelState);
            }
        }
 public async Task<ActionResult> SaveOrder(Order order) {
     await repo.SaveOrderAsync(order);
     return RedirectToAction("Orders");
 }