-
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.
- added get my invitations endpoint, query and query handler - added get my invitations endpoint tests - fixed get team invitations
- Loading branch information
1 parent
cd3c692
commit c674d77
Showing
10 changed files
with
185 additions
and
16 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
src/TeamUp.Api/Endpoints/Invitations/GetMyInvitationsEndpoint.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,31 @@ | ||
using MediatR; | ||
|
||
using Microsoft.AspNetCore.Mvc; | ||
|
||
using TeamUp.Api.Extensions; | ||
using TeamUp.Application.Invitations.GetMyInvitations; | ||
using TeamUp.Contracts.Invitations; | ||
|
||
namespace TeamUp.Api.Endpoints.Invitations; | ||
|
||
public sealed class GetMyInvitationsEndpoint : IEndpointGroup | ||
{ | ||
public void MapEndpoints(RouteGroupBuilder group) | ||
{ | ||
group.MapGet("/", GetTeamInvitationsAsync) | ||
.Produces<List<InvitationResponse>>(StatusCodes.Status200OK) | ||
.ProducesProblem(StatusCodes.Status401Unauthorized) | ||
.WithName(nameof(GetMyInvitationsEndpoint)) | ||
.MapToApiVersion(1); | ||
} | ||
|
||
private async Task<IResult> GetTeamInvitationsAsync( | ||
[FromServices] ISender sender, | ||
HttpContext httpContext, | ||
CancellationToken ct) | ||
{ | ||
var query = new GetMyInvitationsQuery(httpContext.GetCurrentUserId()); | ||
var result = await sender.Send(query, ct); | ||
return result.Match(TypedResults.Ok); | ||
} | ||
} |
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
8 changes: 8 additions & 0 deletions
8
src/TeamUp.Application/Invitations/GetMyInvitations/GetMyInvitationsQuery.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,8 @@ | ||
using TeamUp.Application.Abstractions; | ||
using TeamUp.Common; | ||
using TeamUp.Contracts.Invitations; | ||
using TeamUp.Contracts.Users; | ||
|
||
namespace TeamUp.Application.Invitations.GetMyInvitations; | ||
|
||
public sealed record GetMyInvitationsQuery(UserId InitiatorId) : IQuery<Result<List<InvitationResponse>>>; |
30 changes: 30 additions & 0 deletions
30
src/TeamUp.Application/Invitations/GetMyInvitations/GetMyInvitationsQueryHandler.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,30 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
using TeamUp.Application.Abstractions; | ||
using TeamUp.Common; | ||
using TeamUp.Contracts.Invitations; | ||
|
||
namespace TeamUp.Application.Invitations.GetMyInvitations; | ||
|
||
internal sealed class GetMyInvitationsQueryHandler : IQueryHandler<GetMyInvitationsQuery, Result<List<InvitationResponse>>> | ||
{ | ||
private readonly IAppQueryContext _appQueryContext; | ||
|
||
public GetMyInvitationsQueryHandler(IAppQueryContext appQueryContext) | ||
{ | ||
_appQueryContext = appQueryContext; | ||
} | ||
|
||
public async Task<Result<List<InvitationResponse>>> Handle(GetMyInvitationsQuery request, CancellationToken ct) | ||
{ | ||
return await _appQueryContext.Invitations | ||
.Where(invitation => invitation.RecipientId == request.InitiatorId) | ||
.Select(invitation => new InvitationResponse | ||
{ | ||
Id = invitation.Id, | ||
TeamName = _appQueryContext.Teams.First(team => team.Id == invitation.TeamId).Name, | ||
CreatedUtc = invitation.CreatedUtc | ||
}) | ||
.ToListAsync(ct); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using TeamUp.Contracts.Teams; | ||
|
||
namespace TeamUp.Contracts.Invitations; | ||
|
||
public sealed class InvitationResponse | ||
{ | ||
public required InvitationId Id { get; init; } | ||
public required string TeamName { get; init; } | ||
public required DateTime CreatedUtc { get; init; } | ||
} |
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
83 changes: 83 additions & 0 deletions
83
tests/TeamUp.EndToEndTests/EndpointTests/Invitations/GetMyInvitationsTests.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,83 @@ | ||
using TeamUp.Contracts.Invitations; | ||
|
||
namespace TeamUp.EndToEndTests.EndpointTests.Invitations; | ||
|
||
public sealed class GetMyInvitationsTests : BaseInvitationTests | ||
{ | ||
public GetMyInvitationsTests(TeamApiWebApplicationFactory appFactory) : base(appFactory) { } | ||
|
||
public const string URL = "/api/v1/invitations"; | ||
|
||
[Fact] | ||
public async Task GetMyInvitations_Should_ReturnListOfInvitations() | ||
{ | ||
//arrange | ||
var owner = UserGenerator.ActivatedUser.Generate(); | ||
var initiatorUser = UserGenerator.ActivatedUser.Generate(); | ||
var members = UserGenerator.ActivatedUser.Generate(19); | ||
var teams = new List<Team> | ||
{ | ||
TeamGenerator.GenerateTeamWith(owner, members), | ||
TeamGenerator.GenerateTeamWith(owner, members), | ||
TeamGenerator.GenerateTeamWith(owner, members) | ||
}; | ||
|
||
//need to remove milliseconds as there is slight shift when saving to database | ||
var utcNow = new DateTime(DateTime.UtcNow.Ticks / TimeSpan.TicksPerSecond * TimeSpan.TicksPerSecond, DateTimeKind.Utc); | ||
var invitations = InvitationGenerator.GenerateUserInvitations(initiatorUser.Id, utcNow, teams); | ||
|
||
await UseDbContextAsync(dbContext => | ||
{ | ||
dbContext.Users.AddRange([owner, initiatorUser]); | ||
dbContext.Users.AddRange(members); | ||
dbContext.Teams.AddRange(teams); | ||
dbContext.Invitations.AddRange(invitations); | ||
return dbContext.SaveChangesAsync(); | ||
}); | ||
|
||
Authenticate(initiatorUser); | ||
|
||
//act | ||
var response = await Client.GetAsync(URL); | ||
|
||
//assert | ||
response.Should().Be200Ok(); | ||
|
||
var userInvitations = await response.ReadFromJsonAsync<List<InvitationResponse>>(); | ||
invitations.Should().BeEquivalentTo(userInvitations, o => o.ExcludingMissingMembers()); | ||
} | ||
|
||
[Fact] | ||
public async Task GetMyInvitations_WhenNotInvited_Should_ReturnEmptyList() | ||
{ | ||
//arrange | ||
var owner = UserGenerator.ActivatedUser.Generate(); | ||
var initiatorUser = UserGenerator.ActivatedUser.Generate(); | ||
var members = UserGenerator.ActivatedUser.Generate(19); | ||
var teams = new List<Team> | ||
{ | ||
TeamGenerator.GenerateTeamWith(owner, members), | ||
TeamGenerator.GenerateTeamWith(owner, members), | ||
TeamGenerator.GenerateTeamWith(owner, members) | ||
}; | ||
|
||
await UseDbContextAsync(dbContext => | ||
{ | ||
dbContext.Users.AddRange([owner, initiatorUser]); | ||
dbContext.Users.AddRange(members); | ||
dbContext.Teams.AddRange(teams); | ||
return dbContext.SaveChangesAsync(); | ||
}); | ||
|
||
Authenticate(initiatorUser); | ||
|
||
//act | ||
var response = await Client.GetAsync(URL); | ||
|
||
//assert | ||
response.Should().Be200Ok(); | ||
|
||
var invitations = await response.ReadFromJsonAsync<List<InvitationResponse>>(); | ||
invitations.Should().BeEmpty(); | ||
} | ||
} |
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