Skip to content

Commit c9d6b03

Browse files
samanazadi1996jasontaylordev
authored andcommitted
Refactor endpoint mapping to use RouteGroupBuilder
1 parent 0bcf11b commit c9d6b03

File tree

6 files changed

+36
-46
lines changed

6 files changed

+36
-46
lines changed

src/Web/Endpoints/TodoItems.cs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,13 @@ namespace CleanArchitecture.Web.Endpoints;
1010

1111
public class TodoItems : EndpointGroupBase
1212
{
13-
public override void Map(WebApplication app)
13+
public override void Map(RouteGroupBuilder groupBuilder)
1414
{
15-
app.MapGroup(this)
16-
.RequireAuthorization()
17-
.MapGet(GetTodoItemsWithPagination)
18-
.MapPost(CreateTodoItem)
19-
.MapPut(UpdateTodoItem, "{id}")
20-
.MapPut(UpdateTodoItemDetail, "UpdateDetail/{id}")
21-
.MapDelete(DeleteTodoItem, "{id}");
15+
groupBuilder.MapGet(GetTodoItemsWithPagination).RequireAuthorization();
16+
groupBuilder.MapPost(CreateTodoItem).RequireAuthorization();
17+
groupBuilder.MapPut(UpdateTodoItem, "{id}").RequireAuthorization();
18+
groupBuilder.MapPut(UpdateTodoItemDetail, "UpdateDetail/{id}").RequireAuthorization();
19+
groupBuilder.MapDelete(DeleteTodoItem, "{id}").RequireAuthorization();
2220
}
2321

2422
public async Task<Ok<PaginatedList<TodoItemBriefDto>>> GetTodoItemsWithPagination(ISender sender, [AsParameters] GetTodoItemsWithPaginationQuery query)
@@ -47,9 +45,9 @@ public async Task<Results<NoContent, BadRequest>> UpdateTodoItem(ISender sender,
4745
public async Task<Results<NoContent, BadRequest>> UpdateTodoItemDetail(ISender sender, int id, UpdateTodoItemDetailCommand command)
4846
{
4947
if (id != command.Id) return TypedResults.BadRequest();
50-
48+
5149
await sender.Send(command);
52-
50+
5351
return TypedResults.NoContent();
5452
}
5553

src/Web/Endpoints/TodoLists.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,12 @@ namespace CleanArchitecture.Web.Endpoints;
88

99
public class TodoLists : EndpointGroupBase
1010
{
11-
public override void Map(WebApplication app)
11+
public override void Map(RouteGroupBuilder groupBuilder)
1212
{
13-
app.MapGroup(this)
14-
.RequireAuthorization()
15-
.MapGet(GetTodoLists)
16-
.MapPost(CreateTodoList)
17-
.MapPut(UpdateTodoList, "{id}")
18-
.MapDelete(DeleteTodoList, "{id}");
13+
groupBuilder.MapGet(GetTodoLists).RequireAuthorization();
14+
groupBuilder.MapPost(CreateTodoList).RequireAuthorization();
15+
groupBuilder.MapPut(UpdateTodoList, "{id}").RequireAuthorization();
16+
groupBuilder.MapDelete(DeleteTodoList, "{id}").RequireAuthorization();
1917
}
2018

2119
public async Task<Ok<TodosVm>> GetTodoLists(ISender sender)
@@ -35,7 +33,7 @@ public async Task<Created<int>> CreateTodoList(ISender sender, CreateTodoListCom
3533
public async Task<Results<NoContent, BadRequest>> UpdateTodoList(ISender sender, int id, UpdateTodoListCommand command)
3634
{
3735
if (id != command.Id) return TypedResults.BadRequest();
38-
36+
3937
await sender.Send(command);
4038

4139
return TypedResults.NoContent();

src/Web/Endpoints/WeatherForecasts.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,18 @@ namespace CleanArchitecture.Web.Endpoints;
55

66
public class WeatherForecasts : EndpointGroupBase
77
{
8-
public override void Map(WebApplication app)
8+
public override void Map(RouteGroupBuilder groupBuilder)
99
{
10-
app.MapGroup(this)
11-
.RequireAuthorization()
12-
.MapGet(GetWeatherForecasts);
10+
groupBuilder.RequireAuthorization();
11+
12+
groupBuilder.MapGet(GetWeatherForecasts);
1313
}
1414

1515
public async Task<Ok<IEnumerable<WeatherForecast>>> GetWeatherForecasts(ISender sender)
1616
{
1717
var forecasts = await sender.Send(new GetWeatherForecastsQuery());
18-
18+
1919
return TypedResults.Ok(forecasts);
2020
}
21-
}
21+
22+
}

src/Web/Infrastructure/EndpointGroupBase.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22

33
public abstract class EndpointGroupBase
44
{
5-
public abstract void Map(WebApplication app);
5+
public virtual string? GroupName { get; }
6+
public abstract void Map(RouteGroupBuilder groupBuilder);
67
}

src/Web/Infrastructure/IEndpointRouteBuilderExtensions.cs

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,45 +2,37 @@
22

33
namespace CleanArchitecture.Web.Infrastructure;
44

5-
public static class IEndpointRouteBuilderExtensions
5+
public static class EndpointRouteBuilderExtensions
66
{
7-
public static IEndpointRouteBuilder MapGet(this IEndpointRouteBuilder builder, Delegate handler, [StringSyntax("Route")] string pattern = "")
7+
public static RouteHandlerBuilder MapGet(this IEndpointRouteBuilder builder, Delegate handler, [StringSyntax("Route")] string pattern = "")
88
{
99
Guard.Against.AnonymousMethod(handler);
1010

11-
builder.MapGet(pattern, handler)
12-
.WithName(handler.Method.Name);
13-
14-
return builder;
11+
return builder.MapGet(pattern, handler)
12+
.WithName(handler.Method.Name);
1513
}
1614

17-
public static IEndpointRouteBuilder MapPost(this IEndpointRouteBuilder builder, Delegate handler, [StringSyntax("Route")] string pattern = "")
15+
public static RouteHandlerBuilder MapPost(this IEndpointRouteBuilder builder, Delegate handler, [StringSyntax("Route")] string pattern = "")
1816
{
1917
Guard.Against.AnonymousMethod(handler);
2018

21-
builder.MapPost(pattern, handler)
19+
return builder.MapPost(pattern, handler)
2220
.WithName(handler.Method.Name);
23-
24-
return builder;
2521
}
2622

27-
public static IEndpointRouteBuilder MapPut(this IEndpointRouteBuilder builder, Delegate handler, [StringSyntax("Route")] string pattern)
23+
public static RouteHandlerBuilder MapPut(this IEndpointRouteBuilder builder, Delegate handler, [StringSyntax("Route")] string pattern)
2824
{
2925
Guard.Against.AnonymousMethod(handler);
3026

31-
builder.MapPut(pattern, handler)
27+
return builder.MapPut(pattern, handler)
3228
.WithName(handler.Method.Name);
33-
34-
return builder;
3529
}
3630

37-
public static IEndpointRouteBuilder MapDelete(this IEndpointRouteBuilder builder, Delegate handler, [StringSyntax("Route")] string pattern)
31+
public static RouteHandlerBuilder MapDelete(this IEndpointRouteBuilder builder, Delegate handler, [StringSyntax("Route")] string pattern)
3832
{
3933
Guard.Against.AnonymousMethod(handler);
4034

41-
builder.MapDelete(pattern, handler)
35+
return builder.MapDelete(pattern, handler)
4236
.WithName(handler.Method.Name);
43-
44-
return builder;
4537
}
4638
}

src/Web/Infrastructure/WebApplicationExtensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ namespace CleanArchitecture.Web.Infrastructure;
44

55
public static class WebApplicationExtensions
66
{
7-
public static RouteGroupBuilder MapGroup(this WebApplication app, EndpointGroupBase group)
7+
private static RouteGroupBuilder MapGroup(this WebApplication app, EndpointGroupBase group)
88
{
9-
var groupName = group.GetType().Name;
9+
var groupName = group.GroupName ?? group.GetType().Name;
1010

1111
return app
1212
.MapGroup($"/api/{groupName}")
@@ -27,7 +27,7 @@ public static WebApplication MapEndpoints(this WebApplication app)
2727
{
2828
if (Activator.CreateInstance(type) is EndpointGroupBase instance)
2929
{
30-
instance.Map(app);
30+
instance.Map(app.MapGroup(instance));
3131
}
3232
}
3333

0 commit comments

Comments
 (0)