public async Task <IActionResult> Deposit([Bind("Amount")] DepositViewModel deposit) { if (ModelState.IsValid) { var account = await this.GetAccount(); _context.Add(new TransactionInfo { Id = Guid.NewGuid(), TransactionType = Enums.TransactionType.Credit, Amount = deposit.Amount, TransactionDate = DateTime.Now, UserAccountId = account.Id }); var result = await _context.SaveChangesAsync(); return(RedirectToAction("Index", "Home")); } ViewData["UserAccount"] = await this.GetAccount(); return(View(deposit)); }
public async Task <IActionResult> Register([Bind("AccountNumber,AccountName,Password,InitialBalance")] RegisterAccountViewModel userAccount) { if (User.Identity.IsAuthenticated) { return(RedirectToAction("Index", "Home")); } if (ModelState.IsValid) { var userAccounts = await _context.UserAccounts.Where(u => u.AccountNumber == userAccount.AccountNumber).ToListAsync(); if (userAccounts.Count > 0) { ModelState.AddModelError("Error", "The account number specified already exists."); } else { BankingIdentityUser user = new BankingIdentityUser { UserName = userAccount.AccountNumber, Email = "account_" + userAccount.AccountNumber + "@banking.io", FullName = userAccount.AccountName }; IdentityResult result = _userManager.CreateAsync(user, userAccount.Password).Result; if (result.Succeeded) { if (!_roleManager.RoleExistsAsync("NormalUser").Result) { BankingIdentityRole role = new BankingIdentityRole(); role.Name = "NormalUser"; role.Description = "Perform normal operations."; IdentityResult roleResult = _roleManager.CreateAsync(role).Result; if (!roleResult.Succeeded) { ModelState.AddModelError("", "Error while creating role!"); return(View(userAccount)); } } _userManager.AddToRoleAsync(user, "NormalUser").Wait(); var id = Guid.NewGuid(); var registeredAccount = new UserAccount { Id = id, AccountNumber = userAccount.AccountNumber, CreateDate = DateTime.Now }; _context.Add(registeredAccount); _context.Add(new TransactionInfo { Id = Guid.NewGuid(), TransactionType = Enums.TransactionType.Credit, TransactionDate = DateTime.Now, UserAccountId = id, Amount = userAccount.InitialBalance }); await _context.SaveChangesAsync(); return(RedirectToAction("Login", "Account")); } else { ModelState.AddModelError("Error", "Unable to create the user. " + result.Errors.FirstOrDefault().Description); } } } return(View(userAccount)); }