namespace Prefab.Web.Client.Models.Shared; using System.Collections.Generic; using System.Linq; using System.Net; using FluentValidation; public sealed class ResultProblem { public string? Title { get; init; } public string? Detail { get; init; } public int? StatusCode { get; init; } public IReadOnlyDictionary? Errors { get; init; } public static ResultProblem FromValidation(ValidationException exception) { var errors = exception.Errors .GroupBy(error => string.IsNullOrWhiteSpace(error.PropertyName) ? string.Empty : error.PropertyName) .ToDictionary( group => group.Key, group => group.Select(error => error.ErrorMessage ?? string.Empty).ToArray()); return new ResultProblem { Title = "Validation failed.", Detail = "Request did not satisfy validation rules.", StatusCode = (int)HttpStatusCode.BadRequest, Errors = errors }; } public static ResultProblem Unexpected(string title, Exception exception, HttpStatusCode statusCode = HttpStatusCode.InternalServerError) => new() { Title = title, Detail = exception.Message, StatusCode = (int)statusCode }; } /// /// Represents the outcome of an operation that produces a value of type . /// /// The payload type returned when the operation succeeds. public sealed class Result { public Result() { } public bool IsSuccess => Problem is null; public ResultProblem? Problem { get; init; } public int? StatusCode => Problem?.StatusCode; public T? Value { get; init; } public static Result Success(T value) => new() { Value = value ?? throw new ArgumentNullException(nameof(value)) }; public static Result Failure(ResultProblem problem) => new() { Problem = problem ?? throw new ArgumentNullException(nameof(problem)) }; }