private void CheckInventory(StoreCart storeCart, MarketDbContext context) { Guid storeID = storeCart.StoreID; IStoreInventoryManager store = StoreHandler.GetStoreInventoryManager(storeID, context); store.IsOrderItemsAmountAvailable(storeCart.Items, context); }
public void SearchItems_ByName_spellMistake_SpellingCannotHelp_ShouldFail() { using var context = new MarketDbContext(inMemoryConnection); Store[] stores = DataForTests.CreateStoresForSearchTests(storeHandler, context); IStoreInventoryManager inventoryManager3 = storeHandler.GetStoreInventoryManager(stores[3].Id, context); inventoryManager3.AddItem("apple computer", 300, new HashSet <string>() { "electrical" }, 2000, context, keyWords: new HashSet <string>() { "computers", "electronic" }); Dictionary <Guid, ReadOnlyCollection <Item> > results = searchFacade.SearchItems(context: context, filterItemRank: null, filterMinPrice: 11, filterMaxPrice: 2000, filterStoreRank: null, name: "paplee compuetr", category: "electrical"); Assert.AreEqual(0, results.Keys.Count); }
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); }
public void GetStoreInventoryManager_ShouldPass() { using var context = new MarketDbContext(inMemoryConnection); StoreContactDetails storeContactDetails = DataForTests.CreateTestContactDetails(); storeHandler.OpenStore(storeContactDetails, Owner, context); Store store = storeHandler.GetStoreByName("store", context); IStoreInventoryManager sim = storeHandler.GetStoreInventoryManager(store.Id, context); Assert.AreEqual(store, sim); }
public void SearchItems_ByCategory_StoreRankFilterItemRankFilter_ShouldPass() { using var context = new MarketDbContext(inMemoryConnection); SearchFilter storeRankFilter = new FilterByStoreRank(4.5); SearchFilter itemRankFilter = new FilterByItemRank(6.5); List <SearchFilter> filters = new List <SearchFilter>() { storeRankFilter, itemRankFilter }; Store[] stores = DataForTests.CreateStoresForSearchTests(storeHandler, context); storeHandler.SetStoreRankById(stores[0].Id, 4.5, context); storeHandler.SetStoreRankById(stores[1].Id, 3, context); //update rank of item2 of "store" ReadOnlyCollection <Item> items_Store = stores[0].GetStoreItems(); Guid itemId2 = items_Store[1].Id; Dictionary <StoresUtils.ItemEditDetails, object> newDetails_item2 = new Dictionary <StoresUtils.ItemEditDetails, object>() { { StoresUtils.ItemEditDetails.rank, 6.5 } }; IStoreInventoryManager storeInventoryManager = storeHandler.GetStoreInventoryManager(stores[0].Id, context); storeInventoryManager.EditItem(itemId2, newDetails_item2, context); //update rank of item5 of "store1" ReadOnlyCollection <Item> items_Store1 = stores[1].GetStoreItems(); Guid itemId5 = items_Store1[3].Id; Dictionary <StoresUtils.ItemEditDetails, object> newDetails_item5 = new Dictionary <StoresUtils.ItemEditDetails, object>() { { StoresUtils.ItemEditDetails.rank, 6.9 } }; IStoreInventoryManager storeInventoryManager1 = storeHandler.GetStoreInventoryManager(stores[1].Id, context); storeInventoryManager1.EditItem(itemId5, newDetails_item5, context); //start search test Dictionary <Guid, ReadOnlyCollection <Item> > results = storeHandler.SearchItems(context: context, category: "cat2", filters: filters); Assert.AreEqual(1, results.Keys.Count); ReadOnlyCollection <Item> resultStore = results[stores[0].Id]; Assert.AreEqual("item two", resultStore[0].Name); Assert.AreEqual(1, resultStore.Count); }
internal bool ItemExistInStore(Guid storeID, Guid itemID, MarketDbContext context) { IStoreInventoryManager storeInventory = GetAsInventoryManager(storeID, context); try { Item i = storeInventory.GetItemById(itemID); return(i != null); } catch (ItemNotFoundException) { return(false); } }
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); } }
internal void ValidatePurchase(User user, MarketDbContext context) { foreach (KeyValuePair <Guid, StoreCart> storeCart in user.Cart.StoreCarts) { IStoreInventoryManager store = StoreHandler.GetStoreInventoryManager(storeCart.Key, context); try { store.ValidatePurchase(storeCart.Value, user, context); CheckInventory(storeCart.Value, context); } catch (PolicyException e) { throw new PolicyException(store.GetName(), e.Message); } catch (ItemAmountException e) { throw new ItemAmountException(storeCart.Key, e.Message); } } }
/// <summary> /// Return requested item. /// If item was not found ItemNotFoundException is thrown /// </summary> /// <param name="storeID"></param> /// <param name="itemId"></param> /// <returns> The requested item</returns> public Item GetItemByIdFromStore(Guid storeID, Guid itemId, MarketDbContext context) { IStoreInventoryManager store = storeHandler.GetStoreInventoryManager(storeID, context); return(store.GetItemById(itemId)); }
public static Store[] CreateStoresForSearchTests(StoreHandler storeHandler, MarketDbContext context) { RegisteredUser owner = new RegisteredUser("TEMP", new byte[] { }); context.Users.Add(owner); context.SaveChanges(); Guid Owner = owner.ID; Store[] stores = new Store[4]; StoreContactDetails contactDetails = CreateTestContactDetails(); contactDetails.Name = "store"; storeHandler.OpenStore(contactDetails, Owner, context); Store store = storeHandler.GetStoreByName(contactDetails.Name, context); stores[0] = store; StoreContactDetails contactDetails1 = CreateTestContactDetails(); contactDetails1.Name = "store1"; storeHandler.OpenStore(contactDetails1, Owner, context); Store store1 = storeHandler.GetStoreByName(contactDetails1.Name, context); stores[1] = store1; StoreContactDetails contactDetails2 = CreateTestContactDetails(); contactDetails2.Name = "store2"; storeHandler.OpenStore(contactDetails2, Owner, context); Store store2 = storeHandler.GetStoreByName(contactDetails2.Name, context); stores[2] = store2; StoreContactDetails contactDetails3 = CreateTestContactDetails(); contactDetails3.Name = "store3"; storeHandler.OpenStore(contactDetails3, Owner, context); Store store3 = storeHandler.GetStoreByName(contactDetails3.Name, context); stores[3] = store3; //store items: IStoreInventoryManager inventoryManager = storeHandler.GetStoreInventoryManager(store.Id, context); inventoryManager.AddItem("item one", 20, new HashSet <string>() { "cat1" }, 20.4, context, new HashSet <string>() { "word1" }); inventoryManager.AddItem("item two", 30, new HashSet <string>() { "cat2" }, 20.4, context, new HashSet <string>() { "word2" }); inventoryManager.AddItem("item three", 200, new HashSet <string>() { "cat1", "cat2" }, 20.4, context, new HashSet <string>() { "word3" }); //store1 items: IStoreInventoryManager inventoryManager1 = storeHandler.GetStoreInventoryManager(store1.Id, context); inventoryManager1.AddItem("item one", 20, new HashSet <string>() { "cat1" }, 20.4, context, new HashSet <string>() { "word1" }); inventoryManager1.AddItem("item two", 30, new HashSet <string>() { "cat2" }, 10, context, new HashSet <string>() { "word2" }); inventoryManager1.AddItem("item four", 500, new HashSet <string>() { "cat1", "cat2" }, 20.4, context, new HashSet <string>() { "word3", "word4" }); inventoryManager1.AddItem("item five", 700, new HashSet <string>() { "cat1", "cat2", "cat3" }, 50, context, new HashSet <string>() { "word4", "word50" }); //store2 items: IStoreInventoryManager inventoryManager2 = storeHandler.GetStoreInventoryManager(store2.Id, context); inventoryManager2.AddItem("item one", 30, new HashSet <string>() { "cat1" }, 20.5, context, new HashSet <string>() { "word1" }); inventoryManager2.AddItem("item two", 300, new HashSet <string>() { "cat2" }, 10, context, new HashSet <string>() { "word2" }); inventoryManager2.AddItem("item three", 200, new HashSet <string>() { "cat1", "cat2" }, 20.4, context, new HashSet <string>() { "word3" }); inventoryManager2.AddItem("item four", 3000, new HashSet <string>() { "cat1", "cat2" }, 20.4, context, new HashSet <string>() { "word3", "word4" }); inventoryManager2.AddItem("item five", 30000, new HashSet <string>() { "cat1", "cat2", "cat5" }, 50, context, new HashSet <string>() { }); //store3 items IStoreInventoryManager inventoryManager3 = storeHandler.GetStoreInventoryManager(store3.Id, context); inventoryManager3.AddItem("item twenty", 20, new HashSet <string>() { "cat20" }, 200, context, new HashSet <string>() { "word50" }); return(stores); }
internal bool CheckSufficentAmountInInventory(Guid storeID, Guid itemID, int amountToCheck, MarketDbContext context) { IStoreInventoryManager storeInventory = StoreHandler.GetStoreInventoryManager(storeID, context); return(storeInventory.GetItemById(itemID).Amount >= amountToCheck); }