public IActionResult Get(long accountNumber) { return(Ok(_checkingAccountApplication.GetByAccountNumber(accountNumber))); }
public TransferValidator(ICheckingAccountApplication checkingAccountApplication) { RuleFor(transfer => transfer.NumberFrom) .Cascade(CascadeMode.StopOnFirstFailure) .NotEmpty() .NotNull() .Must(number => number > 0) .WithMessage("Número de conta origem inválido"); RuleFor(transfer => transfer.NumberTo) .Cascade(CascadeMode.StopOnFirstFailure) .NotEmpty() .NotNull() .Must(number => number > 0) .WithMessage("Número de conta destino inválido"); RuleFor(transfer => transfer.NumberFrom) .Cascade(CascadeMode.StopOnFirstFailure) .NotEqual(t => t.NumberTo) .WithMessage("Conta origem e conta destino não podem ser iguais"); RuleFor(transfer => transfer.Amount) .Cascade(CascadeMode.StopOnFirstFailure) .NotEmpty() .NotNull() .Must(number => number > 0) .WithMessage("Valor de transferência não pode ser negativo."); RuleFor(transfer => transfer) .Cascade(CascadeMode.StopOnFirstFailure) .Custom((transfer, context) => { var checkingAccountFrom = checkingAccountApplication.GetByAccountNumber(transfer.NumberFrom); if (checkingAccountFrom == null) { context.AddFailure($"Não foi encontrado a conta corrente (ORIGEM) {transfer.NumberFrom}."); } else if (checkingAccountFrom.Balance < transfer.Amount) { context.AddFailure($"Saldo da conta corrente (ORIGEM) {transfer.NumberFrom}, insuficiente."); } if (checkingAccountFrom.UserId.ToString() != transfer.UserId) { context.AddFailure("Conta origem não pertence ao usuário informado"); } }); RuleFor(transfer => transfer) .Cascade(CascadeMode.StopOnFirstFailure) .Custom((transfer, context) => { var checkingAccountFrom = checkingAccountApplication.GetByAccountNumber(transfer.NumberTo); if (checkingAccountFrom == null) { context.AddFailure($"Não foi encontrado a conta corrente (DESTINO) {transfer.NumberTo}."); } }); }
public BaseResponse <TransferResponse> Transfer(string userId, TransferRequest transferRequest) { var response = new BaseResponse <TransferResponse>(); try { transferRequest.UserId = userId; var checkingAccountFrom = _checkingAccountApplication.GetByAccountNumber(transferRequest.NumberFrom); var checkingAccountTo = _checkingAccountApplication.GetByAccountNumber(transferRequest.NumberTo); TransferValidator transferValidator = new TransferValidator(_checkingAccountApplication); FluentValidation.Results.ValidationResult validationResult = transferValidator.Validate(transferRequest); if (!validationResult.IsValid) { List <ErrorResponse> erros = new List <ErrorResponse>(); foreach (var item in validationResult.Errors) { ErrorResponse errorResponse = new ErrorResponse { Message = item.ErrorMessage, Code = item.GetHashCode() }; erros.Add(errorResponse); } response.Errors.AddRange(erros); return(response); } decimal balanceFrom = checkingAccountFrom.Balance - transferRequest.Amount; decimal balanceTo = checkingAccountTo.Balance + transferRequest.Amount; var from = _checkingAccountApplication.UpdateBalance(transferRequest.NumberFrom, balanceFrom); var to = _checkingAccountApplication.UpdateBalance(transferRequest.NumberTo, balanceTo); if (from != null && to != null) { _transactionService.Add(new Transaction { Amount = transferRequest.Amount, FromCheckingAccountId = checkingAccountFrom.UserId, ToCheckingAccountId = checkingAccountTo.UserId }); response.Result = new TransferResponse { Balance = balanceFrom }; } } catch (Exception ex) { response.Errors.Add(new ErrorResponse { Code = ex.GetHashCode(), Message = ex.Message }); } return(response); }