Example #1
0
 public Withdraw ToModel(CoinifyWebContext context)
 {
     return(new Withdraw()
     {
         WithdrawId = WithdrawId,
         User = context.User.Find(UserId),
         AutomatedTellerMachine = context
                                  .AutomatedTellerMachine
                                  .Include(atm => atm.CurrencyDictionary)
                                  .SingleOrDefault(atm => atm.AutomatedTellerMachineId == AtmId)
     });
 }
Example #2
0
 public AutomatedTellerMachine ToModel(CoinifyWebContext context)
 {
     return(new AutomatedTellerMachine()
     {
         AutomatedTellerMachineId = AutomatedTellerMachineId,
         Alias = Alias,
         CurrencyDictionary = new CurrencyDictionary()
         {
             CoinDictionary = CoinDictionary.ToDictionary(k => context.Coin.Find(k.Key), v => v.Value),
             NoteDictionary = NoteDictionary.ToDictionary(k => context.Note.Find(k.Key), v => v.Value),
         },
         HasNoteDispenser = HasNoteDispenser,
         CoinDispensersDictionary = CoinDispensersDictionary
                                    .ToDictionary(k => context.CoinSize.Find(k.Key), v => v.Value)
     });
 }
Example #3
0
        public static void EnsureSeedData(this CoinifyWebContext context)
        {
            var(coinSizes, coins, notes, users, atm, currencyDictionary) = CreateEntitiesArrays();

            if (context.Database.GetPendingMigrations().Count() > 0)
            {
                context.Database.Migrate();
            }

            if (!context.CoinSize.Any())
            {
                context.CoinSize.AddRange(coinSizes);
            }

            if (!context.Coin.Any())
            {
                context.Coin.AddRange(coins);
            }

            if (!context.Note.Any())
            {
                context.Note.AddRange(notes);
            }

            if (!context.AutomatedTellerMachine.Any())
            {
                context.CurrencyDictionary.Add(currencyDictionary);
                context.AutomatedTellerMachine.Add(atm);
            }

            if (!context.User.Any())
            {
                context.User.AddRange(users);
            }

            context.SaveChanges();
        }
Example #4
0
        public static CurrencyDictionary WithdrawFromAtm(User user, AutomatedTellerMachine atm, int amount, CoinifyWebContext context = null)
        {
            var curDict = atm.CurrencyDictionary;

            var ret = new CurrencyDictionary()
            {
                CoinDictionary = new Dictionary <Coin, int>(),
                NoteDictionary = new Dictionary <Note, int>()
            };

            if (user.Balance - amount < 0)
            {
                throw new InsufficentFundsException($"User {user.UserId} has insuficient funds to withdraw ${amount}");
            }

            user.Balance -= amount;

            var validMoney = GenerateValidMoneyDictionary(curDict);

            foreach (var kvp in validMoney.ToList())
            {
                while (validMoney[kvp.Key] != 0)
                {
                    if ((amount - kvp.Key.Value) < 0)
                    {
                        break;
                    }

                    amount -= kvp.Key.Value;
                    validMoney[kvp.Key]--;

                    if (kvp.Key is Note)
                    {
                        var note = kvp.Key as Note;

                        if (ret.NoteDictionary.ContainsKey(note))
                        {
                            ret.NoteDictionary[note]++;
                        }
                        else
                        {
                            ret.NoteDictionary[note] = 1;
                        }

                        curDict.NoteDictionary[note]--;
                    }
                    else if (kvp.Key is Coin)
                    {
                        var coin = kvp.Key as Coin;

                        if (ret.CoinDictionary.ContainsKey(coin))
                        {
                            ret.CoinDictionary[coin]++;
                        }
                        else
                        {
                            ret.CoinDictionary[coin] = 1;
                        }

                        curDict.CoinDictionary[coin]--;
                    }
                    else
                    {
                        break;
                    }
                }
            }
            if (amount > 0)
            {
                throw new
                      InsufficientChangeException($"There is no change in ATM {atm.AutomatedTellerMachineId} to withdraw ${amount}");
            }

            if (context != null)
            {
                context.Update(user);
                context.Update(atm);

                context.SaveChanges();
            }

            return(ret);
        }
Example #5
0
 public NotesController(CoinifyWebContext context)
 {
     _context = context;
 }
Example #6
0
        public async static Task <IEnumerable <ReportLeastUsedMoneyViewModel> > GenerateMoneyReport(CoinifyWebContext context, int?id, bool shouldOrder = true)
        {
            var             tmpDict      = new Dictionary <Tuple <string, int>, Dictionary <string, KeyValuePair <Money, int> > >();
            List <Withdraw> withdrawList = new List <Withdraw>();

            if (id == null)
            {
                withdrawList = await context
                               .Withdraw
                               .Include(w => w.CurrencyDictionary)
                               .Include(w => w.AutomatedTellerMachine)
                               .ToListAsync();
            }
            else
            {
                withdrawList = await context
                               .Withdraw
                               .Include(w => w.CurrencyDictionary)
                               .Include(w => w.AutomatedTellerMachine)
                               .Where(w => w.AutomatedTellerMachine.AutomatedTellerMachineId == id)
                               .ToListAsync();
            }

            foreach (var withdraw in withdrawList)
            {
                var atm = withdraw.AutomatedTellerMachine;
                var key = new Tuple <string, int>
                              (atm.Alias, atm.AutomatedTellerMachineId);

                if (!tmpDict.ContainsKey(key))
                {
                    tmpDict.Add(key, new Dictionary <string, KeyValuePair <Money, int> >());
                }

                foreach (var money in withdraw.CurrencyDictionary.MoneyDictionary)
                {
                    var moneyKey = $"{money.Key.GetType().FullName}${money.Key.Value}${money.Key.MoneyId}";

                    if (!tmpDict[key].ContainsKey(moneyKey))
                    {
                        tmpDict[key].Add(moneyKey, new KeyValuePair <Money, int>(money.Key, money.Value));
                    }
                    else
                    {
                        var kvp = tmpDict[key][moneyKey];
                        tmpDict[key][moneyKey] = new KeyValuePair <Money, int>(kvp.Key, kvp.Value + money.Value);
                    }
                }
            }

            return(tmpDict.Select(kvp => new ReportLeastUsedMoneyViewModel()
            {
                AtmId = kvp.Key.Item2,
                AtmAlias = kvp.Key.Item1,
                Report = shouldOrder ?
                         kvp.Value.Select(_ => _.Value).OrderBy(_ => _.Value).ToDictionary(k => k.Key, k => k.Value)
                    : kvp.Value.ToDictionary(k => k.Value.Key, k => k.Value.Value)
            }));
        }
 public WithdrawsController(CoinifyWebContext context)
 {
     _context = context;
 }
 public AutomatedTellerMachinesController(CoinifyWebContext context)
 {
     _context = context;
 }
Example #9
0
 public CoinSizesController(CoinifyWebContext context)
 {
     _context = context;
 }
Example #10
0
 public UsersController(CoinifyWebContext context)
 {
     _context = context;
 }