Пример #1
0
        public bool Buy(ItemToSale product, out string errorMessage)
        {
            errorMessage = null;
            if (!EnoughMoneyToBuy(product))
            {
                errorMessage = "Недостаточно внесено денег для покупки";
                return false;
            }

            using (var context = new VendingContext())
            {
                var coinsInserted = context.MoneyCache.Select(x => x.Count * x.Nominal).ToList();
                var moneyInCacheCount = coinsInserted.Aggregate((arg1, arg2) => arg1 + arg2);
                var needReturn = moneyInCacheCount - product.Price;
                foreach (var coins in context.MoneyCache)
                {
                    AddToBank(coins.Nominal, coins.Count, context);
                }
                foreach (MoneyCache cache in context.MoneyCache)
                {
                    cache.Count = 0;
                }

                ReturnToCache(needReturn, context);
                var productToBuy = context.ItemsToSale.First(x => x.Id == product.Id);
                productToBuy.AvailableCount -= 1;
                context.SaveChanges();
            }
            return true;
        }
Пример #2
0
 public void AddProduct(ItemToSale item)
 {
     using (var context = new VendingContext())
     {
         context.ItemsToSale.Add(item);
         context.SaveChanges();
     }
 }
Пример #3
0
 public void AddToUserWallet(decimal coin, int count)
 {
     using (var context = new VendingContext())
     {
         AddToUserWallet(coin, count, context);
         context.SaveChanges();
     }
 }
Пример #4
0
 public List<Bank> GetBankCoins()
 {
     using (var context = new VendingContext())
     {
         return context.Bank.ToList();
     }
 }
Пример #5
0
 private void AddToUserWallet(decimal coin, int count, VendingContext context)
 {
     var coins = context.UserWallet.FirstOrDefault(x => x.Nominal == coin);
     if (coins != null)
     {
         coins.Count += count;
     }
     else
     {
         context.UserWallet.Add(new UserWallet { Nominal = coin, Count = count });
     }
 }
Пример #6
0
 private void ReturnToCache(decimal needReturn, VendingContext context)
 {
     var ordered = context.Bank.OrderByDescending(x => x.Nominal);
     foreach (var coins in ordered)
     {
         while (coins.Nominal <= needReturn && coins.Count != 0)
         {
             coins.Count -= 1;
             needReturn -= coins.Nominal;
             AddToCache(coins.Nominal, 1, context);
         }
         if (needReturn <= 0) return;
     }
 }
Пример #7
0
 private void AddToBank(decimal coin, int count, VendingContext context)
 {
     var coins = context.Bank.FirstOrDefault(x => x.Nominal == coin);
     if (coins != null)
     {
         coins.Count += count;
     }
     else
     {
         context.Bank.Add(new Bank { Nominal = coin, Count = count });
     }
 }
Пример #8
0
 private void AddToCache(decimal coin, int count, VendingContext context)
 {
     var coins = context.MoneyCache.FirstOrDefault(x => x.Nominal == coin);
     if (coins != null)
     {
         coins.Count += count;
     }
     else
     {
         context.MoneyCache.Add(new MoneyCache { Nominal = coin, Count = count });
     }
 }
Пример #9
0
 public void MoneyBack()
 {
     using (var context = new VendingContext())
     {
         foreach (var coins in context.MoneyCache)
         {
             AddToUserWallet(coins.Nominal, coins.Count, context);
         }
         foreach (MoneyCache cache in context.MoneyCache)
         {
             cache.Count = 0;
         }
         context.SaveChanges();
     }
 }
Пример #10
0
 public void Reset()
 {
     using (var context = new VendingContext())
     {
         context.Database.ExecuteSqlCommand("TRUNCATE TABLE [wares]");
         context.Database.ExecuteSqlCommand("TRUNCATE TABLE [bank]");
         context.Database.ExecuteSqlCommand("TRUNCATE TABLE [money_cache]");
         context.Database.ExecuteSqlCommand("TRUNCATE TABLE [user_wallet]");
     }
 }
Пример #11
0
        public void InsertCoin(decimal nominal)
        {
            using (var context = new VendingContext())
            {
                var coins = context.UserWallet.FirstOrDefault(x => x.Nominal == nominal);
                if (coins != null && coins.Count >= 1)
                {
                    coins.Count -= 1;
                    AddToCache(nominal, 1);
                    context.SaveChanges();
                }

            }
        }
Пример #12
0
 public List<UserWallet> GetUserCoins()
 {
     using (var context = new VendingContext())
     {
         return context.UserWallet.Where(x => x.Count > 0).ToList();
     }
 }
Пример #13
0
 public List<ItemToSale> GetProducts()
 {
     using (var context = new VendingContext())
     {
         return context.ItemsToSale.ToList();
     }
 }
Пример #14
0
 public ItemToSale GetProduct(int productId)
 {
     using (var context = new VendingContext())
     {
         return context.ItemsToSale.FirstOrDefault(x => x.Id == productId);
     }
 }
Пример #15
0
 public List<MoneyCache> GetCacheCoins()
 {
     using (var context = new VendingContext())
     {
         return context.MoneyCache.ToList();
     }
 }