public async Task <BaseResponse <UserResponse> > Add(UserRequest userRequest) { var response = new BaseResponse <UserResponse>(); var results = _validator.Validate(userRequest); if (!results.IsValid) { results.Errors.ToList().ForEach(x => { response.AddErrors(x.ErrorMessage, HttpStatusCode.BadRequest); }); return(response); } var user = _mapper.Map <Model.Entities.User>(userRequest); await _context.Users.AddAsync(user); var save = await SaveChanges(); if (!save.Item1) { response.AddErrors(save.Item2, HttpStatusCode.InternalServerError); return(response); } var userResponse = _mapper.Map <UserResponse>(user); response.AddItems(new[] { userResponse }); return(response); }
public async Task <BaseResponse <UserResponse> > UpdateByHashId(string hashId, UserRequest userRequest) { var response = new BaseResponse <UserResponse>(); var user = _context.Users.FirstOrDefault(x => x.HashId == hashId); if (user == null) { response.AddItems(null); return(response); } var results = _validator.Validate(userRequest); if (!results.IsValid) { results.Errors.ToList().ForEach(x => { response.AddErrors(x.ErrorMessage, HttpStatusCode.BadRequest); }); return(response); } user.Name = userRequest.Name; user.Age = userRequest.Age; user.Address = userRequest.Address; user.UpdateDate = DateTime.Now; var save = await SaveChanges(); if (!save.Item1) { response.AddErrors(save.Item2, HttpStatusCode.InternalServerError); return(response); } var userResponse = _mapper.Map <UserResponse>(user); response.AddItems(new[] { userResponse }); return(response); }
public BaseResponse <TransferResponse> Transfer(string userId, TransferRequest transferRequest) { var response = new BaseResponse <TransferResponse>(); try { transferRequest.UserId = userId; TransferValidator transferValidator = new TransferValidator(_checkingAccountService); FluentValidation.Results.ValidationResult validationResult = transferValidator.Validate(transferRequest); if (!validationResult.IsValid) { response.AddErrors(validationResult.Errors); return(response); } var from = _checkingAccountService.UpdateBalance(transferRequest.NumberFrom, transferRequest.Amount * -1); var to = _checkingAccountService.UpdateBalance(transferRequest.NumberTo, transferRequest.Amount); _transactionService.Add(new Transaction { Amount = transferRequest.Amount, FromCheckingAccountId = from.Id, ToCheckingAccountId = to.Id }); _checkingAccountService.Commit(); response.Data = new TransferResponse { Balance = from.Balance }; } catch (Exception ex) { _checkingAccountService.Rollback(); response.AddError(1, "Erro inesperado"); } return(response); }
public static BaseResponse <TResponse, TError> AddErrors <TResponse, TError>(this BaseResponse <TResponse, TError> baseResponse, IList <ValidationFailure> errors) where TError : ErrorResponse { return(baseResponse.AddErrors(errors.ToErrorResponse <TError>())); }
public static BaseResponse <TResponse> AddErrors <TResponse>(this BaseResponse <TResponse> baseResponse, IEnumerable <Event> errors) { return(baseResponse.AddErrors(errors.ToErrorResponse())); }
public static BaseResponse <TResponse> AddErrors <TResponse>(this BaseResponse <TResponse> baseResponse, IList <ValidationFailure> errors) { return(baseResponse.AddErrors(errors.ToErrorResponse())); }