-
-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(dir): ✨🚧 WIP port query endpoints
- Loading branch information
1 parent
78749d6
commit 65969c4
Showing
21 changed files
with
949 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
using FilterLists.Directory.Application.Queries; | ||
using MediatR; | ||
|
||
namespace FilterLists.Directory.Api; | ||
|
||
internal static class Endpoints | ||
{ | ||
internal static void MapEndpoints(this WebApplication app) | ||
{ | ||
app.MapGet("/languages", | ||
(IMediator mediator, CancellationToken ct) => | ||
mediator.CreateStream(new GetLanguages.Request(), ct) | ||
); | ||
|
||
app.MapGet("/licenses", | ||
(IMediator mediator, CancellationToken ct) => | ||
mediator.CreateStream(new GetLicenses.Request(), ct) | ||
); | ||
|
||
app.MapGet("/lists", | ||
(IMediator mediator, CancellationToken ct) => | ||
mediator.CreateStream(new GetLists.Request(), ct) | ||
); | ||
|
||
app.MapGet("/lists/{id:int}", | ||
async (int id, IMediator mediator, CancellationToken ct) => | ||
await mediator.Send(new GetListDetails.Request(id), ct) | ||
); | ||
|
||
app.MapGet("/maintainers", | ||
(IMediator mediator, CancellationToken ct) => | ||
mediator.CreateStream(new GetMaintainers.Request(), ct) | ||
); | ||
|
||
app.MapGet("/software", | ||
(IMediator mediator, CancellationToken ct) => | ||
mediator.CreateStream(new GetSoftware.Request(), ct) | ||
); | ||
|
||
app.MapGet("/syntaxes", | ||
(IMediator mediator, CancellationToken ct) => | ||
mediator.CreateStream(new GetSyntaxes.Request(), ct) | ||
); | ||
|
||
app.MapGet("/tags", | ||
(IMediator mediator, CancellationToken ct) => | ||
mediator.CreateStream(new GetTags.Request(), ct) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,16 @@ | ||
using FilterLists.Directory.Api; | ||
using FilterLists.Directory.Application; | ||
|
||
var builder = WebApplication.CreateBuilder(args); | ||
|
||
// Add service defaults & Aspire components. | ||
builder.AddServiceDefaults(); | ||
|
||
// Add services to the container. | ||
builder.Services.AddProblemDetails(); | ||
builder.AddApplication(); | ||
|
||
var app = builder.Build(); | ||
|
||
// Configure the HTTP request pipeline. | ||
app.UseExceptionHandler(); | ||
|
||
var summaries = new[] | ||
{ | ||
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" | ||
}; | ||
|
||
app.MapGet("/weatherforecast", () => | ||
{ | ||
var forecast = Enumerable.Range(1, 5).Select(index => | ||
new WeatherForecast | ||
( | ||
DateOnly.FromDateTime(DateTime.Now.AddDays(index)), | ||
Random.Shared.Next(-20, 55), | ||
summaries[Random.Shared.Next(summaries.Length)] | ||
)) | ||
.ToArray(); | ||
return forecast; | ||
}); | ||
|
||
app.MapEndpoints(); | ||
app.MapDefaultEndpoints(); | ||
|
||
app.Run(); | ||
|
||
internal record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary) | ||
{ | ||
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); | ||
} | ||
app.Run(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
services/Directory/FilterLists.Directory.Application/ConfigurationExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using FilterLists.Directory.Infrastructure; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
|
||
namespace FilterLists.Directory.Application; | ||
|
||
public static class ConfigurationExtensions | ||
{ | ||
public static void AddApplication(this IHostApplicationBuilder builder) | ||
{ | ||
builder.AddInfrastructure(); | ||
builder.Services.AddMediatR(cfg => | ||
{ | ||
cfg.RegisterServicesFromAssembly(typeof(ConfigurationExtensions).Assembly); | ||
}); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
...ices/Directory/FilterLists.Directory.Application/FilterLists.Directory.Application.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="JetBrains.Annotations" Version="2023.3.0"/> | ||
<PackageReference Include="MediatR" Version="12.2.0"/> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\FilterLists.Directory.Infrastructure\FilterLists.Directory.Infrastructure.csproj"/> | ||
</ItemGroup> | ||
|
||
</Project> |
65 changes: 65 additions & 0 deletions
65
services/Directory/FilterLists.Directory.Application/Queries/GetLanguages.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
using System.Runtime.CompilerServices; | ||
using FilterLists.Directory.Infrastructure.Persistence.Queries.Context; | ||
using JetBrains.Annotations; | ||
using MediatR; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace FilterLists.Directory.Application.Queries; | ||
|
||
public static class GetLanguages | ||
{ | ||
private static readonly Func<QueryDbContext, IAsyncEnumerable<LanguageVm>> Query = | ||
EF.CompileAsyncQuery((QueryDbContext ctx) => | ||
ctx.Languages | ||
.Where(l => l.FilterListLanguages.Any()) | ||
.OrderBy(l => l.Iso6391) | ||
.Select(l => new LanguageVm | ||
{ | ||
Id = l.Id, | ||
Iso6391 = l.Iso6391, | ||
Name = l.Name, | ||
FilterListIds = l.FilterListLanguages | ||
.OrderBy(fl => fl.FilterListId) | ||
.Select(fl => fl.FilterListId) | ||
}) | ||
.TagWith(nameof(GetLanguages)) | ||
); | ||
|
||
public sealed record Request : IStreamRequest<LanguageVm>; | ||
|
||
internal sealed class Handler(QueryDbContext ctx) : IStreamRequestHandler<Request, LanguageVm> | ||
{ | ||
public async IAsyncEnumerable<LanguageVm> Handle(Request request, [EnumeratorCancellation] CancellationToken ct) | ||
{ | ||
await foreach (var language in Query(ctx).WithCancellation(ct)) yield return language; | ||
} | ||
} | ||
|
||
[PublicAPI] | ||
public record LanguageVm | ||
{ | ||
/// <summary> | ||
/// The identifier. | ||
/// </summary> | ||
/// <example>37</example> | ||
public short Id { get; init; } | ||
|
||
/// <summary> | ||
/// The unique ISO 639-1 code. | ||
/// </summary> | ||
/// <example>en</example> | ||
public required string Iso6391 { get; init; } | ||
|
||
/// <summary> | ||
/// The unique ISO name. | ||
/// </summary> | ||
/// <example>English</example> | ||
public required string Name { get; init; } | ||
|
||
/// <summary> | ||
/// The identifiers of the FilterLists targeted by this Language. | ||
/// </summary> | ||
/// <example>[ 114, 141 ]</example> | ||
public IEnumerable<int> FilterListIds { get; init; } = []; | ||
} | ||
} |
86 changes: 86 additions & 0 deletions
86
services/Directory/FilterLists.Directory.Application/Queries/GetLicenses.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
using System.Runtime.CompilerServices; | ||
using FilterLists.Directory.Infrastructure.Persistence.Queries.Context; | ||
using JetBrains.Annotations; | ||
using MediatR; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace FilterLists.Directory.Application.Queries; | ||
|
||
public static class GetLicenses | ||
{ | ||
private static readonly Func<QueryDbContext, IAsyncEnumerable<LicenseVm>> Query = | ||
EF.CompileAsyncQuery((QueryDbContext ctx) => | ||
ctx.Licenses | ||
.Where(l => l.FilterLists.Any()) | ||
.OrderBy(l => l.Id) | ||
.Select(l => new LicenseVm | ||
{ | ||
Id = l.Id, | ||
Name = l.Name, | ||
Url = l.Url, | ||
PermitsModification = l.PermitsModification, | ||
PermitsDistribution = l.PermitsDistribution, | ||
PermitsCommercialUse = l.PermitsCommercialUse, | ||
FilterListIds = l.FilterLists | ||
.OrderBy(f => f.Id) | ||
.Select(f => f.Id) | ||
}) | ||
.TagWith(nameof(GetLicenses)) | ||
); | ||
|
||
public sealed record Request : IStreamRequest<LicenseVm>; | ||
|
||
internal sealed class Handler(QueryDbContext ctx) : IStreamRequestHandler<Request, LicenseVm> | ||
{ | ||
public async IAsyncEnumerable<LicenseVm> Handle(Request request, [EnumeratorCancellation] CancellationToken ct) | ||
{ | ||
await foreach (var license in Query(ctx).WithCancellation(ct)) yield return license; | ||
} | ||
} | ||
|
||
[PublicAPI] | ||
public record LicenseVm | ||
{ | ||
/// <summary> | ||
/// The identifier. | ||
/// </summary> | ||
/// <example>5</example> | ||
public int Id { get; init; } | ||
|
||
/// <summary> | ||
/// The unique name. | ||
/// </summary> | ||
/// <example>All Rights Reserved</example> | ||
public required string Name { get; init; } | ||
|
||
/// <summary> | ||
/// The URL of the home page. | ||
/// </summary> | ||
/// <example>https://en.wikipedia.org/wiki/All_rights_reserved</example> | ||
public Uri? Url { get; init; } | ||
|
||
/// <summary> | ||
/// If the License permits modification. | ||
/// </summary> | ||
/// <example>false</example> | ||
public bool PermitsModification { get; init; } | ||
|
||
/// <summary> | ||
/// If the License permits distribution. | ||
/// </summary> | ||
/// <example>false</example> | ||
public bool PermitsDistribution { get; init; } | ||
|
||
/// <summary> | ||
/// If the License permits commercial use. | ||
/// </summary> | ||
/// <example>false</example> | ||
public bool PermitsCommercialUse { get; init; } | ||
|
||
/// <summary> | ||
/// The identifiers of the FilterLists released under this License. | ||
/// </summary> | ||
/// <example>[ 6, 31 ]</example> | ||
public IEnumerable<int> FilterListIds { get; init; } = []; | ||
} | ||
} |
Oops, something went wrong.