public void PopulateDB() { chain curChain; List <store> listOfStores = new List <store>(); List <item> listOfItems = new List <item>(); List <price> listOfPrices = new List <price>(); DirectoryInfo allPrices = new DirectoryInfo(@"C:\finalProject_PriceCompare\AllPrices\bin\prices"); foreach (var subDirectory in allPrices.GetDirectories()) { DecompressAllFiles(); string StoreFilePath = subDirectory.GetFiles().Where(t => t.Name.StartsWith("Stores")).Single().FullName; XDocument storesDoc = XDocument.Load(StoreFilePath); curChain = ChainToDB(storesDoc, _context); chain existingChain = _context.chains.FirstOrDefault(c => c.chain_id == curChain.chain_id); if (existingChain == null) { _context.chains.Add(curChain); _context.SaveChanges(); } listOfStores = StoresToDB(storesDoc, _context, curChain); listOfStores.ForEach(s => { _context.stores.Add(s); _context.SaveChanges(); }); FileInfo[] priceFullXmlFiles = subDirectory.GetFiles("PriceFull*.xml"); foreach (var file in priceFullXmlFiles) { listOfItems = ItemsToDB(file, _context); foreach (item item in listOfItems) { var existingItem = _context.items.FirstOrDefault(i => i.item_code == item.item_code); if (existingItem == null) { _context.items.Add(item); _context.SaveChanges(); } } } foreach (var file in priceFullXmlFiles) { listOfPrices = PricesToDB(file, _context); foreach (price price in listOfPrices) { var existingPrice = _context.prices.FirstOrDefault(p => p.item_code == price.item_code && p.store_key == price.store_key); if (existingPrice == null) { _context.prices.Add(price); _context.SaveChanges(); } } } } }
public float TotalCartPrice(chain chain) { float sum = 0; foreach (var price in _minPricesForAllChains[chain.chain_id]) { sum += price.price1; } return(sum); }
private chain ChainToDB(XDocument storesDoc, PriceCompareDBEntitie context) { XElement root = storesDoc.Root; chain chain = new chain(); long chain_id; long.TryParse(root.Element("ChainId").Value, out chain_id); chain.chain_id = chain_id; chain.chain_name = root.Element("ChainName").Value; return(chain); }
public price FindMinPriceForItemAndChain(item item, chain chain) { price minPrice; if (chain != null) { minPrice = _minPricesForAllChains[chain.chain_id].Find(x => x.item_code == item.item_code); } else { minPrice = null; } return(minPrice); }
public List <string> FindMissingItemsInCart(chain chain) { List <string> listOfMissingItems = new List <string>(); foreach (var item in _shoppingCart.selectedItems) { if (_minPricesForAllChains[chain.chain_id].Find(x => x.item_code == item.item_code) == null) { if (!listOfMissingItems.Contains(item.item_name)) { listOfMissingItems.Add(item.item_name); } } } return(listOfMissingItems); }
public List <price> GetMinimumPricesForChian(chain chain) { return(_minPricesForAllChains[chain.chain_id]); }
private List <store> StoresToDB(XDocument storesDoc, PriceCompareDBEntitie context, chain chain) { List <store> listOfStores = new List <store>(); XElement StoresElm = storesDoc.Root.Element("SubChains").Element("SubChain").Element("Stores"); int store_id; foreach (var Store in StoresElm.Elements("Store")) { store store = new store(); int.TryParse(Store.Element("StoreId").Value, out store_id); store.store_id = store_id; store.chain_id = chain.chain_id; store.store_type = null; store.store_name = Store.Element("StoreName").Value; store.address = Store.Element("Address").Value; store.city = Store.Element("City").Value; var existingStore = context.stores.FirstOrDefault(s => s.store_id == store.store_id && s.chain_id == store.chain_id); if (existingStore == null) { listOfStores.Add(store); } else { existingStore.chain_id = chain.chain_id; existingStore.store_type = null; existingStore.store_name = Store.Element("StoreName").Value; existingStore.address = Store.Element("Address").Value; existingStore.city = Store.Element("City").Value; } } return(listOfStores); }