Skip to content

Commit 12e9e6b

Browse files
author
jarvis
committed
refactor: standardize endpoints to use TypedResults
1 parent 77623b1 commit 12e9e6b

17 files changed

Lines changed: 106 additions & 89 deletions

File tree

src/Modules/Identity/Modules.Identity/Features/v1/Roles/DeleteRole/DeleteRoleEndpoint.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public static RouteHandlerBuilder MapDeleteRoleEndpoint(this IEndpointRouteBuild
1515
return endpoints.MapDelete("/roles/{id:guid}", async (string id, IMediator mediator, CancellationToken cancellationToken) =>
1616
{
1717
await mediator.Send(new DeleteRoleCommand(id), cancellationToken);
18-
return Results.NoContent();
18+
return TypedResults.NoContent();
1919
})
2020
.WithName("DeleteRole")
2121
.WithSummary("Delete role by ID")

src/Modules/Identity/Modules.Identity/Features/v1/Roles/UpdateRolePermissions/UpdateRolePermissionsEndpoint.cs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Mediator;
55
using Microsoft.AspNetCore.Builder;
66
using Microsoft.AspNetCore.Http;
7+
using Microsoft.AspNetCore.Http.HttpResults;
78
using Microsoft.AspNetCore.Mvc;
89
using Microsoft.AspNetCore.Routing;
910

@@ -13,23 +14,25 @@ public static class UpdateRolePermissionsEndpoint
1314
{
1415
public static RouteHandlerBuilder MapUpdateRolePermissionsEndpoint(this IEndpointRouteBuilder endpoints)
1516
{
16-
return endpoints.MapPut("/{id}/permissions", async (
17-
string id,
18-
[FromBody] UpdatePermissionsCommand request,
19-
IMediator mediator,
20-
CancellationToken cancellationToken) =>
21-
{
22-
if (id != request.RoleId)
23-
{
24-
return Results.BadRequest();
25-
}
26-
27-
var response = await mediator.Send(request, cancellationToken);
28-
return Results.Ok(response);
29-
})
17+
return endpoints.MapPut("/{id}/permissions", Handler)
3018
.WithName("UpdateRolePermissions")
3119
.WithSummary("Update role permissions")
3220
.RequirePermission(IdentityPermissionConstants.Roles.Update)
3321
.WithDescription("Replace the set of permissions assigned to a role.");
3422
}
23+
24+
private static async Task<Results<Ok<string>, BadRequest>> Handler(
25+
string id,
26+
[FromBody] UpdatePermissionsCommand request,
27+
IMediator mediator,
28+
CancellationToken cancellationToken)
29+
{
30+
if (id != request.RoleId)
31+
{
32+
return TypedResults.BadRequest();
33+
}
34+
35+
var response = await mediator.Send(request, cancellationToken);
36+
return TypedResults.Ok(response);
37+
}
3538
}

src/Modules/Identity/Modules.Identity/Features/v1/Sessions/AdminRevokeSession/AdminRevokeSessionEndpoint.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Mediator;
55
using Microsoft.AspNetCore.Builder;
66
using Microsoft.AspNetCore.Http;
7+
using Microsoft.AspNetCore.Http.HttpResults;
78
using Microsoft.AspNetCore.Routing;
89

910
namespace FSH.Modules.Identity.Features.v1.Sessions.AdminRevokeSession;
@@ -19,13 +20,13 @@ internal static RouteHandlerBuilder MapAdminRevokeSessionEndpoint(this IEndpoint
1920
.WithDescription("Revoke a specific session for a user. Requires admin permission.");
2021
}
2122

22-
private static async Task<IResult> Handler(
23+
private static async Task<Results<Ok, NotFound>> Handler(
2324
Guid userId,
2425
Guid sessionId,
2526
IMediator mediator,
2627
CancellationToken cancellationToken)
2728
{
2829
var result = await mediator.Send(new AdminRevokeSessionCommand(userId, sessionId), cancellationToken);
29-
return result ? Results.Ok() : Results.NotFound();
30+
return result ? TypedResults.Ok() : TypedResults.NotFound();
3031
}
3132
}

src/Modules/Identity/Modules.Identity/Features/v1/Sessions/RevokeSession/RevokeSessionEndpoint.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Mediator;
55
using Microsoft.AspNetCore.Builder;
66
using Microsoft.AspNetCore.Http;
7+
using Microsoft.AspNetCore.Http.HttpResults;
78
using Microsoft.AspNetCore.Routing;
89

910
namespace FSH.Modules.Identity.Features.v1.Sessions.RevokeSession;
@@ -19,12 +20,12 @@ internal static RouteHandlerBuilder MapRevokeSessionEndpoint(this IEndpointRoute
1920
.WithDescription("Revoke a specific session for the currently authenticated user.");
2021
}
2122

22-
private static async Task<IResult> Handler(
23+
private static async Task<Results<Ok, NotFound>> Handler(
2324
Guid sessionId,
2425
IMediator mediator,
2526
CancellationToken cancellationToken)
2627
{
2728
var result = await mediator.Send(new RevokeSessionCommand(sessionId), cancellationToken);
28-
return result ? Results.Ok() : Results.NotFound();
29+
return result ? TypedResults.Ok() : TypedResults.NotFound();
2930
}
3031
}
Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
using FSH.Framework.Shared.Identity;
1+
using FSH.Framework.Shared.Identity;
22
using FSH.Framework.Shared.Identity.Authorization;
33
using FSH.Modules.Identity.Contracts.v1.Users.AssignUserRoles;
44
using Mediator;
55
using Microsoft.AspNetCore.Builder;
66
using Microsoft.AspNetCore.Http;
7+
using Microsoft.AspNetCore.Http.HttpResults;
78
using Microsoft.AspNetCore.Routing;
89

910
namespace FSH.Modules.Identity.Features.v1.Users.AssignUserRoles;
@@ -12,24 +13,25 @@ public static class AssignUserRolesEndpoint
1213
{
1314
internal static RouteHandlerBuilder MapAssignUserRolesEndpoint(this IEndpointRouteBuilder endpoints)
1415
{
15-
return endpoints.MapPost("/users/{id:guid}/roles", async (
16-
string id,
17-
AssignUserRolesCommand command,
18-
HttpContext context,
19-
IMediator mediator,
20-
CancellationToken cancellationToken) =>
21-
{
22-
if (!string.Equals(id, command.UserId, StringComparison.Ordinal))
23-
{
24-
return Results.BadRequest();
25-
}
26-
27-
var result = await mediator.Send(command, cancellationToken);
28-
return Results.Ok(result);
29-
})
16+
return endpoints.MapPost("/users/{id:guid}/roles", Handler)
3017
.WithName("AssignUserRoles")
3118
.WithSummary("Assign roles to user")
3219
.WithDescription("Assign one or more roles to a user.")
3320
.RequirePermission(IdentityPermissionConstants.Users.ManageRoles);
3421
}
22+
23+
private static async Task<Results<Ok<string>, BadRequest>> Handler(
24+
string id,
25+
AssignUserRolesCommand command,
26+
IMediator mediator,
27+
CancellationToken cancellationToken)
28+
{
29+
if (!string.Equals(id, command.UserId, StringComparison.Ordinal))
30+
{
31+
return TypedResults.BadRequest();
32+
}
33+
34+
var result = await mediator.Send(command, cancellationToken);
35+
return TypedResults.Ok(result);
36+
}
3537
}

src/Modules/Identity/Modules.Identity/Features/v1/Users/ChangePassword/ChangePasswordEndpoint.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ internal static RouteHandlerBuilder MapChangePasswordEndpoint(this IEndpointRout
1717
CancellationToken cancellationToken) =>
1818
{
1919
var result = await mediator.Send(command, cancellationToken);
20-
return Results.Ok(result);
20+
return TypedResults.Ok(result);
2121
})
2222
.WithName("ChangePassword")
2323
.WithSummary("Change password")

src/Modules/Identity/Modules.Identity/Features/v1/Users/ConfirmEmail/ConfirmEmailEndpoint.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ internal static RouteHandlerBuilder MapConfirmEmailEndpoint(this IEndpointRouteB
1313
return endpoints.MapGet("/confirm-email", async (string userId, string code, string tenant, IMediator mediator, CancellationToken cancellationToken) =>
1414
{
1515
var result = await mediator.Send(new ConfirmEmailCommand(userId, code, tenant), cancellationToken);
16-
return Results.Ok(result);
16+
return TypedResults.Ok(result);
1717
})
1818
.WithName("ConfirmEmail")
1919
.WithSummary("Confirm user email")

src/Modules/Identity/Modules.Identity/Features/v1/Users/DeleteUser/DeleteUserEndpoint.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ internal static RouteHandlerBuilder MapDeleteUserEndpoint(this IEndpointRouteBui
1515
return endpoints.MapDelete("/users/{id:guid}", async (string id, IMediator mediator, CancellationToken cancellationToken) =>
1616
{
1717
await mediator.Send(new DeleteUserCommand(id), cancellationToken);
18-
return Results.NoContent();
18+
return TypedResults.NoContent();
1919
})
2020
.WithName("DeleteUser")
2121
.WithSummary("Delete user")

src/Modules/Identity/Modules.Identity/Features/v1/Users/ForgotPassword/ForgotPasswordEndpoint.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ internal static RouteHandlerBuilder MapForgotPasswordEndpoint(this IEndpointRout
2020
CancellationToken cancellationToken) =>
2121
{
2222
var result = await mediator.Send(command, cancellationToken);
23-
return Results.Ok(result);
23+
return TypedResults.Ok(result);
2424
})
2525
.WithName("RequestPasswordReset")
2626
.WithSummary("Request password reset")

src/Modules/Identity/Modules.Identity/Features/v1/Users/ResetPassword/ResetPasswordEndpoint.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ internal static RouteHandlerBuilder MapResetPasswordEndpoint(this IEndpointRoute
1919
CancellationToken cancellationToken) =>
2020
{
2121
var result = await mediator.Send(command, cancellationToken);
22-
return Results.Ok(result);
22+
return TypedResults.Ok(result);
2323
})
2424
.WithName("ResetPassword")
2525
.WithSummary("Reset password")

0 commit comments

Comments
 (0)