Ejemplo n.º 1
0
 public OrderItemDTO(OrderItem order)
 {
     this.MenuItemId = order.MenuItemId;
     this.OrderItemId = order.OrderItemId;
     this.OrderId = order.OrderId;
     this.Quantity = order.Quantity;
 }
Ejemplo n.º 2
0
        // POST api/Default1
        public HttpResponseMessage PostOrder(IList<CartItem> cartItems)
        {
            if (ModelState.IsValid)
            {
                var user = new UserProfile();
                using (CanteenContext ctx = new CanteenContext())
                {
                    user = (from o in ctx.UserProfiles orderby o.UserId where o.UserName == User.Identity.Name select o).ToList().FirstOrDefault();
                }
                db.Entry(user).State = EntityState.Unchanged;
                var order = new Order()
                {
                    DateConfirmed = DateTime.Now,
                    DateCreated = DateTime.Now,
                    DateDelivered = DateTime.Now,
                    IsConfirmed = false,
                    IsDelivered = false,

                    User = user
                };
                db.Orders.Add(order);
                db.SaveChanges();

                foreach (var i in cartItems)
                {
                    var orderItem = new OrderItem()
                    {
                        MenuItemId = i.Id,
                        OrderId = order.OrderId,
                        Quantity = i.Quantity
                    };

                    db.OrderItems.Add(orderItem);
                }

                db.SaveChanges();

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, cartItems);
                return response;
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }