Example #1
0
        public void HandleValidAccount()
        {
            var account = new Account()
            {
                AccountNumber = "account_test",
                Balance       = 0,
                Id            = 1
            };

            Assert.NotNull(_accountsHandler.Handle(account));
        }
Example #2
0
        public async Task <ActionResult <Account> > Post(Account entity)
        {
            //Check for not null entity.
            if (entity == null)
            {
                return(BadRequest());
            }

            //Validate entity via handler.
            if (_accountsHandler.Handle(entity) == null)
            {
                return(BadRequest());
            }

            //Check if account already exists.
            var account = _accountRepository.Data.FirstOrDefaultAsync
                              (doc => doc.AccountNumber == entity.AccountNumber);

            if (account.Result != null)
            {
                return(CreatedAtAction(nameof(GetById), new { id = account.Result.Id }, account.Result));
            }

            //Add to database.
            _accountRepository.Data.Add(entity);
            await _accountRepository.SaveChangesAsync();

            //Return created account.
            return(CreatedAtAction(nameof(GetById), new { id = entity.Id }, entity));
        }