Example #1
0
        public async Task <IActionResult> Add([FromBody] FundInputModel model)
        {
            var result = await this.funds.Add(model, this.currentUser.Id);

            if (!result.Succeeded)
            {
                return(this.BadRequest(result.Errors));
            }

            return(this.Ok());
        }
Example #2
0
        public async Task <Result> Add(FundInputModel model, string userId)
        {
            var client = await this.dbContext.Clients.FirstOrDefaultAsync(c => c.UserId == userId);

            if (client == null)
            {
                return(Result.Failure(Messages.YouAreNotAClient));
            }

            var account = await this.dbContext.ExchangeAccounts.FirstOrDefaultAsync(ea => ea.Id == model.AccountId && ea.OwnerId == client.Id);

            if (account == null)
            {
                return(Result.Failure(Messages.AccountNotFound));
            }

            if (!account.IsActive)
            {
                return(Result.Failure(Messages.ReceiverAccountIsNotActive));
            }

            var fund = new Fund()
            {
                AccountId          = account.Id,
                Amount             = model.Amount,
                CardIdentityNumber = model.CardIdentityNumber,
                ClientId           = client.Id
            };

            account.Balance += model.Amount;

            this.dbContext.Funds.Add(fund);

            await this.dbContext.SaveChangesAsync();

            return(Result.Success);
        }