예제 #1
0
        public async Task <IActionResult> PutFoodItem(int id, FoodItem foodItem)
        {
            if (id != foodItem.FoodItemId)
            {
                return(BadRequest());
            }

            _context.Entry(foodItem).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!FoodItemExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
        public async Task <IActionResult> PutCustomer(int id, Customer customer)
        {
            if (id != customer.CustomerId)
            {
                return(BadRequest());
            }

            _context.Entry(customer).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CustomerExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
예제 #3
0
        public async Task <IActionResult> PutOrder(int id, Order order)
        {
            try
            {
                var ord = _context.orders.Where(p => p.OrderId == order.OrderId).Include(p => p.OrderItem).SingleOrDefault();

                if (ord != null)
                {
                    //update order
                    _context.Entry(ord).CurrentValues.SetValues(order);
                    // Delete children
                    foreach (var existingOrdItem in ord.OrderItem.ToList())
                    {
                        if (!order.OrderItem.Any(c => c.OrderItemId == existingOrdItem.OrderItemId))
                        {
                            _context.OrderItems.Remove(existingOrdItem);
                        }
                    }

                    //update and insert orderitem
                    foreach (var orderItemModel in order.OrderItem)
                    {
                        var ordItem = ord.OrderItem.Where(c => c.OrderItemId == orderItemModel.OrderItemId).SingleOrDefault();

                        if (ordItem != null)
                        {
                            //update orderitem
                            _context.Entry(ordItem).CurrentValues.SetValues(orderItemModel);
                        }
                        else
                        {
                            //insert orderitem
                            var newitem = new OrderItem
                            {
                                OrderItemId = orderItemModel.OrderItemId,
                                Quantity    = orderItemModel.Quantity,
                                Amount      = orderItemModel.Amount,
                                FoodItemId  = orderItemModel.FoodItemId,
                            };
                            ord.OrderItem.Add(newitem);
                        }
                    }

                    await _context.SaveChangesAsync();
                }
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!OrderExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(Ok());
        }
예제 #4
0
        public async Task <ActionResult <Order> > PostOrder(Order order)
        {
            _context.Order.Add(order);
            await _context.SaveChangesAsync();

            return(CreatedAtAction("GetOrder", new { id = order.OrderId }, order));
        }