public async Task <IActionResult> Create([Bind("InterestRate,Comment,Balance")] BankAccount bankAccount) { bankAccount.CreationTime = DateTime.Now.ToString(new CultureInfo("se-SE")); bankAccount.OwnerId = new Guid(_userManager.GetUserId(User)); if (ModelState.IsValid) { bankAccount.Id = Guid.NewGuid(); _context.Add(bankAccount); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(bankAccount)); }
public async Task <IActionResult> Create([Bind("BankAccountId,Comment,Deposition,Amount")] TransactionViewModel transactionViewModel) { if (ModelState.IsValid) { var transaction = new Transaction { Id = Guid.NewGuid(), BankAccountId = transactionViewModel.BankAccountId, Comment = transactionViewModel.Comment, CreationTime = DateTime.Now.ToString(new CultureInfo("se-SE")), Deposit = transactionViewModel.Deposition ? transactionViewModel.Amount : "", Withdraw = !transactionViewModel.Deposition ? transactionViewModel.Amount : "" }; var bankAccount = await _context.BankAccount.SingleOrDefaultAsync(m => m.Id == transactionViewModel.BankAccountId); decimal oldBalance = decimal.Parse(bankAccount.Balance, new CultureInfo("se-SV")); if (transactionViewModel.Deposition) { decimal deposit = decimal.Parse(transactionViewModel.Amount, new CultureInfo("se-SV")); bankAccount.Balance = (oldBalance + deposit).ToString(new CultureInfo("se-SV")); } else { decimal withdraw = decimal.Parse(transactionViewModel.Amount, new CultureInfo("se-SV")); bankAccount.Balance = (oldBalance - withdraw).ToString(new CultureInfo("se-SV")); if (decimal.Parse(bankAccount.Balance, new CultureInfo("se-SV")) < 0) { TempData["CustomError"] = "Saldot får ej bli negativt!"; return(RedirectToAction("Create", new { id = transactionViewModel.BankAccountId })); } } _context.Add(transaction); await _context.SaveChangesAsync(); return(RedirectToAction("Index", new { id = transactionViewModel.BankAccountId })); } return(View(transactionViewModel)); }