Exemplo n.º 1
0
        public async Task<ActionResult> AddProduct(int id, int productId)
        {
            RSProduct product = await db.Products.FindAsync(productId);
            RSOrder order = await db.Orders.FindAsync(id);

            if (product != null && order != null)
            {
                RSOrderItem orderItem = new RSOrderItem(product, order);

                db.OrderItems.Add(orderItem);
                await db.SaveChangesAsync();
                return Json(true);
            }
            else throw new HttpException(400, "Error inserting OrderItem");
        }
Exemplo n.º 2
0
        public async Task<RSOrderItem> AddOrderItem(int id, [FromBody]int productId)
        {
            RSOrder order = await db.Orders.FindAsync(id);

            if (!order.Active) //Disallow editing closed orders
                return null;

            RSProduct product = await db.Products.FindAsync(productId);
            if (order != null && product != null)
            {
                RSOrderItem oi = new RSOrderItem(product, order);
                db.OrderItems.Add(oi);
                await db.SaveChangesAsync();
                return new RSOrderItem()
                {
                    Id = oi.Id,
                    Price = oi.Price,
                    State = oi.State,
                    Product = oi.Product != null ? new RSProduct()
                    {
                        Id = oi.Product.Id,
                        Title = oi.Product.Title
                    } : null
                };
            }
            else return null;
        }