public IActionResult TransferBetweenAccounts(TransferBetweenAccountsViewModel model) { if (ValidateAccounts(model) is JsonResult json && json.Value is string error) { ModelState.AddModelError(nameof(model.AccountId1), error); } if (!ModelState.IsValid) { return(View("TransferBetweenAccounts", model)); } Account account1 = _bankRepository.GetAccount(model.AccountId1 ?? 0); Account account2 = _bankRepository.GetAccount(model.AccountId2 ?? 0); try { account1.Transfer(account2, model.Amount); ViewData["TransferSuccess"] = true; } catch (Exception e) { ModelState.AddModelError(nameof(model.Amount), e.Message); } return(View("TransferBetweenAccounts", model)); }
public IActionResult ValidateAccounts(TransferBetweenAccountsViewModel model) { if (!(model.AccountId1 is null) && model.AccountId2 is null && _bankRepository.GetAccount(model.AccountId1.Value) is null) { return(Json($"Account #{model.AccountId1} doesnt exist.")); } if (!(model.AccountId2 is null) && _bankRepository.GetAccount(model.AccountId2.Value) is null) { return(Json($"Account #{model.AccountId2} doesnt exist.")); } if (model.AccountId1 == model.AccountId2) { return(Json($"Account can not transfer to itself.")); } return(Json(true)); }