Skip to content

Commit 02c1c81

Browse files
authored
Move logged-in user endpoints to /users/me and apply code cleanup (#682)
### Summary & Motivation Following the previous change that moved tenant-related API endpoints to `/tenants/current`, all endpoints for the logged-in user are now moved to `/users/me` for consistency. This applies to all relevant endpoints, including `GET`, `PUT`, `change-locale`, `update-avatar`, `remove-avatar`, etc. Commands, tests, and the frontend have been updated to align with this change, including the removal of obsolete tests. Additionally, minor code cleanups have been applied: - Simplified guards in commands to one-liners where appropriate. - Updated `IDomainEvent` documentation to remove outdated references to the Application Layer. - Standardized spelling by replacing British English with American English. ### Checklist - [x] I have added tests, or done manual regression tests - [x] I have updated the documentation, if necessary
2 parents 6949fb3 + c63bfb2 commit 02c1c81

File tree

20 files changed

+150
-217
lines changed

20 files changed

+150
-217
lines changed

application/account-management/Api/Endpoints/UserEndpoints.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@ public void MapEndpoints(IEndpointRouteBuilder routes)
2222
=> await mediator.Send(new GetUserSummaryQuery())
2323
).Produces<GetUserSummaryResponse>();
2424

25-
group.MapGet("/{id}", async Task<ApiResult<UserResponse>> ([AsParameters] GetUserQuery query, IMediator mediator)
26-
=> await mediator.Send(query)
27-
).Produces<UserResponse>();
28-
2925
group.MapPost("/", async Task<ApiResult> (CreateUserCommand command, IMediator mediator)
3026
=> (await mediator.Send(command)).AddResourceUri(RoutesPrefix)
3127
);
@@ -43,19 +39,23 @@ public void MapEndpoints(IEndpointRouteBuilder routes)
4339
);
4440

4541
// The following endpoints are for the current user only
46-
group.MapPut("/", async Task<ApiResult> (UpdateUserCommand command, IMediator mediator)
42+
group.MapGet("/me", async Task<ApiResult<UserResponse>> ([AsParameters] GetUserQuery query, IMediator mediator)
43+
=> await mediator.Send(query)
44+
).Produces<UserResponse>();
45+
46+
group.MapPut("/me", async Task<ApiResult> (UpdateCurrentUserCommand command, IMediator mediator)
4747
=> (await mediator.Send(command)).AddRefreshAuthenticationTokens()
4848
);
4949

50-
group.MapPost("/update-avatar", async Task<ApiResult> (IFormFile file, IMediator mediator)
50+
group.MapPost("/me/update-avatar", async Task<ApiResult> (IFormFile file, IMediator mediator)
5151
=> await mediator.Send(new UpdateAvatarCommand(file.OpenReadStream(), file.ContentType))
5252
).DisableAntiforgery(); // Disable anti-forgery until we implement it
5353

54-
group.MapDelete("/remove-avatar", async Task<ApiResult> (IMediator mediator)
54+
group.MapDelete("/me/remove-avatar", async Task<ApiResult> (IMediator mediator)
5555
=> await mediator.Send(new RemoveAvatarCommand())
5656
);
5757

58-
group.MapPut("/change-locale", async Task<ApiResult> (ChangeLocaleCommand command, IMediator mediator)
58+
group.MapPut("/me/change-locale", async Task<ApiResult> (ChangeLocaleCommand command, IMediator mediator)
5959
=> (await mediator.Send(command)).AddRefreshAuthenticationTokens()
6060
);
6161
}

application/account-management/Core/Features/Authentication/Commands/CompleteLogin.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,7 @@ public async Task<Result> Handle(CompleteLoginCommand command, CancellationToken
3333
{
3434
var login = await loginRepository.GetByIdAsync(command.Id, cancellationToken);
3535

36-
if (login is null)
37-
{
38-
return Result.NotFound($"Login with id '{command.Id}' not found.");
39-
}
36+
if (login is null) return Result.NotFound($"Login with id '{command.Id}' not found.");
4037

4138
if (login.Completed)
4239
{

application/account-management/Core/Features/Authentication/Commands/ResendLoginCode.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,7 @@ ILogger<ResendLoginCodeCommandHandler> logger
3131
public async Task<Result<ResendLoginCodeResponse>> Handle(ResendLoginCodeCommand command, CancellationToken cancellationToken)
3232
{
3333
var login = await loginRepository.GetByIdAsync(command.Id, cancellationToken);
34-
if (login is null)
35-
{
36-
return Result<ResendLoginCodeResponse>.NotFound($"Login with id '{command.Id}' not found.");
37-
}
34+
if (login is null) return Result<ResendLoginCodeResponse>.NotFound($"Login with id '{command.Id}' not found.");
3835

3936
if (login.Completed)
4037
{

application/account-management/Core/Features/Signups/Commands/CompleteSignup.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,7 @@ public async Task<Result> Handle(CompleteSignupCommand command, CancellationToke
3333
{
3434
var signup = await signupRepository.GetByIdAsync(command.Id, cancellationToken);
3535

36-
if (signup is null)
37-
{
38-
return Result.NotFound($"Signup with id '{command.Id}' not found.");
39-
}
36+
if (signup is null) return Result.NotFound($"Signup with id '{command.Id}' not found.");
4037

4138
if (signup.Completed)
4239
{

application/account-management/Core/Features/Signups/Commands/ResendSignupCode.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,7 @@ ILogger<ResendSignupCodeCommandHandler> logger
2929
public async Task<Result<ResendSignupCodeResponse>> Handle(ResendSignupCodeCommand command, CancellationToken cancellationToken)
3030
{
3131
var signup = await signupRepository.GetByIdAsync(command.Id, cancellationToken);
32-
if (signup is null)
33-
{
34-
return Result<ResendSignupCodeResponse>.NotFound($"Signup with id '{command.Id}' not found.");
35-
}
32+
if (signup is null) return Result<ResendSignupCodeResponse>.NotFound($"Signup with id '{command.Id}' not found.");
3633

3734
if (signup.Completed)
3835
{

application/account-management/Core/Features/Users/Commands/ChangeLocale.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ public sealed class ChangeLocaleHandler(IUserRepository userRepository, ITelemet
2626
public async Task<Result> Handle(ChangeLocaleCommand command, CancellationToken cancellationToken)
2727
{
2828
var user = await userRepository.GetLoggedInUserAsync(cancellationToken);
29-
if (user is null) return Result.BadRequest("User not found.");
30-
3129
var fromLocale = user.Locale;
3230
user.ChangeLocale(command.Locale);
3331
userRepository.Update(user);

application/account-management/Core/Features/Users/Commands/ChangeUserRole.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,7 @@ public sealed class ChangeUserRoleHandler(IUserRepository userRepository, IExecu
2121
{
2222
public async Task<Result> Handle(ChangeUserRoleCommand command, CancellationToken cancellationToken)
2323
{
24-
if (executionContext.UserInfo.Id == command.Id)
25-
{
26-
return Result.Forbidden("You cannot change your own user role.");
27-
}
24+
if (executionContext.UserInfo.Id == command.Id) return Result.Forbidden("You cannot change your own user role.");
2825

2926
if (executionContext.UserInfo.Role != UserRole.Owner.ToString())
3027
{

application/account-management/Core/Features/Users/Commands/DeleteUser.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,7 @@ public sealed class DeleteUserHandler(IUserRepository userRepository, IExecution
1515
{
1616
public async Task<Result> Handle(DeleteUserCommand command, CancellationToken cancellationToken)
1717
{
18-
if (executionContext.UserInfo.Id == command.Id)
19-
{
20-
return Result.Forbidden("You cannot delete yourself.");
21-
}
18+
if (executionContext.UserInfo.Id == command.Id) return Result.Forbidden("You cannot delete yourself.");
2219

2320
if (executionContext.UserInfo.Role != UserRole.Owner.ToString())
2421
{

application/account-management/Core/Features/Users/Commands/RemoveAvatar.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ public sealed class RemoveAvatarCommandHandler(IUserRepository userRepository, I
1414
public async Task<Result> Handle(RemoveAvatarCommand command, CancellationToken cancellationToken)
1515
{
1616
var user = await userRepository.GetLoggedInUserAsync(cancellationToken);
17-
if (user is null) return Result.BadRequest("User not found.");
1817

1918
user.RemoveAvatar();
2019
userRepository.Update(user);

application/account-management/Core/Features/Users/Commands/UpdateAvatar.cs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,12 @@ public UpdateAvatarValidator()
2424
}
2525
}
2626

27-
public sealed class UpdateAvatarHandler(
28-
IUserRepository userRepository,
29-
AvatarUpdater avatarUpdater,
30-
ITelemetryEventsCollector events
31-
) : IRequestHandler<UpdateAvatarCommand, Result>
27+
public sealed class UpdateAvatarHandler(IUserRepository userRepository, AvatarUpdater avatarUpdater, ITelemetryEventsCollector events)
28+
: IRequestHandler<UpdateAvatarCommand, Result>
3229
{
3330
public async Task<Result> Handle(UpdateAvatarCommand command, CancellationToken cancellationToken)
3431
{
3532
var user = await userRepository.GetLoggedInUserAsync(cancellationToken);
36-
if (user is null)
37-
{
38-
return Result.BadRequest("User not found.");
39-
}
4033

4134
if (await avatarUpdater.UpdateAvatar(user, false, command.ContentType, command.FileSteam, cancellationToken))
4235
{

0 commit comments

Comments
 (0)