-
Notifications
You must be signed in to change notification settings - Fork 0
feat: implement toggle behavior for user interests #26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ahmed12348
wants to merge
4
commits into
feat/solution-refactor-plan
Choose a base branch
from
feat/user-interests
base: feat/solution-refactor-plan
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e49876e
feat: implement toggle behavior for user interests
ahmed12348 ed2e61f
feat: add user interest toggle endpoint
ahmed12348 fce5967
delete Toggle Interests user
ahmed12348 f7864f5
UpsertUserInterest update hte unnessesary wrote in db
ahmed12348 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
36 changes: 36 additions & 0 deletions
36
backend/src/CCE.Api.External/Endpoints/UserInterestEndpoints.cs
This file contains hidden or 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,36 @@ | ||
| using CCE.Api.Common.Extensions; | ||
| using CCE.Application.Common.Interfaces; | ||
| using CCE.Application.Identity.Public.Commands.UserInterest; | ||
| using MediatR; | ||
| using Microsoft.AspNetCore.Builder; | ||
| using Microsoft.AspNetCore.Http; | ||
| using Microsoft.AspNetCore.Routing; | ||
|
|
||
| namespace CCE.Api.External.Endpoints; | ||
|
|
||
| public static class UserInterestEndpoints | ||
| { | ||
| public static IEndpointRouteBuilder MapUserInterestEndpoints(this IEndpointRouteBuilder app) | ||
| { | ||
| var me = app.MapGroup("/api/me").WithTags("User Interests").RequireAuthorization(); | ||
|
|
||
| me.MapPatch("/interests", async ( | ||
| UpsertUserInterestRequest body, | ||
| ICurrentUserAccessor currentUser, | ||
| IMediator mediator, | ||
| CancellationToken ct) => | ||
| { | ||
| var userId = currentUser.GetUserId() ?? System.Guid.Empty; | ||
| if (userId == System.Guid.Empty) return Results.Unauthorized(); | ||
|
|
||
| var result = await mediator.Send( | ||
| new UpsertUserInterestCommand(userId, body.Interests), ct).ConfigureAwait(false); | ||
| return result.ToHttpResult(); | ||
| }) | ||
| .WithName("UpsertUserInterest"); | ||
|
|
||
| return app; | ||
| } | ||
| } | ||
|
|
||
| public sealed record UpsertUserInterestRequest(IReadOnlyList<string> Interests); |
This file contains hidden or 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
8 changes: 8 additions & 0 deletions
8
...nd/src/CCE.Application/Identity/Public/Commands/UserInterest/UpsertUserInterestCommand.cs
This file contains hidden or 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,8 @@ | ||
| using CCE.Application.Common; | ||
| using MediatR; | ||
|
|
||
| namespace CCE.Application.Identity.Public.Commands.UserInterest; | ||
|
|
||
| public sealed record UpsertUserInterestCommand( | ||
| System.Guid UserId, | ||
| IReadOnlyList<string> Interests) : IRequest<Response<UpsertUserInterestResult>>; |
67 changes: 67 additions & 0 deletions
67
...CCE.Application/Identity/Public/Commands/UserInterest/UpsertUserInterestCommandHandler.cs
This file contains hidden or 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,67 @@ | ||
| using System.Linq; | ||
| using CCE.Application.Common; | ||
| using CCE.Application.Common.Interfaces; | ||
| using CCE.Application.Messages; | ||
| using MediatR; | ||
|
|
||
| namespace CCE.Application.Identity.Public.Commands.UserInterest; | ||
|
|
||
| public sealed class UpsertUserInterestCommandHandler | ||
| : IRequestHandler<UpsertUserInterestCommand, Response<UpsertUserInterestResult>> | ||
| { | ||
| private readonly IUserProfileRepository _service; | ||
| private readonly ICceDbContext _db; | ||
| private readonly MessageFactory _msg; | ||
|
|
||
| public UpsertUserInterestCommandHandler( | ||
| IUserProfileRepository service, | ||
| ICceDbContext db, | ||
| MessageFactory msg) | ||
| { | ||
| _service = service; | ||
| _db = db; | ||
| _msg = msg; | ||
| } | ||
|
|
||
| public async Task<Response<UpsertUserInterestResult>> Handle( | ||
| UpsertUserInterestCommand request, | ||
| CancellationToken cancellationToken) | ||
| { | ||
| var user = await _service.FindAsync(request.UserId, cancellationToken).ConfigureAwait(false); | ||
| if (user is null) | ||
| return _msg.UserNotFound<UpsertUserInterestResult>(); | ||
|
|
||
| var oldInterests = user.Interests.ToList(); | ||
| var rawList = request.Interests ?? System.Array.Empty<string>(); | ||
|
|
||
| var normalizedNew = rawList | ||
| .Select(static s => s?.Trim() ?? string.Empty) | ||
| .Where(static s => s.Length > 0) | ||
| .Distinct(StringComparer.OrdinalIgnoreCase) | ||
| .ToList(); | ||
|
|
||
| var oldSet = new HashSet<string>(oldInterests, StringComparer.OrdinalIgnoreCase); | ||
| var newSet = new HashSet<string>(normalizedNew, StringComparer.OrdinalIgnoreCase); | ||
|
|
||
| if (oldSet.SetEquals(newSet)) | ||
| { | ||
| return _msg.InterestUpserted(new UpsertUserInterestResult( | ||
| user.Interests, | ||
| System.Array.Empty<string>(), | ||
| System.Array.Empty<string>())); | ||
| } | ||
|
|
||
| user.UpdateInterests(normalizedNew); | ||
|
|
||
| var added = normalizedNew.Except(oldInterests, StringComparer.OrdinalIgnoreCase).ToList(); | ||
| var removed = oldInterests.Except(normalizedNew, StringComparer.OrdinalIgnoreCase).ToList(); | ||
|
|
||
| _service.Update(user); | ||
| await _db.SaveChangesAsync(cancellationToken).ConfigureAwait(false); | ||
|
|
||
| return _msg.InterestUpserted(new UpsertUserInterestResult( | ||
| user.Interests, | ||
| added, | ||
| removed)); | ||
| } | ||
| } | ||
6 changes: 6 additions & 0 deletions
6
...end/src/CCE.Application/Identity/Public/Commands/UserInterest/UpsertUserInterestResult.cs
This file contains hidden or 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,6 @@ | ||
| namespace CCE.Application.Identity.Public.Commands.UserInterest; | ||
|
|
||
| public sealed record UpsertUserInterestResult( | ||
| IReadOnlyList<string> Interests, | ||
| IReadOnlyList<string> Added, | ||
| IReadOnlyList<string> Removed); |
This file contains hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use StringComparer.OrdinalIgnoreCase in Except() to avoid false add/remove detection caused by casing differences.
skipping _service.Update(user) and SaveChangesAsync() when nothing changed can avoid unnecessary db writes.