Skip to content

Commit

Permalink
Queries Fix (#21)
Browse files Browse the repository at this point in the history
* fixed selecting data in db queries
* refactored user profile query
  • Loading branch information
skrasekmichael authored Feb 22, 2024
1 parent 9efa463 commit f9cc511
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@

namespace TeamUp.Api.Endpoints.UserAccess;

public sealed class GetMyProfileEndpoint : IEndpointGroup
public sealed class GetUserDetailsEndpoint : IEndpointGroup
{
public void MapEndpoints(RouteGroupBuilder group)
{
group.MapGet("/my-profile", GetMyProfileAsync)
group.MapGet("/", GetUserDetailsAsync)
.Produces<UserResponse>(StatusCodes.Status200OK)
.ProducesProblem(StatusCodes.Status401Unauthorized)
.WithName(nameof(GetMyProfileEndpoint))
.WithName(nameof(GetUserDetailsEndpoint))
.MapToApiVersion(1)
.RequireAuthorization();
}

private async Task<IResult> GetMyProfileAsync(
private async Task<IResult> GetUserDetailsAsync(
[FromServices] ISender sender,
[FromServices] IHttpContextAccessor httpContextAccessor,
CancellationToken ct)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ private async Task<IResult> RegisterUserAsync(
var command = mapper.ToCommand(request);
var result = await sender.Send(command, ct);
return result.Match(
userId => TypedResults.Created(linkGenerator.GetPathByName(nameof(GetMyProfileEndpoint)), userId)
userId => TypedResults.Created(linkGenerator.GetPathByName(nameof(GetUserDetailsEndpoint)), userId)
);
}
}
2 changes: 1 addition & 1 deletion src/TeamUp.Api/Endpoints/UserAccessEndpointGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ public void MapEndpoints(RouteGroupBuilder group)
group.MapEndpoint<RegisterUserEndpoint>()
.MapEndpoint<ActivateAccountEndpoint>()
.MapEndpoint<LoginUserEndpoint>()
.MapEndpoint<GetMyProfileEndpoint>();
.MapEndpoint<GetUserDetailsEndpoint>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ public GetTeamInvitationsQueryHandler(IAppQueryContext appQueryContext)
public async Task<Result<List<TeamInvitationResponse>>> Handle(GetTeamInvitationsQuery request, CancellationToken ct)
{
var teamWithInitiator = await _appQueryContext.Teams
.Where(team => team.Id == request.TeamId)
.Select(team => new
{
Initiaotor = team.Members.FirstOrDefault(member => member.UserId == request.InitiatorId),
team.Id,
Initiaotor = team.Members
.Select(member => new { member.UserId, member.Role })
.FirstOrDefault(member => member.UserId == request.InitiatorId),
})
.FirstOrDefaultAsync(ct);
.FirstOrDefaultAsync(team => team.Id == request.TeamId, ct);

return await teamWithInitiator
.EnsureNotNull(TeamErrors.TeamNotFound)
Expand All @@ -33,10 +35,18 @@ public async Task<Result<List<TeamInvitationResponse>>> Handle(GetTeamInvitation
.ThenAsync(team =>
{
return _appQueryContext.Invitations
.Select(invitation => new
{
invitation.TeamId,
invitation.CreatedUtc,
_appQueryContext.Users
.Select(user => new { user.Id, user.Email })
.First(user => user.Id == invitation.RecipientId).Email,
})
.Where(invitation => invitation.TeamId == request.TeamId)
.Select(invitation => new TeamInvitationResponse
{
Email = _appQueryContext.Users.First(user => user.Id == invitation.RecipientId).Email,
Email = invitation.Email,
CreatedUtc = invitation.CreatedUtc
})
.ToListAsync(ct);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,22 @@ namespace TeamUp.Application.Users.GetUserDetail;
internal sealed class GetUserDetailsQueryHandler : IQueryHandler<GetUserDetailsQuery, Result<UserResponse>>
{
private readonly IAppQueryContext _queryContext;
private readonly UserMapper _mapper;

public GetUserDetailsQueryHandler(IAppQueryContext queryContext, UserMapper mapper)
public GetUserDetailsQueryHandler(IAppQueryContext queryContext)
{
_queryContext = queryContext;
_mapper = mapper;
}

public async Task<Result<UserResponse>> Handle(GetUserDetailsQuery request, CancellationToken ct)
{
var user = await _queryContext.Users
.Where(user => user.Id == request.UserId)
.Select(user => _mapper.ToResponse(user))
.Select(user => new UserResponse
{
Email = user.Email,
Name = user.Name,
Status = user.Status
})
.FirstOrDefaultAsync(ct);

return user.EnsureNotNull(UserErrors.UserNotFound);
Expand Down
3 changes: 0 additions & 3 deletions src/TeamUp.Application/Users/UserMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using TeamUp.Application.Users.Login;
using TeamUp.Application.Users.Register;
using TeamUp.Contracts.Users;
using TeamUp.Domain.Aggregates.Users;

namespace TeamUp.Application.Users;

Expand All @@ -13,6 +12,4 @@ public sealed partial class UserMapper
public partial RegisterUserCommand ToCommand(RegisterUserRequest request);

public partial LoginCommand ToCommand(LoginRequest request);

public partial UserResponse ToResponse(User user);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

namespace TeamUp.EndToEndTests.EndpointTests.UserAccess;

public sealed class GetMyProfileTests : BaseUserAccessTests
public sealed class GetUserDetailsTests : BaseUserAccessTests
{
public GetMyProfileTests(TeamApiWebApplicationFactory appFactory) : base(appFactory) { }
public GetUserDetailsTests(TeamApiWebApplicationFactory appFactory) : base(appFactory) { }

[Fact]
public async Task GetMyProfile_WhenUnauthenticated_Should_ResultInUnauthorized()
{
//arrange
//act
var response = await Client.GetAsync("/api/v1/users/my-profile");
var response = await Client.GetAsync("/api/v1/users");

//assert
response.Should().Be401Unauthorized();
Expand All @@ -32,7 +32,7 @@ await UseDbContextAsync(dbContext =>
Authenticate(user);

//act
var response = await Client.GetAsync("/api/v1/users/my-profile");
var response = await Client.GetAsync("/api/v1/users");

//assert
response.Should().Be200Ok();
Expand Down

0 comments on commit f9cc511

Please sign in to comment.