Exemplo n.º 1
0
        /// <summary>
        /// Function to Add new order into datebase include: OrderOverView, OrderItem
        /// And Update Inventory for specific store
        /// </summary>
        /// <param name="order"></param>
        /// <returns>order success or fail message</returns>
        public int OrderPlaced(l.Order order)
        {
            IQueryable <d.Inventory> CurrentInventoryQ
                = dbcontext.Inventory.Where(i => i.StoreId == order.StoreId)
                  .AsNoTracking();
            IEnumerable <l.Inventory> CurrentInventoryE = CurrentInventoryQ.Select(Mapper.MapInventory);
            List <l.Inventory>        invent            = CurrentInventoryE.ToList();

            for (int j = 0; j < CurrentInventoryE.Count(); j++)
            {
                if (invent[j].Amount - order.Amount[j] < 0)
                {
                    logger.Info($"Inventory for {invent[j].InventoryId} is not enough");
                    return(-1);
                }
                else
                {
                    var inventory = dbcontext.Inventory.Where(i => i.ProductId == j + 1 &&
                                                              i.StoreId == order.StoreId)
                                    .First();
                    inventory.Amount = invent[j].Amount - order.Amount[j];
                    if (j == CurrentInventoryE.Count() - 1)
                    {
                        dbcontext.SaveChanges();
                        logger.Info("Inventory Updated!");
                    }
                }
            }

            d.OrderOverView orderOverView = Mapper.MapOrderOverView(order);
            dbcontext.Add(orderOverView);
            logger.Info("A new order overview is added into database");
            dbcontext.SaveChanges();

            int orderId = orderOverView.OrderId;

            string productName;
            int    orderAmount;
            IQueryable <d.Product> products        = dbcontext.Product;
            List <l.Product>       libraryProducts = products.Select(Mapper.MapProduct).ToList();

            for (int i = 0; i < 6; i++)
            {
                if (order.Amount[i] != 0)
                {
                    orderAmount = order.Amount[i];
                    productName = libraryProducts[i].ProductName;
                    d.OrderItem item
                        = Mapper.MapOrderItem(orderAmount, orderId, productName);
                    dbcontext.Add(item);
                }
            }

            dbcontext.SaveChanges();
            return(orderId);
        }
Exemplo n.º 2
0
        public void OrderTest()
        {
            List <int> amount = new List <int> {
                1
            };
            DateTime date = Convert.ToDateTime("2008/01/01");

            l.Order order = new l.Order
            {
                CustomerId = 1,
                StoreId    = 2,
                Amount     = amount,
                TotalPrice = (decimal)299.99,
                OrderDate  = date
            };

            Assert.Equal(1, order.CustomerId);
            Assert.Equal(2, order.StoreId);
            Assert.Equal(1, order.Amount[0]);
            Assert.Equal((decimal)299.99, order.TotalPrice);
            Assert.Equal(date, order.OrderDate);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Function to Add new order into datebase include: OrderOverView, OrderItem
        /// And Update Inventory for specific store
        /// </summary>
        /// <param name="order"></param>
        /// <returns>order success or fail message</returns>
        public string OrderPlaced(l.Order order)
        {
            IQueryable <d.Inventory> CurrentInventoryQ
                = dbcontext.Inventory.Where(i => i.StoreId == order.StoreId)
                  .AsNoTracking();
            IEnumerable <l.Inventory> CurrentInventoryE = CurrentInventoryQ.Select(Mapper.MapInventory);
            List <l.Inventory>        invent            = CurrentInventoryE.ToList();

            for (int j = 0; j < CurrentInventoryE.Count(); j++)
            {
                if (j == 0)
                {
                    if (invent[j].Amount - order.NSAmount < 0)
                    {
                        return("Order Failed, not enough Inventory");

                        logger.Warn($"Stoed ID: {order.StoreId} Doesn't have enough NSwitch in stock.");
                    }
                    else
                    {
                        var nsN = dbcontext.Inventory.Where(i => i.ProductId == j + 1)
                                  .First();
                        nsN.Amount = invent[j].Amount - order.NSAmount;
                    }
                }
                else if (j == 1)
                {
                    if (invent[j].Amount - order.XBAmount < 0)
                    {
                        return("Order Failed, not enough Inventory");

                        logger.Warn($"Stoed ID: {order.StoreId} Doesn't have enough NSwitch in stock.");
                    }
                    else
                    {
                        var xbN = dbcontext.Inventory.Where(i => i.ProductId == j + 1)
                                  .First();
                        xbN.Amount = invent[j].Amount - order.XBAmount;
                    }
                }
                else
                {
                    if (invent[j].Amount - order.PSAmount < 0)
                    {
                        return("Order Failed, not enough Inventory");

                        logger.Warn($"Stoed ID: {order.StoreId} Doesn't have enough NSwitch in stock.");
                    }
                    else
                    {
                        var psN = dbcontext.Inventory.Where(i => i.ProductId == j + 1)
                                  .First();
                        psN.Amount = invent[j].Amount - order.PSAmount;
                        dbcontext.SaveChanges();
                        logger.Info("Inventory updated");
                    }
                }
            }

            d.OrderOverView orderOverView = Mapper.MapOrderOverView(order);
            dbcontext.Add(orderOverView);
            logger.Info("A new order overview is added into database");
            dbcontext.SaveChanges();

            int orderId = orderOverView.OrderId;

            if (order.NSAmount != 0)
            {
                int         amount1 = order.NSAmount;
                string      name1   = "NSwitch";
                d.OrderItem itemN   = Mapper.MapOrderItem(amount1, orderId, name1);
                dbcontext.Add(itemN);
                logger.Info($"Order detail for {orderId} and {name1} is added to database");
            }
            if (order.XBAmount != 0)
            {
                int         amount2 = order.XBAmount;
                string      name2   = "Xbox One";
                d.OrderItem itemX   = Mapper.MapOrderItem(amount2, orderId, name2);
                dbcontext.Add(itemX);
                logger.Info($"Order detail for {orderId} and {name2} is added to database");
            }
            if (order.PSAmount != 0)
            {
                int         amount3 = order.PSAmount;
                string      name3   = "Playstation 4 Pro";
                d.OrderItem itemP   = Mapper.MapOrderItem(amount3, orderId, name3);
                dbcontext.Add(itemP);
                logger.Info($"Order detail for {orderId} and {name3} is added to database");
            }

            dbcontext.SaveChanges();
            return($"Order Success!! Your order Id is: {orderId}.");
        }