Ejemplo n.º 1
0
        public async Task <IActionResult> Create([Bind("NoteId,Value")] Note note)
        {
            if (ModelState.IsValid)
            {
                _context.Add(note);
                await _context.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }
            return(View(note));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> Create([Bind("CoinSizeId,Size")] CoinSize coinSize)
        {
            if (ModelState.IsValid)
            {
                _context.Add(coinSize);
                await _context.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }
            return(View(coinSize));
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> Create([Bind("UserId,Name,Balance")] User user)
        {
            if (ModelState.IsValid)
            {
                _context.Add(user);
                await _context.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }
            return(View(user));
        }
Ejemplo n.º 4
0
        public async Task <IActionResult> Create
            ([Bind("WithdrawId,WithdrawDate,Amount,AtmId,UserId")] WithdrawViewModel withdraw)
        {
            if (ModelState.IsValid)
            {
                var model = withdraw.ToModel(_context);

                bool hasError = model.AutomatedTellerMachine == null || model.User == null;

                try
                {
                    model.CurrencyDictionary = WithdrawHelper.WithdrawFromAtm(model.User, model.AutomatedTellerMachine, withdraw.Amount, _context);
                }
                catch (InsufficentFundsException)
                {
                    ModelState.AddModelError("UserId", $"User {model.User.Name} has insufficient funds (${model.User.Balance}) to withdraw ${withdraw.Amount}");
                    hasError = true;
                }
                catch (InsufficientChangeException)
                {
                    ModelState.AddModelError("AtmId", $"ATM {model.AutomatedTellerMachine.Alias} has insufficient change to withdraw ${withdraw.Amount}");
                    hasError = true;
                }
                catch
                {
                    throw;
                }

                if (model.AutomatedTellerMachine == null)
                {
                    ModelState.AddModelError("AtmId", "ATM not found");
                }

                if (model.User == null)
                {
                    ModelState.AddModelError("UserId", "User not found");
                }

                if (hasError)
                {
                    ViewBag.Users = await GetUsers();

                    ViewBag.Atms = await GetAtms();

                    return(View(withdraw));
                }

                model.WithdrawDate = DateTime.Now;

                _context.Add(model);
                await _context.SaveChangesAsync();

                (TempData["SuccessMessage"], TempData["InfoMessages"]) = BuildWithdrawMessage(model);

                return(RedirectToAction("Index"));
            }
            return(View(withdraw));
        }
        public async Task <IActionResult> Create(AutomatedTellerMachineViewModel automatedTellerMachine)
        {
            if (ModelState.IsValid)
            {
                var warnings = new List <string>();
                var model    = automatedTellerMachine.ToModel(_context);

                // We should not add any notes or coins if the ATM has not a dispenser
                // to spit them out
                if (!model.HasNoteDispenser)
                {
                    model.CurrencyDictionary.NoteDictionary = new Dictionary <Note, int>();
                    warnings.Add("Note dispenser not present, values cleared");
                }

                // We should not add coins if they're bigger than the biggest dispenser,
                // otherwise how could they get out?
                var biggestDispenserSize = model.CoinDispensersDictionary.Max(kvp => kvp.Key.Size);

                foreach (var coin in model.CurrencyDictionary.CoinDictionary.Keys)
                {
                    if (coin.Size.Size > biggestDispenserSize)
                    {
                        model.CurrencyDictionary.CoinDictionary[coin] = 0;
                        warnings.Add($"Coin of size {coin.Size.Size} too big, values cleared");
                    }
                }

                _context.Add(model);
                await _context.SaveChangesAsync();

                TempData["SuccessMessage"] = $"ATM added succesfully! Alias: {model.Alias}";
                TempData["WarningMessage"] = warnings;
                return(RedirectToAction("Index"));
            }
            return(View(automatedTellerMachine));
        }
Ejemplo n.º 6
0
        public async Task <IActionResult> Create([Bind("CoinId,Value,Size")] CoinViewModel coin)
        {
            if (ModelState.IsValid)
            {
                var size  = _context.CoinSize.Find(coin.Size);
                var model = new Coin()
                {
                    CoinId = coin.CoinId,
                    Value  = coin.Value,
                    Size   = size
                };

                _context.Add(model);
                await _context.SaveChangesAsync();

                TempData["SuccessMessage"] = "Coin created sucessfully";
                return(RedirectToAction("Index"));
            }
            else
            {
                ViewBag.CoinSizes = await GetCoinSizes();
            }
            return(View(coin));
        }