Skip to content

Commit

Permalink
feat(dir): ✨🚧 WIP port query endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
collinbarrett committed Jun 3, 2024
1 parent 78749d6 commit 97c740f
Show file tree
Hide file tree
Showing 18 changed files with 695 additions and 88 deletions.
35 changes: 35 additions & 0 deletions services/Directory/FilterLists.Directory.Api/Endpoints.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
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)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

<ItemGroup>
<ProjectReference Include="..\..\FilterLists.ServiceDefaults\FilterLists.ServiceDefaults.csproj"/>
<ProjectReference Include="..\FilterLists.Directory.Application\FilterLists.Directory.Application.csproj"/>
</ItemGroup>

</Project>
35 changes: 6 additions & 29 deletions services/Directory/FilterLists.Directory.Api/Program.cs
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();
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "weatherforecast",
"launchUrl": "lists",
"applicationUrl": "http://localhost:5444",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
Expand All @@ -15,11 +15,11 @@
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "weatherforecast",
"launchUrl": "lists",
"applicationUrl": "https://localhost:7490;http://localhost:5444",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
}
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);
});
}
}
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>
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; } = [];
}
}
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; } = [];
}
}
Loading

0 comments on commit 97c740f

Please sign in to comment.