public OrderSummary(Order order, List<OrderDetail> orderDetail)
 {
     this.Order = order;
     this.OrderDetail = orderDetail;
 }
示例#2
0
        public ActionResult Submit(OrderModel Model)
        {
            if (Model == null) {
                return Json(new {
                    Success = false,
                    Messages = new string[] { "Invalid order!" }
                });
            }

            List<string> messages = new List<string>();

            if (string.IsNullOrEmpty(Model.Customer)) {
                messages.Add("Please provide customer name!");
            }

            if (string.IsNullOrEmpty(Model.Address)) {
                messages.Add("Please provide shipping address!");
            }

            if (!Model.OrderItems.Any()) {
                messages.Add("Please add at least one order item!");
            } else {
                if (Model.OrderItems.Any(x => x.Product == null)) {
                    messages.Add("Please choose product for each order item!");
                }

                if (Model.OrderItems.Any(x => x.Quantity < 1)) {
                    messages.Add("Please set quantity of each order item!");
                }

                if (AnyWrongOrderItems(Model)) {
                    messages.Add("Please do not hack product prices!");
                }
            }

            if (messages.Any()) {
                return Json(new {
                    Success = false,
                    Messages = messages
                });
            }

            Order order = new Order() {
                Date = DateTime.Now,
                Customer = Model.Customer,
                Address = Model.Address
            };

            foreach (OrderItemModel item in Model.OrderItems) {
                OrderItem orderItem = new OrderItem() {
                    Product = item.Product,
                    Quantity = item.Quantity,
                    Price = item.Price
                };

                orderItem.TotalAmount = orderItem.Quantity * orderItem.Price;
                order.TotalAmount += orderItem.TotalAmount;
                order.OrderItems.Add(orderItem);
            }

            db.Orders.Add(order);
            db.SaveChanges();

            return Json(new {
                Success = true
            });
        }