Handle(Queries.GetLogsQuery request, System.Threading.CancellationToken cancellationToken) { var result = new FluentResults.Result <System.Collections.Generic.IEnumerable <Persistence.ViewModels.GetLogsQueryResponseViewModel> >(); try { // ************************************************** var logs = await UnitOfWork.Logs .GetSomeAsync(count : request.Count.Value) ; // ************************************************** // ************************************************** result.WithValue(value: logs); // ************************************************** } catch (System.Exception ex) { Logger.LogError (exception: ex, message: ex.Message); result.WithError (errorMessage: ex.Message); } return(result); }
System.Threading.Tasks.Task HandleExceptionAsync (Microsoft.AspNetCore.Http.HttpContext context, System.Exception exception) { FluentResults.Result result = new FluentResults.Result(); FluentValidation.ValidationException validationException = exception as FluentValidation.ValidationException; if (validationException != null) { var code = System.Net.HttpStatusCode.BadRequest; context.Response.StatusCode = (int)code; context.Response.ContentType = "application/json"; foreach (var error in validationException.Errors) { result.WithError(error.ErrorMessage); } } else { // Log Error! var code = System.Net.HttpStatusCode.InternalServerError; context.Response.StatusCode = (int)code; context.Response.ContentType = "application/json"; result.WithError("Internal Server Error!"); } var options = new System.Text.Json.JsonSerializerOptions { IncludeFields = true, PropertyNamingPolicy = System.Text.Json.JsonNamingPolicy.CamelCase, }; string resultString = System.Text.Json.JsonSerializer.Serialize(value: result, options: options); return(context.Response.WriteAsync(resultString)); }
Validate <TCommand, TValue> (FluentValidation.AbstractValidator <TCommand> validator, TCommand command) { FluentResults.Result <TValue> result = new FluentResults.Result <TValue>(); FluentValidation.Results.ValidationResult validationResult = await validator.ValidateAsync(instance : command); if (validationResult.IsValid == false) { foreach (var error in validationResult.Errors) { result.WithError(errorMessage: error.ErrorMessage); } } return(result); }
Handle(Commands.CreateLogCommand request, System.Threading.CancellationToken cancellationToken) { var result = new FluentResults.Result <System.Guid>(); try { // ************************************************** var log = Mapper.Map <Domain.Models.Log>(source: request); // ************************************************** // ************************************************** await UnitOfWork.Logs.InsertAsync(entity : log); await UnitOfWork.SaveAsync(); // ************************************************** // ************************************************** result.WithValue(value: log.Id); string successInsert = string.Format(Resources.Messages.SuccessInsert, nameof(Domain.Models.Log)); result.WithSuccess (successMessage: successInsert); // ************************************************** } catch (System.Exception ex) { Logger.LogError (exception: ex, message: ex.Message); result.WithError (errorMessage: ex.Message); } return(result); }