Skip to content

Commit

Permalink
⚗️ Clean-up endpoint group feature.
Browse files Browse the repository at this point in the history
  • Loading branch information
jasontaylordev committed Jun 29, 2023
1 parent d88bff0 commit 83e31a7
Show file tree
Hide file tree
Showing 12 changed files with 133 additions and 70 deletions.
59 changes: 0 additions & 59 deletions src/WebUI/Endpoints/TodoListEndpoints.cs

This file was deleted.

43 changes: 43 additions & 0 deletions src/WebUI/Endpoints/TodoListsEndpointGroup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using CleanArchitecture.Application.TodoLists.Commands.CreateTodoList;
using CleanArchitecture.Application.TodoLists.Commands.DeleteTodoList;
using CleanArchitecture.Application.TodoLists.Commands.UpdateTodoList;
using CleanArchitecture.Application.TodoLists.Queries.ExportTodos;
using CleanArchitecture.Application.TodoLists.Queries.GetTodos;

namespace CleanArchitecture.WebUI.Endpoints;

public class TodoListsEndpointGroup : EndpointGroupBase
{
public override void Map(WebApplication app)
{
MapGroup("TodoLists", app);

MapGet("GetTodoLists",
async (ISender sender) => await sender.Send(new GetTodosQuery()));

MapGet("ExportTodos", "Export/{id}",
async (ISender sender, int id) =>
{
var vm = await sender.Send(new ExportTodosQuery { ListId = id });
return Results.File(vm.Content, vm.ContentType, vm.FileName);
});

MapPost("CreateTodoList",
async (ISender sender, CreateTodoListCommand command) => await sender.Send(command));

MapPut("UpdateTodoList", "{id}",
async (ISender sender, int id, UpdateTodoListCommand command) =>
{
if (id != command.Id) return Results.BadRequest();
await sender.Send(command);
return Results.NoContent();
});

MapDelete("DeleteTodoList", "{id}",
async (ISender sender, int id) =>
{
await sender.Send(new DeleteTodoListCommand(id));
return Results.NoContent();
});
}
}
2 changes: 1 addition & 1 deletion src/WebUI/Infrastructure/EndPointBase.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace CleanArchitecture.WebUI.Infrastructure;

public abstract class EndPointBase : IEndpoint
public abstract class EndpointBase : IEndpoint
{
public abstract void Map(WebApplication app);

Expand Down
60 changes: 60 additions & 0 deletions src/WebUI/Infrastructure/EndpointGroupBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using CleanArchitecture.WebUI.Filters;

namespace CleanArchitecture.WebUI.Infrastructure;

public abstract class EndpointGroupBase
{
private string? _groupName;
private RouteGroupBuilder? _group;

public abstract void Map(WebApplication app);

protected void MapGroup(string groupName, WebApplication app)
{
_groupName = groupName;

_group = app
.MapGroup($"/api/{_groupName}")
.WithGroupName(_groupName)
.WithTags(_groupName)
.RequireAuthorization()
.WithOpenApi()
.AddEndpointFilter<ApiExceptionFilter>();
}

protected void MapGet(string name, Delegate handler)
{
MapGet(name, "", handler);
}

protected void MapGet(string name, string prefix, Delegate handler)
{
_group!.MapGet(prefix, handler)
.WithName(GetEndpointName(name));
}

protected void MapPost(string name, Delegate handler)
{
MapPost(name, "", handler);
}

protected void MapPost(string name, string prefix, Delegate handler)
{
_group!.MapPost(prefix, handler)
.WithName(GetEndpointName(name));
}

protected void MapPut(string name, string prefix, Delegate handler)
{
_group!.MapPut(prefix, handler)
.WithName(GetEndpointName(name));
}

protected void MapDelete(string name, string prefix, Delegate handler)
{
_group!.MapDelete(prefix, handler)
.WithName(GetEndpointName(name));
}

private string GetEndpointName(string name) => $"{_groupName}_{name}";
}
2 changes: 1 addition & 1 deletion src/WebUI/Infrastructure/RouteHandlerBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace CleanArchitecture.WebUI.Infrastructure;

public static class RouteHandlerBuilderExtensions
{
public static RouteHandlerBuilder WithDefaults(this RouteHandlerBuilder builder, EndPointBase endpoint)
public static RouteHandlerBuilder WithDefaults(this RouteHandlerBuilder builder, EndpointBase endpoint)
{
return builder
.WithName($"{endpoint.Group}_{endpoint.Name}")
Expand Down
24 changes: 22 additions & 2 deletions src/WebUI/Infrastructure/WebApplicationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ public static class WebApplicationExtensions
{
public static WebApplication MapEndpoints(this WebApplication app)
{
var endPointType = typeof(IEndpoint);
var endpointType = typeof(IEndpoint);

var assembly = Assembly.GetExecutingAssembly();

var endpointTypes = assembly.GetExportedTypes()
.Where(t => t.IsAbstract == false &&
t.GetInterfaces().Contains(endPointType));
t.GetInterfaces().Contains(endpointType));

foreach (var type in endpointTypes)
{
Expand All @@ -24,4 +24,24 @@ public static WebApplication MapEndpoints(this WebApplication app)

return app;
}

public static WebApplication MapEndpointGroups(this WebApplication app)
{
var endpointGroupType = typeof(EndpointGroupBase);

var assembly = Assembly.GetExecutingAssembly();

var endpointGroupTypes = assembly.GetExportedTypes()
.Where(t => t.IsSubclassOf(endpointGroupType));

foreach (var type in endpointGroupTypes)
{
if (Activator.CreateInstance(type) is EndpointGroupBase instance)
{
instance.Map(app);
}
}

return app;
}
}
3 changes: 1 addition & 2 deletions src/WebUI/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using CleanArchitecture.Infrastructure.Persistence;
using CleanArchitecture.WebUI.Endpoints;

var builder = WebApplication.CreateBuilder(args);

Expand Down Expand Up @@ -48,6 +47,6 @@
app.MapFallbackToFile("index.html");

app.MapEndpoints();
TodoListEndpoints.Map(app);
app.MapEndpointGroups();

app.Run();
2 changes: 1 addition & 1 deletion src/WebUI/TodoItems/CreateTodoItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace CleanArchitecture.WebUI.TodoItems;

public class CreateTodoItem : EndPointBase
public class CreateTodoItem : EndpointBase
{
public override void Map(WebApplication app)
{
Expand Down
2 changes: 1 addition & 1 deletion src/WebUI/TodoItems/DeleteTodoItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace CleanArchitecture.WebUI.TodoItems;

public class DeleteTodoItem : EndPointBase
public class DeleteTodoItem : EndpointBase
{
public override void Map(WebApplication app)
{
Expand Down
2 changes: 1 addition & 1 deletion src/WebUI/TodoItems/UpdateTodoItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace CleanArchitecture.WebUI.TodoItems;

public class UpdateTodoItem : EndPointBase
public class UpdateTodoItem : EndpointBase
{
public override void Map(WebApplication app)
{
Expand Down
2 changes: 1 addition & 1 deletion src/WebUI/TodoItems/UpdateTodoItemDetail.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace CleanArchitecture.WebUI.TodoItems;

public class UpdateTodoItemDetail : EndPointBase
public class UpdateTodoItemDetail : EndpointBase
{
public override void Map(WebApplication app)
{
Expand Down
2 changes: 1 addition & 1 deletion src/WebUI/WeatherForecasts/GetWeatherForecasts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace CleanArchitecture.WebUI.WeatherForecasts;

public class GetWeatherForecasts : EndPointBase
public class GetWeatherForecasts : EndpointBase
{
public override void Map(WebApplication app)
{
Expand Down

0 comments on commit 83e31a7

Please sign in to comment.