Ejemplo n.º 1
0
        internal Order DiscountAndReserve(Guid?userID, Dictionary <Guid, StoreCart> storeCarts, DbAccess.MarketDbContext context)
        {
            if (storeCarts == null || !storeCarts.Any())
            {
                Logger.writeEvent("OrderManager: ReserveItems| shopping cart is empty");
                throw new OrderNotValidException($"{nameof(DiscountAndReserve)}: shopping cart is null or empty");
            }
            Guid orderID = Guid.NewGuid();
            List <StoreOrder> storeOrders = new List <StoreOrder>();

            foreach (KeyValuePair <Guid, StoreCart> storeCart in storeCarts)
            {
                try
                {
                    Guid storeOrderID            = Guid.NewGuid();
                    IStoreInventoryManager store = StoreHandler.GetStoreInventoryManager(storeCart.Key, context);
                    StoreHandler.AquireLockOnStore(storeCart.Key);
                    List <Tuple <Item, int, double> > itemsWithPrices;
                    try
                    {
                        store.IsOrderItemsAmountAvailable(storeCart.Value, context);
                        itemsWithPrices = store.GetDiscountedPriceAndReserve(storeCart.Value, context);
                    }
                    finally
                    {
                        StoreHandler.ReleaseLockOnStore(storeCart.Key);
                    }

                    List <OrderItem> itemList = new List <OrderItem>();
                    foreach (Tuple <Item, int, double> itemAmountPrice in itemsWithPrices)
                    {
                        OrderItem orderItem = new OrderItem(storeOrderID, itemAmountPrice.Item1, itemAmountPrice.Item2, itemAmountPrice.Item3);
                        itemList.Add(orderItem);
                    }
                    Logger.writeEvent(string.Format("OrderManager: ReserveItems| new Store Order was created for store: {0}", storeCart.Key));
                    StoreOrder storeOrder = new StoreOrder(storeOrderID, orderID, userID, storeCart.Key,
                                                           itemList, PurchaseType.IMMEDIATE, DateTime.Now);
                    storeOrders.Add(storeOrder);
                }
                catch (Exception e)
                {
                    foreach (StoreOrder storeOrder in storeOrders)// revert order
                    {
                        RevertStoreOrder(storeOrder, context);
                    }
                    throw e;
                }
            }
            Order order = new Order(orderID, userID, storeOrders, PurchaseType.IMMEDIATE, DateTime.Now);

            context.Orders.Add(order);
            context.SaveChanges();
            return(order);
        }
Ejemplo n.º 2
0
        private void RevertStoreOrder(StoreOrder storeOrder, MarketDbContext context)
        {
            IStoreInventoryManager store = StoreHandler.GetStoreInventoryManager(storeOrder.StoreId, context);

            StoreHandler.AquireLockOnStore(storeOrder.StoreId);
            try
            {
                store.ReturnItemsFromOrder(storeOrder.StoreOrderItems.ToList(), context);
            }
            finally
            {
                StoreHandler.ReleaseLockOnStore(storeOrder.StoreId);
            }
        }