-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Two HTTP endpoints have been added, allowing user metadata to be created and deleted. In addition, metadata validation has been implemented in UserCommandValidator. Async methods for user services have also been renamed to follow the standard convention, and a method for updating external metadata is added in the UserService class.
- Loading branch information
1 parent
076a8eb
commit f49e57a
Showing
13 changed files
with
252 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
...e.Backend.Application/Features/Http/Users/Metadata/Upsert/UpsertMetadataCommandHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using MediatR; | ||
using Serilog; | ||
using Sharecode.Backend.Application.Client; | ||
using Sharecode.Backend.Application.Data; | ||
using Sharecode.Backend.Application.Exceptions; | ||
using Sharecode.Backend.Application.Service; | ||
using Sharecode.Backend.Domain.Entity.Profile; | ||
using Sharecode.Backend.Domain.Exceptions; | ||
using Sharecode.Backend.Utilities.RedisCache; | ||
|
||
namespace Sharecode.Backend.Application.Features.Http.Users.Metadata.Upsert; | ||
|
||
public class UpsertMetadataCommandHandler(ILogger logger, IHttpClientContext context, IUserService userService, IUnitOfWork unitOfWork) : IRequestHandler<UpsertUserMetadataCommand, UpsertUserMetadataResponse> | ||
{ | ||
public async Task<UpsertUserMetadataResponse> Handle(UpsertUserMetadataCommand request, CancellationToken cancellationToken) | ||
{ | ||
var requestingUserIdRaw = await context.GetUserIdentifierAsync(); | ||
if(!requestingUserIdRaw.HasValue) | ||
throw new NoAccessException($"{request.UserId.ToString()}/metadata", Guid.Empty, typeof(User)); | ||
|
||
var requestingUserId = requestingUserIdRaw.Value; | ||
if (requestingUserId != request.UserId) | ||
{ | ||
if (!await context.HasPermissionAsync(Permissions.UpdateUserOtherAdmin, cancellationToken)) | ||
{ | ||
throw new NotEnoughPermissionException("Update user metadata"); | ||
} | ||
} | ||
|
||
if (!request.MetaDictionary.Any()) | ||
{ | ||
return UpsertUserMetadataResponse.Empty; | ||
} | ||
|
||
var oldValues = await userService.UpdateExternalMetadataAsync(request.UserId, request.MetaDictionary, cancellationToken); | ||
context.AddCacheKeyToInvalidate(CacheModules.UserMetadata, request.UserId.ToString()); | ||
await unitOfWork.CommitAsync(cancellationToken); | ||
return UpsertUserMetadataResponse.From(oldValues); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...code.Backend.Application/Features/Http/Users/Metadata/Upsert/UpsertUserMetadataCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using Sharecode.Backend.Application.Base; | ||
|
||
namespace Sharecode.Backend.Application.Features.Http.Users.Metadata.Upsert; | ||
|
||
public class UpsertUserMetadataCommand : IAppRequest<UpsertUserMetadataResponse> | ||
{ | ||
public Guid UserId { get; set; } | ||
public Dictionary<string, object> MetaDictionary = new(); | ||
} |
22 changes: 22 additions & 0 deletions
22
...ode.Backend.Application/Features/Http/Users/Metadata/Upsert/UpsertUserMetadataResponse.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
namespace Sharecode.Backend.Application.Features.Http.Users.Metadata.Upsert; | ||
|
||
public class UpsertUserMetadataResponse | ||
{ | ||
public Dictionary<string, object?> OldValues = new(); | ||
|
||
private UpsertUserMetadataResponse(Dictionary<string, object?> oldValues) | ||
{ | ||
OldValues = oldValues; | ||
} | ||
|
||
private UpsertUserMetadataResponse() | ||
{ | ||
} | ||
|
||
public static UpsertUserMetadataResponse From(Dictionary<string, object?> oldValues) | ||
{ | ||
return new UpsertUserMetadataResponse(oldValues); | ||
} | ||
|
||
public static UpsertUserMetadataResponse Empty => new(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters