Exemplo n.º 1
0
        public async Task <IActionResult> DepositAndWidthdraw(Guid userId, Guid accountId, [FromBody] UpdateAccountDto model, bool deposit = true)
        {
            if (!await _accountRepo.AccountExists(userId, accountId))  //check account exist
            {
                return(BadRequest("Account does not exits"));
            }

            lock (_lockAccount) //user lock or RabbitMQ
            {
                var accountFromDb  = _accountRepo.GetEntities(x => true).SingleOrDefaultAsync(x => x.Id == accountId).Result;
                var editAccountDto = _mapper.Map <EditAccountDto>(accountFromDb);
                if (deposit)
                {
                    editAccountDto.CurrentBalance += model.CurrentBalance;
                }
                else
                {
                    editAccountDto.CurrentBalance -= model.CurrentBalance;
                }

                if (editAccountDto.CurrentBalance > 0)
                {
                    var result = _accountRepo.EditByDTOAsync(editAccountDto).GetAwaiter().GetResult();
                    if (result)
                    {
                        _logger.LogInformation($"Deposit Success");
                        return(Ok("Update success"));
                    }
                }

                return(BadRequest($"Cannot Withdraw or deposit"));
            }
        }