mirror of
https://github.com/collinbarrett/FilterLists.git
synced 2026-03-11 09:04:27 +00:00
refactor(api): GetLanguages w/o mediatr
This commit is contained in:
parent
74e744f42e
commit
43c0e16d5f
11 changed files with 42 additions and 80 deletions
|
|
@ -7,17 +7,14 @@ internal static class CorsConfiguration
|
|||
private const string ProductionClientOrigin = "https://filterlists.com";
|
||||
private const string StagingClientOriginStart = "https://nice-water-05873140f";
|
||||
private const string StagingClientOriginEnd = ".eastus2.5.azurestaticapps.net";
|
||||
private const string NextJsStagingClientOrigin = "https://next.filterlists.com";
|
||||
private const string LocalClientOrigin = "http://localhost:3000";
|
||||
|
||||
internal static readonly Action<CorsOptions> SetupAction = options => options.AddDefaultPolicy(policy =>
|
||||
{
|
||||
policy.SetIsOriginAllowed(origin => origin switch
|
||||
{
|
||||
ProductionClientOrigin or LocalClientOrigin or NextJsStagingClientOrigin => true,
|
||||
_ => origin.StartsWith(StagingClientOriginStart, StringComparison.InvariantCulture) &&
|
||||
origin.EndsWith(StagingClientOriginEnd, StringComparison.InvariantCulture)
|
||||
})
|
||||
policy.SetIsOriginAllowed(origin =>
|
||||
origin is ProductionClientOrigin or LocalClientOrigin ||
|
||||
(origin.StartsWith(StagingClientOriginStart, StringComparison.InvariantCulture) &&
|
||||
origin.EndsWith(StagingClientOriginEnd, StringComparison.InvariantCulture)))
|
||||
.WithMethods("GET")
|
||||
.AllowAnyHeader();
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
using FilterLists.Directory.Application.Queries;
|
||||
using MediatR;
|
||||
using Microsoft.AspNetCore.Http.HttpResults;
|
||||
using Microsoft.OpenApi.Any;
|
||||
using Microsoft.OpenApi.Models;
|
||||
|
|
@ -11,10 +10,7 @@ internal static class Endpoints
|
|||
{
|
||||
internal static void MapEndpoints(this WebApplication app)
|
||||
{
|
||||
app.MapGet("/languages",
|
||||
(IMediator mediator, CancellationToken ct) =>
|
||||
mediator.Send(new GetLanguages.Request(), ct)
|
||||
)
|
||||
app.MapGet("/languages", async (GetLanguages.Query query, CancellationToken ct) => await query.ExecuteAsync(ct))
|
||||
.Produces<List<GetLanguages.Response>>()
|
||||
.WithOpenApi(operation => new OpenApiOperation(operation)
|
||||
{
|
||||
|
|
@ -23,10 +19,7 @@ internal static void MapEndpoints(this WebApplication app)
|
|||
OperationId = nameof(GetLanguages)
|
||||
});
|
||||
|
||||
app.MapGet("/licenses",
|
||||
(IMediator mediator, CancellationToken ct) =>
|
||||
mediator.Send(new GetLicenses.Request(), ct)
|
||||
)
|
||||
app.MapGet("/licenses", async (GetLicenses.Query query, CancellationToken ct) => await query.ExecuteAsync(ct))
|
||||
.Produces<List<GetLicenses.Response>>()
|
||||
.WithOpenApi(operation => new OpenApiOperation(operation)
|
||||
{
|
||||
|
|
@ -35,10 +28,7 @@ internal static void MapEndpoints(this WebApplication app)
|
|||
OperationId = nameof(GetLicenses)
|
||||
});
|
||||
|
||||
app.MapGet("/lists",
|
||||
(IMediator mediator, CancellationToken ct) =>
|
||||
mediator.Send(new GetLists.Request(), ct)
|
||||
)
|
||||
app.MapGet("/lists", async (GetLists.Query query, CancellationToken ct) => await query.ExecuteAsync(ct))
|
||||
.Produces<List<GetLists.Response>>()
|
||||
.WithOpenApi(operation => new OpenApiOperation(operation)
|
||||
{
|
||||
|
|
@ -48,10 +38,10 @@ internal static void MapEndpoints(this WebApplication app)
|
|||
});
|
||||
|
||||
app.MapGet("/lists/{id:int}",
|
||||
async Task<Results<Ok<GetListDetails.Response>, NotFound>> (int id, IMediator mediator,
|
||||
CancellationToken ct) =>
|
||||
async Task<Results<Ok<GetListDetails.Response>, NotFound>>
|
||||
(int id, GetListDetails.Query query, CancellationToken ct) =>
|
||||
{
|
||||
var list = await mediator.Send(new GetListDetails.Request(id), ct);
|
||||
var list = await query.ExecuteAsync(id, ct);
|
||||
return list is not null
|
||||
? TypedResults.Ok(list)
|
||||
: TypedResults.NotFound();
|
||||
|
|
@ -77,9 +67,7 @@ internal static void MapEndpoints(this WebApplication app)
|
|||
});
|
||||
|
||||
app.MapGet("/maintainers",
|
||||
(IMediator mediator, CancellationToken ct) =>
|
||||
mediator.Send(new GetMaintainers.Request(), ct)
|
||||
)
|
||||
async (GetMaintainers.Query query, CancellationToken ct) => await query.ExecuteAsync(ct))
|
||||
.Produces<List<GetMaintainers.Response>>()
|
||||
.WithOpenApi(operation => new OpenApiOperation(operation)
|
||||
{
|
||||
|
|
@ -88,10 +76,7 @@ internal static void MapEndpoints(this WebApplication app)
|
|||
OperationId = nameof(GetMaintainers)
|
||||
});
|
||||
|
||||
app.MapGet("/software",
|
||||
(IMediator mediator, CancellationToken ct) =>
|
||||
mediator.Send(new GetSoftware.Request(), ct)
|
||||
)
|
||||
app.MapGet("/software", async (GetSoftware.Query query, CancellationToken ct) => await query.ExecuteAsync(ct))
|
||||
.Produces<List<GetSoftware.Response>>()
|
||||
.WithOpenApi(operation => new OpenApiOperation(operation)
|
||||
{
|
||||
|
|
@ -100,10 +85,7 @@ internal static void MapEndpoints(this WebApplication app)
|
|||
OperationId = nameof(GetSoftware)
|
||||
});
|
||||
|
||||
app.MapGet("/syntaxes",
|
||||
(IMediator mediator, CancellationToken ct) =>
|
||||
mediator.Send(new GetSyntaxes.Request(), ct)
|
||||
)
|
||||
app.MapGet("/syntaxes", async (GetSyntaxes.Query query, CancellationToken ct) => await query.ExecuteAsync(ct))
|
||||
.Produces<List<GetSyntaxes.Response>>()
|
||||
.WithOpenApi(operation => new OpenApiOperation(operation)
|
||||
{
|
||||
|
|
@ -112,10 +94,7 @@ internal static void MapEndpoints(this WebApplication app)
|
|||
OperationId = nameof(GetSyntaxes)
|
||||
});
|
||||
|
||||
app.MapGet("/tags",
|
||||
(IMediator mediator, CancellationToken ct) =>
|
||||
mediator.Send(new GetTags.Request(), ct)
|
||||
)
|
||||
app.MapGet("/tags", async (GetTags.Query query, CancellationToken ct) => await query.ExecuteAsync(ct))
|
||||
.Produces<List<GetTags.Response>>()
|
||||
.WithOpenApi(operation => new OpenApiOperation(operation)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -1,4 +1,6 @@
|
|||
using FilterLists.Directory.Application.Queries;
|
||||
using FilterLists.Directory.Infrastructure;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
|
||||
namespace FilterLists.Directory.Application;
|
||||
|
|
@ -8,5 +10,13 @@ public static class ConfigurationExtensions
|
|||
public static void AddApplication(this IHostApplicationBuilder builder)
|
||||
{
|
||||
builder.AddInfrastructure();
|
||||
builder.Services.AddScoped<GetLanguages.Query>();
|
||||
builder.Services.AddScoped<GetLicenses.Query>();
|
||||
builder.Services.AddScoped<GetListDetails.Query>();
|
||||
builder.Services.AddScoped<GetLists.Query>();
|
||||
builder.Services.AddScoped<GetMaintainers.Query>();
|
||||
builder.Services.AddScoped<GetSoftware.Query>();
|
||||
builder.Services.AddScoped<GetSyntaxes.Query>();
|
||||
builder.Services.AddScoped<GetTags.Query>();
|
||||
}
|
||||
}
|
||||
|
|
@ -1,5 +1,4 @@
|
|||
using FilterLists.Directory.Infrastructure.Persistence.Queries.Context;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Caching.Hybrid;
|
||||
|
||||
|
|
@ -7,11 +6,9 @@ namespace FilterLists.Directory.Application.Queries;
|
|||
|
||||
public static class GetLanguages
|
||||
{
|
||||
public sealed record Request : IRequest<List<Response>>;
|
||||
|
||||
private sealed class Handler(QueryDbContext ctx, HybridCache cache) : IRequestHandler<Request, List<Response>>
|
||||
public class Query(QueryDbContext ctx, HybridCache cache)
|
||||
{
|
||||
public async Task<List<Response>> Handle(Request request, CancellationToken ct)
|
||||
public async Task<List<Response>> ExecuteAsync(CancellationToken ct)
|
||||
{
|
||||
const string key = nameof(GetLanguages);
|
||||
return await cache.GetOrCreateAsync(
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
using FilterLists.Directory.Infrastructure.Persistence.Queries.Context;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Caching.Hybrid;
|
||||
|
||||
|
|
@ -7,11 +6,9 @@ namespace FilterLists.Directory.Application.Queries;
|
|||
|
||||
public static class GetLicenses
|
||||
{
|
||||
public sealed record Request : IRequest<List<Response>>;
|
||||
|
||||
private sealed class Handler(QueryDbContext ctx, HybridCache cache) : IRequestHandler<Request, List<Response>>
|
||||
public class Query(QueryDbContext ctx, HybridCache cache)
|
||||
{
|
||||
public async Task<List<Response>> Handle(Request request, CancellationToken ct)
|
||||
public async Task<List<Response>> ExecuteAsync(CancellationToken ct)
|
||||
{
|
||||
const string key = nameof(GetLicenses);
|
||||
return await cache.GetOrCreateAsync(
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
using FilterLists.Directory.Infrastructure.Persistence.Queries.Context;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Caching.Hybrid;
|
||||
|
||||
|
|
@ -7,13 +6,11 @@ namespace FilterLists.Directory.Application.Queries;
|
|||
|
||||
public static class GetListDetails
|
||||
{
|
||||
public sealed record Request(int Id) : IRequest<Response?>;
|
||||
|
||||
private sealed class Handler(QueryDbContext ctx, HybridCache cache) : IRequestHandler<Request, Response?>
|
||||
public class Query(QueryDbContext ctx, HybridCache cache)
|
||||
{
|
||||
public async Task<Response?> Handle(Request request, CancellationToken ct)
|
||||
public async Task<Response?> ExecuteAsync(int id, CancellationToken ct)
|
||||
{
|
||||
var key = $"{nameof(GetListDetails)}_{request.Id}";
|
||||
var key = $"{nameof(GetListDetails)}_{id}";
|
||||
return await cache.GetOrCreateAsync(
|
||||
key,
|
||||
async cancel =>
|
||||
|
|
@ -74,7 +71,7 @@ await ctx.FilterLists
|
|||
.Select(fd => fd.DependentFilterListId)
|
||||
})
|
||||
.TagWith(key)
|
||||
.SingleOrDefaultAsync(l => l.Id == request.Id, cancel),
|
||||
.SingleOrDefaultAsync(l => l.Id == id, cancel),
|
||||
cancellationToken: ct);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
using FilterLists.Directory.Infrastructure.Persistence.Queries.Context;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Caching.Hybrid;
|
||||
|
||||
|
|
@ -7,11 +6,9 @@ namespace FilterLists.Directory.Application.Queries;
|
|||
|
||||
public static class GetLists
|
||||
{
|
||||
public sealed record Request : IRequest<List<Response>>;
|
||||
|
||||
private sealed class Handler(QueryDbContext ctx, HybridCache cache) : IRequestHandler<Request, List<Response>>
|
||||
public class Query(QueryDbContext ctx, HybridCache cache)
|
||||
{
|
||||
public async Task<List<Response>> Handle(Request request, CancellationToken ct)
|
||||
public async Task<List<Response>> ExecuteAsync(CancellationToken ct)
|
||||
{
|
||||
const string key = nameof(GetLists);
|
||||
return await cache.GetOrCreateAsync(
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
using FilterLists.Directory.Infrastructure.Persistence.Queries.Context;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Caching.Hybrid;
|
||||
|
||||
|
|
@ -7,11 +6,9 @@ namespace FilterLists.Directory.Application.Queries;
|
|||
|
||||
public static class GetMaintainers
|
||||
{
|
||||
public sealed record Request : IRequest<List<Response>>;
|
||||
|
||||
private sealed class Handler(QueryDbContext ctx, HybridCache cache) : IRequestHandler<Request, List<Response>>
|
||||
public class Query(QueryDbContext ctx, HybridCache cache)
|
||||
{
|
||||
public async Task<List<Response>> Handle(Request request, CancellationToken ct)
|
||||
public async Task<List<Response>> ExecuteAsync(CancellationToken ct)
|
||||
{
|
||||
const string key = nameof(GetMaintainers);
|
||||
return await cache.GetOrCreateAsync(
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
using FilterLists.Directory.Infrastructure.Persistence.Queries.Context;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Caching.Hybrid;
|
||||
|
||||
|
|
@ -7,11 +6,9 @@ namespace FilterLists.Directory.Application.Queries;
|
|||
|
||||
public static class GetSoftware
|
||||
{
|
||||
public sealed record Request : IRequest<List<Response>>;
|
||||
|
||||
private sealed class Handler(QueryDbContext ctx, HybridCache cache) : IRequestHandler<Request, List<Response>>
|
||||
public class Query(QueryDbContext ctx, HybridCache cache)
|
||||
{
|
||||
public async Task<List<Response>> Handle(Request request, CancellationToken ct)
|
||||
public async Task<List<Response>> ExecuteAsync(CancellationToken ct)
|
||||
{
|
||||
const string key = nameof(GetSoftware);
|
||||
return await cache.GetOrCreateAsync(
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
using FilterLists.Directory.Infrastructure.Persistence.Queries.Context;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Caching.Hybrid;
|
||||
|
||||
|
|
@ -7,11 +6,9 @@ namespace FilterLists.Directory.Application.Queries;
|
|||
|
||||
public static class GetSyntaxes
|
||||
{
|
||||
public sealed record Request : IRequest<List<Response>>;
|
||||
|
||||
private sealed class Handler(QueryDbContext ctx, HybridCache cache) : IRequestHandler<Request, List<Response>>
|
||||
public class Query(QueryDbContext ctx, HybridCache cache)
|
||||
{
|
||||
public async Task<List<Response>> Handle(Request request, CancellationToken ct)
|
||||
public async Task<List<Response>> ExecuteAsync(CancellationToken ct)
|
||||
{
|
||||
const string key = nameof(GetSyntaxes);
|
||||
return await cache.GetOrCreateAsync(
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
using FilterLists.Directory.Infrastructure.Persistence.Queries.Context;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Caching.Hybrid;
|
||||
|
||||
|
|
@ -7,11 +6,9 @@ namespace FilterLists.Directory.Application.Queries;
|
|||
|
||||
public static class GetTags
|
||||
{
|
||||
public sealed record Request : IRequest<List<Response>>;
|
||||
|
||||
private sealed class Handler(QueryDbContext ctx, HybridCache cache) : IRequestHandler<Request, List<Response>>
|
||||
public class Query(QueryDbContext ctx, HybridCache cache)
|
||||
{
|
||||
public async Task<List<Response>> Handle(Request request, CancellationToken ct)
|
||||
public async Task<List<Response>> ExecuteAsync(CancellationToken ct)
|
||||
{
|
||||
const string key = nameof(GetTags);
|
||||
return await cache.GetOrCreateAsync(
|
||||
|
|
|
|||
Loading…
Reference in a new issue