Exemplo n.º 1
0
        public async Task <bool> PutOrder(Order order)
        {
            var oldOrder = _context.Orders.Where(o => o.OrderId == order.OrderId).FirstOrDefault();



            if (oldOrder != null)
            {
                try
                {
                    _context.Orders.Remove(oldOrder);

                    _context.SaveChanges();

                    _context.Add(order);

                    //order.StoreOrder= null;

                    if (order.StoreOrder != null)
                    {
                        _context.Attach(order.StoreOrder);

                        if (order.StoreOrder.Products != null)
                        {
                            foreach (var item in order.StoreOrder.Products)
                            {
                                _context.Attach(item);
                            }
                        }


                        order.StoreOrder.WorkHours = null;

                        if (order.StoreOrder.WorkHours != null)
                        {
                            foreach (var item in order.StoreOrder.WorkHours)
                            {
                                _context.Attach(item);
                            }
                        }
                    }

                    ProductController productController = new ProductController(_context);
                    if (order.OrderStatus == Status.Submited)
                    {
                        foreach (var item in order.OrderProducts)
                        {
                            var product = _context.Products.Where(p => p.StoreId == item.StoreId && p.ProductName == item.ProductName).FirstOrDefault();

                            if (product != null && product.InventoryQuantity > 0)
                            {
                                var result = productController.UpdateInventoryFromOrderSubmited(product.ProductId, item.Quantity);
                            }
                        }
                    }



                    _context.SaveChanges();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }


                return(true);
            }
            else
            {
                return(false);
            }
        }