public async Task <Result <bool> > ValidateDuplicateAccountNumberAsync(ContaRequest contaRequest) { if (contaRequest == null) { return(new Result <bool>(false, HttpStatusCode.BadRequest, Failure.GenerateOneFailure("Object is null."))); } var account = await _contaService.GetByFilterAsync <ContaResponse>(x => x.Numero == contaRequest.Numero && x.Id != contaRequest.Id); if (account.Value?.Count > 0) { return(new Result <bool>(false, HttpStatusCode.BadRequest, Failure.GenerateOneFailure("The account number is already in use."))); } return(new Result <bool>(true, HttpStatusCode.OK, null)); }
public Task <Result <string> > GenerateAccountNumberAsync() { try { string accountNumber = string.Empty; for (int i = 0; i < 8; i++) { accountNumber += new Random().Next(0, 9).ToString(); } accountNumber += "-"; accountNumber += new Random().Next(0, 9).ToString(); return(Task.FromResult(new Result <string>(accountNumber, HttpStatusCode.OK, null))); } catch (Exception ex) { return(Task.FromResult(new Result <string>(null, HttpStatusCode.OK, Failure.GenerateOneFailure(ex.Message)))); } }
public async Task <Result <string> > PerformOperationAsync(LancamentoRequest lancamentoRequest, CancellationToken cancellationToken) { if (lancamentoRequest == null) { return(new Result <string>(null, HttpStatusCode.BadRequest, Failure.GenerateOneFailure("Object is null."))); } Result <decimal> result; switch (lancamentoRequest.EOperacao) { case EOperacao.Deposito: { result = await _lancamentoService.DepositAsync(lancamentoRequest); break; } case EOperacao.Saque: { result = await _lancamentoService.WithdrawAsync(lancamentoRequest); break; } case EOperacao.Transferencia: { result = await _lancamentoService.TransferAsync(lancamentoRequest); break; } default: return(new Result <string>(null, HttpStatusCode.BadRequest, Failure.GenerateOneFailure("Operation kind not supported."))); } if (!result.StatusCode.IsSuccessStatusCode()) { return(new Result <string>(null, result.StatusCode, result.Failures)); } return (await _lancamentoService.InsertOneAsync(lancamentoRequest, cancellationToken)); }
public async Task <Result <decimal> > WithdrawAsync(LancamentoRequest lancamentoRequest) { try { var validation = await ValidateOperation(lancamentoRequest); if (!validation.StatusCode.IsSuccessStatusCode()) { return(validation); } var sourceAccount = await _contaService.GetByIdAsync <ContaResponse>(lancamentoRequest.IdContaOrigem); var sourceAccountRequest = _mapper.Map <ContaResponse, ContaRequest>(sourceAccount.Value); lancamentoRequest.ContaOrigem = sourceAccountRequest; if (!sourceAccount.StatusCode.IsSuccessStatusCode()) { return(new Result <decimal>(0, sourceAccount.StatusCode, sourceAccount.Failures)); } var account = base._mapper.Map <ContaResponse, Conta>(sourceAccount.Value); var newBalance = await account.DecreaseBalance(lancamentoRequest.Valor); if (!newBalance.StatusCode.IsSuccessStatusCode()) { return(new Result <decimal>(0, newBalance.StatusCode, newBalance.Failures)); } var updateBalance = await _contaService.UpdateBalanceAsync(account.Id, newBalance.Value); if (!updateBalance.StatusCode.IsSuccessStatusCode()) { return(new Result <decimal>(0, updateBalance.StatusCode, updateBalance.Failures)); } return(new Result <decimal>(newBalance.Value, HttpStatusCode.OK, null)); } catch (Exception ex) { return(new Result <decimal>(0, HttpStatusCode.InternalServerError, Failure.GenerateOneFailure(ex.Message))); } }
public async Task <Result <bool> > UpdateAccountAsync(ContaRequest conta) { if (conta == null) { return(new Result <bool>(false, HttpStatusCode.BadRequest, Failure.GenerateOneFailure("Object is null."))); } var objConta = _mapper.Map <ContaRequest, Conta>(conta); if (!objConta.IsValid(EValidationStage.Replace)) { return(new Result <bool>(false, HttpStatusCode.BadRequest, objConta.ValidationErrors)); } var result = await _contaRepository.UpdateAccountAsync(objConta); if (!result) { return(new Result <bool>(false, HttpStatusCode.NotFound, Failure.GenerateOneFailure("Cannot update account."))); } return(new Result <bool>(true, HttpStatusCode.OK, null)); }
public async Task <ActionResult <dynamic> > Post([FromBody] UserRequest userRequest) { if (userRequest.User != "admin" || userRequest.Password != "admin") { return(StatusCode((int)HttpStatusCode.BadRequest, new Result <string>(null, HttpStatusCode.BadRequest, Failure.GenerateOneFailure("User or password invalid.")))); } string token = await _tokenService.GenerateToken(userRequest.User); var userResponse = new UserResponse { User = userRequest.User, Token = token }; return(new Result <UserResponse>(userResponse, HttpStatusCode.OK, null)); }
public async Task <Result <TResponse> > GetByIdAsync <TResponse>(string id) where TResponse : BaseResponse { try { var obj = await this._repositoryBase.GetByIdAsync(id); if (obj == null) { return(new Result <TResponse>(default(TResponse), HttpStatusCode.NotFound, Failure.GenerateOneFailure($"{typeof(T).Name} not found."))); } TResponse objRequest = this._mapper.Map <T, TResponse>(obj); var objResult = new Result <TResponse>(objRequest, HttpStatusCode.OK, null); return(objResult); } catch (Exception ex) { return(new Result <TResponse>(default, HttpStatusCode.InternalServerError, Failure.GenerateOneFailure(ex.Message)));
public async Task <Result <bool> > UpdateBalanceAsync(string id, decimal valor) { var updateBalanceResult = await _contaRepository.UpdateBalanceAsync(id, valor); if (!updateBalanceResult) { return(new Result <bool>(false, System.Net.HttpStatusCode.InternalServerError, Failure.GenerateOneFailure("Cannot update balance."))); } return(new Result <bool>(true, System.Net.HttpStatusCode.OK, null)); }
public async Task <Result <decimal> > TransferAsync(LancamentoRequest lancamentoRequest) { try { #region Validate the operation var validation = await ValidateOperation(lancamentoRequest); if (!validation.StatusCode.IsSuccessStatusCode()) { return(validation); } #endregion #region Map the account object, so we can track the operation var sourceAccountResponse = await _contaService.GetByIdAsync <ContaResponse>(lancamentoRequest.IdContaOrigem); var targetAccountResponse = await _contaService.GetByIdAsync <ContaResponse>(lancamentoRequest.IdContaDestino); var sourceAccountRequest = _mapper.Map <ContaResponse, ContaRequest>(sourceAccountResponse.Value); var targetAccountRequest = _mapper.Map <ContaResponse, ContaRequest>(targetAccountResponse.Value); lancamentoRequest.ContaOrigem = sourceAccountRequest; lancamentoRequest.ContaDestino = targetAccountRequest; if (!sourceAccountResponse.StatusCode.IsSuccessStatusCode()) { return(new Result <decimal>(0, sourceAccountResponse.StatusCode, sourceAccountResponse.Failures)); } if (!targetAccountResponse.StatusCode.IsSuccessStatusCode()) { return(new Result <decimal>(0, targetAccountResponse.StatusCode, targetAccountResponse.Failures)); } var sourceAccount = base._mapper.Map <ContaResponse, Conta>(sourceAccountResponse.Value); var targetAccount = base._mapper.Map <ContaResponse, Conta>(targetAccountResponse.Value); #endregion #region Decrease Balance var newBalanceSourceAccount = await sourceAccount.DecreaseBalance(lancamentoRequest.Valor); if (!newBalanceSourceAccount.StatusCode.IsSuccessStatusCode()) { return(new Result <decimal>(0, newBalanceSourceAccount.StatusCode, newBalanceSourceAccount.Failures)); } #endregion #region Increase Balance var newBalanceTargetAccount = await targetAccount.IncreaseBalance(lancamentoRequest.Valor); if (!newBalanceTargetAccount.StatusCode.IsSuccessStatusCode()) { return(new Result <decimal>(0, newBalanceTargetAccount.StatusCode, newBalanceTargetAccount.Failures)); } #endregion #region Update Balance var updateBalance = await _contaService.UpdateBalanceAsync(sourceAccount.Id, newBalanceSourceAccount.Value); if (!updateBalance.StatusCode.IsSuccessStatusCode()) { return(new Result <decimal>(0, updateBalance.StatusCode, updateBalance.Failures)); } updateBalance = await _contaService.UpdateBalanceAsync(targetAccount.Id, newBalanceTargetAccount.Value); if (!updateBalance.StatusCode.IsSuccessStatusCode()) { return(new Result <decimal>(0, updateBalance.StatusCode, updateBalance.Failures)); } #endregion return(new Result <decimal>(newBalanceSourceAccount.Value, HttpStatusCode.OK, null)); } catch (Exception ex) { return(new Result <decimal>(0, HttpStatusCode.InternalServerError, Failure.GenerateOneFailure(ex.Message))); } }