/// <summary> /// Load coin stocks from db to calculate coin change. /// </summary> /// <returns>Coin stocks available</returns> public List <CoinStock> LoadCoinStocks() { List <CoinStock> list = new List <CoinStock>(); using (DrinksDistributorContext dbContext = new DrinksDistributorContext()) { list = dbContext.CoinStocks.Include("CoinType").ToList(); // 'Include' For lazyloading. } return(list); }
/// <summary> /// Load drinks from db. /// </summary> /// <returns>Drinks available</returns> public List <Drink> LoadDrinks() { List <Drink> list = new List <Drink>(); using (DrinksDistributorContext dbContext = new DrinksDistributorContext()) { list = dbContext.Drinks.Include("PriceCurrency").ToList(); // 'Include' For lazyloading. } return(list); }
/// <summary> /// Update coin stocks quantities into db. /// </summary> public void UpdateCoinStocksQuantities(Dictionary <double, int> stockToUpdate) { using (DrinksDistributorContext dbContext = new DrinksDistributorContext()) { foreach (var coinType in stockToUpdate) { var coinStock = dbContext.CoinStocks.SingleOrDefault(cs => cs.CoinType.Value == coinType.Key); if (coinStock != null) { try { // Update quantity coinStock.Quantity = (coinStock.Quantity - coinType.Value); dbContext.SaveChanges(); } catch (Exception ex) { throw ex; } } } } }