public IActionResult CreateTransaction(TransactionViewModel transaction)
 {
     if (ModelState.IsValid)
     {
         transaction.Result = _handler.Handle(transaction);
     }
     return(View("Index", transaction));
 }
Exemplo n.º 2
0
        public async Task UpdatesAccountBalance()
        {
            const decimal initialAccountBalance       = 10.0m;
            const decimal transactionAmount           = 5.0m;
            const decimal expectedAccountFinalBalance = initialAccountBalance + transactionAmount;
            var           account = new Account
            {
                AccountType = new AccountType
                {
                    Id   = 1,
                    Name = "account-type-test"
                },
                AccountTypeId = 1,
                Balance       = 10.0m,
                Id            = 1,
                Name          = "account-test",
                Status        = true
            };

            _dataContext.Accounts.Add(account);
            await _dataContext.SaveChangesAsync();

            var transaction = new Transaction
            {
                Account   = account,
                AccountId = 1,
                Amount    = 5.0m,
                Category  = new Category
                {
                    CostType = new CostType
                    {
                        Id   = 1,
                        Name = "cost-type-test"
                    },
                    CostTypeId = 1,
                    Id         = 1,
                    Name       = "category-test"
                },
                CategoryId  = 1,
                Description = "description-test",
                Id          = 1,
                IsBudget    = false,
                TimeStamp   = DateTime.Now
            };

            _dataContext.Transactions.Add(transaction);
            await _dataContext.SaveChangesAsync();

            await _transactionHandler.Handle(transaction);

            account.Balance.Should().Be(expectedAccountFinalBalance);
        }
        public async Task <IActionResult> Put([FromBody] Transaction newTransaction)
        {
            if (newTransaction == null ||
                newTransaction.AccountId == 0 ||
                newTransaction.CategoryId == 0)
            {
                return(BadRequest());
            }

            newTransaction.Account = await _dataContext.Accounts.FindAsync(newTransaction.AccountId);

            newTransaction.Category = await _dataContext.Categories.FindAsync(newTransaction.CategoryId);

            if (newTransaction.Account == null ||
                newTransaction.Category == null)
            {
                return(NotFound("Account not found."));
            }

            var transactionCreated = await _dataContext.Transactions.AddAsync(newTransaction);

            try
            {
                await _dataContext.SaveChangesAsync();

                await _transactionHandler.Handle(transactionCreated.Entity);

                return(Ok());
            }
            catch (DbUpdateException)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError));
            }
            catch (DBConcurrencyException)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError));
            }
            catch (KeyNotFoundException)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError));
            }
        }