Change editor rule
This commit is contained in:
@@ -1,43 +1,44 @@
|
||||
using ErrorOr;
|
||||
using IResult = Microsoft.AspNetCore.Http.IResult;
|
||||
|
||||
namespace Musify.Api.Extensions;
|
||||
|
||||
public static class ErrorOrHttpExtensions
|
||||
namespace Musify.Api.Extensions
|
||||
{
|
||||
public static IResult ToHttpResult<T>(this ErrorOr<T> result)
|
||||
public static class ErrorOrHttpExtensions
|
||||
{
|
||||
if (result.IsError)
|
||||
return Problem(result.Errors);
|
||||
|
||||
return result.Value is Success
|
||||
? Results.NoContent()
|
||||
: Results.Ok(result.Value);
|
||||
}
|
||||
|
||||
public static IResult ToCreatedResult<T>(this ErrorOr<T> result, Func<T, string> locationFactory) =>
|
||||
result.IsError
|
||||
? Problem(result.Errors)
|
||||
: Results.Created(locationFactory(result.Value), result.Value);
|
||||
|
||||
private static IResult Problem(List<Error> errors)
|
||||
{
|
||||
if (errors.All(e => e.Type == ErrorType.Validation))
|
||||
return Results.ValidationProblem(ToValidationDictionary(errors));
|
||||
|
||||
var error = errors[0];
|
||||
return error.Type switch
|
||||
public static IResult ToHttpResult<T>(this ErrorOr<T> result)
|
||||
{
|
||||
ErrorType.NotFound => Results.Problem(error.Description, statusCode: StatusCodes.Status404NotFound),
|
||||
ErrorType.Unauthorized => Results.Unauthorized(),
|
||||
ErrorType.Forbidden => Results.Problem(error.Description, statusCode: StatusCodes.Status403Forbidden),
|
||||
ErrorType.Conflict => Results.Problem(error.Description, statusCode: StatusCodes.Status409Conflict),
|
||||
_ => Results.Problem(error.Description)
|
||||
};
|
||||
}
|
||||
if (result.IsError)
|
||||
return Problem(result.Errors);
|
||||
|
||||
private static Dictionary<string, string[]> ToValidationDictionary(IEnumerable<Error> errors) =>
|
||||
errors
|
||||
.GroupBy(e => string.IsNullOrEmpty(e.Code) ? "general" : e.Code)
|
||||
.ToDictionary(g => g.Key, g => g.Select(e => e.Description).ToArray());
|
||||
return result.Value is Success
|
||||
? Results.NoContent()
|
||||
: Results.Ok(result.Value);
|
||||
}
|
||||
|
||||
public static IResult ToCreatedResult<T>(this ErrorOr<T> result, Func<T, string> locationFactory) =>
|
||||
result.IsError
|
||||
? Problem(result.Errors)
|
||||
: Results.Created(locationFactory(result.Value), result.Value);
|
||||
|
||||
private static IResult Problem(List<Error> errors)
|
||||
{
|
||||
if (errors.All(e => e.Type == ErrorType.Validation))
|
||||
return Results.ValidationProblem(ToValidationDictionary(errors));
|
||||
|
||||
var error = errors[0];
|
||||
return error.Type switch
|
||||
{
|
||||
ErrorType.NotFound => Results.Problem(error.Description, statusCode: StatusCodes.Status404NotFound),
|
||||
ErrorType.Unauthorized => Results.Unauthorized(),
|
||||
ErrorType.Forbidden => Results.Problem(error.Description, statusCode: StatusCodes.Status403Forbidden),
|
||||
ErrorType.Conflict => Results.Problem(error.Description, statusCode: StatusCodes.Status409Conflict),
|
||||
_ => Results.Problem(error.Description)
|
||||
};
|
||||
}
|
||||
|
||||
private static Dictionary<string, string[]> ToValidationDictionary(IEnumerable<Error> errors) =>
|
||||
errors
|
||||
.GroupBy(e => string.IsNullOrEmpty(e.Code) ? "general" : e.Code)
|
||||
.ToDictionary(g => g.Key, g => g.Select(e => e.Description).ToArray());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,22 +8,23 @@ using Musify.Infrastructure.MassTransit;
|
||||
using Musify.Infrastructure.Persistence;
|
||||
using Musify.Infrastructure.Services;
|
||||
|
||||
namespace Musify.Api.Extensions;
|
||||
|
||||
public static class ServiceCollectionExtensions
|
||||
namespace Musify.Api.Extensions
|
||||
{
|
||||
public static IServiceCollection AddApplicationServices(this IServiceCollection services, IConfiguration configuration)
|
||||
public static class ServiceCollectionExtensions
|
||||
{
|
||||
services.AddApplication(configuration);
|
||||
services.AddDatabase(configuration);
|
||||
services.AddStorageService(configuration);
|
||||
services.AddStreamTicketService(configuration);
|
||||
services.AddMassTransitClient(configuration);
|
||||
services.AddAuthenticationConfiguration(configuration);
|
||||
services.AddOpenApiConfiguration();
|
||||
services.AddScalarConfiguration();
|
||||
services.AddValidatorsFromAssemblyContaining<CreatePlayListRequest>();
|
||||
public static IServiceCollection AddApplicationServices(this IServiceCollection services, IConfiguration configuration)
|
||||
{
|
||||
services.AddApplication(configuration);
|
||||
services.AddDatabase(configuration);
|
||||
services.AddStorageService(configuration);
|
||||
services.AddStreamTicketService(configuration);
|
||||
services.AddMassTransitClient(configuration);
|
||||
services.AddAuthenticationConfiguration(configuration);
|
||||
services.AddOpenApiConfiguration();
|
||||
services.AddScalarConfiguration();
|
||||
services.AddValidatorsFromAssemblyContaining<CreatePlayListRequest>();
|
||||
|
||||
return services;
|
||||
return services;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user