Esempio n. 1
0
        public async Task <ActionResult> Index(TransactionViewModel model)
        {
            var creditorId = User.FindFirstValue(ClaimTypes.NameIdentifier);
            var userName   = User.FindFirstValue(ClaimTypes.Name);

            var userAccount = _userAccountService.GetUserAccount(creditorId);

            ViewBag.UserAccount = userAccount;

            var userTransactions = _transactionService.GetAccountTransactions(userAccount.Id);

            var tableData =
                PaginatedList <TransactionHistoryViewModel> .CreateAsync(userTransactions, 1, 5);

            ViewBag.History = tableData;
            if (userAccount.User.Email == model.CreditorEmail.Trim())
            {
                ModelState.AddModelError("Receiver", "Сan't borrow money for yourself!");
            }

            if (!_userAccountService.CanMakeTransaction(creditorId, model.Amount))
            {
                ModelState.AddModelError("Amount", "Insufficient funds in the account!");
            }

            try
            {
                _transactionService.CreateTransaction(model, creditorId);
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("DB", $"Something went wrong during transaction saving: {ex.Message}");
            }

            if (!ModelState.IsValid)
            {
                return(View());
            }

            _transactionService.CreateTransaction(model, creditorId);

            return(View());
        }