Ejemplo n.º 1
0
        public async Task <User> PutUser(User user)
        {
            var oldUser = _context.Users.Where(u => u.UserId == user.UserId).FirstOrDefault();

            if (oldUser != null)
            {
                _context.Users.Remove(oldUser);

                _context.Users.Add(user);
                _context.Attach(user.UserLogin);

                if (user.Stores.Count > 0)
                {
                    foreach (var item in user.Stores)
                    {
                        _context.Attach(item);
                    }
                }


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

                return(user);
            }
            else
            {
                return(null);
            }
        }
Ejemplo n.º 2
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);
            }
        }