public IActionResult Deposite(TransactionViewModel model) { if (ModelState.IsValid) { if (model.Amount > 0) { var charge = _configuration.GetValue <float>("DepositeCharge"); var depositePercent = Math.Round((100.0 - charge) / 100.0, 3); var newDeposite = _mapper.Map <TransactionViewModel, Transaction>(model); var customerAccount = _repository.GetBankAccountById(model.SenderAccId); newDeposite.Amount = model.Amount * depositePercent; newDeposite.Type = TransactionType.Deposite; newDeposite.Status = TransactionStatus.Completed; newDeposite.SenderAcc = customerAccount; newDeposite.RecieverAcc = customerAccount; newDeposite.TransferDate = DateTime.UtcNow; customerAccount.Balance = Math.Round(customerAccount.Balance + newDeposite.Amount, 2); _repository.AddEntity(newDeposite); _repository.UpdateEntity(customerAccount); try { if (_repository.SaveAll()) { ViewBag.Msg = "Deposite Success!"; model.Balance = customerAccount.Balance; } } catch (DbUpdateConcurrencyException ex) { var exceptionEntry = ex.Entries.Single(); var databaseEntry = exceptionEntry.GetDatabaseValues(); if (databaseEntry == null) { ModelState.AddModelError(string.Empty, "Unable to save changes. The balance was updated by another user."); } } } else { ModelState.AddModelError(String.Empty, "Amount should > 0"); } } ViewBag.Accounts = _repository.GetAllBankAccountOpened(); return(View(model)); }
public IActionResult CreateAccount(BankAccountViewModel model) { var allusers = _repository.GetAllBankUser(); ViewBag.BankUsers = allusers; if (ModelState.IsValid) { if (!_ibanService.Validate(model.IBAN)) { ModelState.AddModelError("", "This IBAN is invalid!"); } else if (!_repository.IsIBANNumberExist(model.IBAN)) { var newAccount = _mapper.Map <BankAccountViewModel, BankAccount>(model); newAccount.User = _repository.GetBankUserById(model.UserId); newAccount.Status = AccountStatus.Open; _repository.AddEntity(newAccount); if (_repository.SaveAll()) { ViewBag.Msg = "Create Success!"; } } else { ModelState.AddModelError("", "This IBAN is exist!"); } } return(View(model)); }