public async Task <IActionResult> Withdraw(int id, decimal amount) { Account account = await _context.Accounts.FindAsync(id); if (amount < 0) { ModelState.AddModelError(nameof(amount), "Amount must not be lower than 0."); } if (amount.HasMoreThanTwoDecimalPlaces()) { ModelState.AddModelError(nameof(amount), "Amount cannot have more than 2 decimal places."); } if (!ModelState.IsValid) { ViewBag.Amount = amount; return(View(account)); } ATMMediator.Withdraw(account, amount, ModelState); if (!ModelState.IsValid) { return(View(account)); } await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); }
// Transfer method. // actionOrigin parameter is used in case if there is another kind of transfer in the future. Ex: transfer between accounts public async Task <IActionResult> Transfer(int id, int destinationAccountNumber, decimal amount, string comment, string actionOrigin) { Account account = await _context.Accounts.FindAsync(id); Account destinationAccount = await _context.Accounts.FindAsync(destinationAccountNumber); // input validation if (amount < 0) { ModelState.AddModelError(nameof(amount), "Amount must not be lower than 0."); } if (amount.HasMoreThanTwoDecimalPlaces()) { ModelState.AddModelError(nameof(amount), "Amount cannot have more than 2 decimal places."); } if (destinationAccount == null) { ModelState.AddModelError(nameof(destinationAccountNumber), "Account with this number is not found"); } if (!ModelState.IsValid) { return(ReturnWithError()); } // operation execution ATMMediator.Transfer(account, destinationAccount, amount, comment, ModelState); if (!ModelState.IsValid) { return(ReturnWithError()); } await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); // private method for repeatitive calling inside this method IActionResult ReturnWithError() { ViewBag.DestinationAccountNumber = destinationAccountNumber; ViewBag.Amount = amount; ViewBag.Comment = comment; return(View(actionOrigin, account)); } }