-
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 events contract, endpoint, query and query handler * added get events endpoint tests * fixed domain to not use complex object as value object since its not supported to group by nested property (migration fix)
- Loading branch information
1 parent
3d6f21a
commit a0a746c
Showing
16 changed files
with
753 additions
and
43 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using MediatR; | ||
|
||
using Microsoft.AspNetCore.Mvc; | ||
|
||
using TeamUp.Api.Extensions; | ||
using TeamUp.Application.Events.GetEvents; | ||
using TeamUp.Contracts.Events; | ||
using TeamUp.Contracts.Teams; | ||
|
||
namespace TeamUp.Api.Endpoints.Events; | ||
|
||
public sealed class GetEventsEndpoint : IEndpointGroup | ||
{ | ||
public void MapEndpoints(RouteGroupBuilder group) | ||
{ | ||
group.MapGet("/", GetEventsAsync) | ||
.Produces<List<EventSlimResponse>>(StatusCodes.Status200OK) | ||
.ProducesProblem(StatusCodes.Status401Unauthorized) | ||
.ProducesProblem(StatusCodes.Status403Forbidden) | ||
.ProducesProblem(StatusCodes.Status404NotFound) | ||
.ProducesValidationProblem() | ||
.WithName(nameof(GetEventsEndpoint)) | ||
.MapToApiVersion(1); | ||
} | ||
|
||
public async Task<IResult> GetEventsAsync( | ||
[FromRoute] Guid teamId, | ||
[FromQuery] DateTime? fromUtc, | ||
[FromServices] ISender sender, | ||
HttpContext httpContext, | ||
CancellationToken ct) | ||
{ | ||
var query = new GetEventsQuery( | ||
httpContext.GetCurrentUserId(), | ||
TeamId.FromGuid(teamId), | ||
fromUtc | ||
); | ||
var response = await sender.Send(query, ct); | ||
return response.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
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,9 @@ | ||
using TeamUp.Application.Abstractions; | ||
using TeamUp.Common; | ||
using TeamUp.Contracts.Events; | ||
using TeamUp.Contracts.Teams; | ||
using TeamUp.Contracts.Users; | ||
|
||
namespace TeamUp.Application.Events.GetEvents; | ||
|
||
public sealed record GetEventsQuery(UserId InitiatorId, TeamId TeamId, DateTime? FromUtc = null) : IQuery<Result<List<EventSlimResponse>>>; |
64 changes: 64 additions & 0 deletions
64
src/TeamUp.Application/Events/GetEvents/GetEvetnsQueryHandlers.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,64 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
using TeamUp.Application.Abstractions; | ||
using TeamUp.Common; | ||
using TeamUp.Common.Abstractions; | ||
using TeamUp.Contracts.Events; | ||
using TeamUp.Domain.Aggregates.Teams; | ||
|
||
namespace TeamUp.Application.Events.GetEvents; | ||
|
||
internal sealed class GetEventsQueryHandlers : IQueryHandler<GetEventsQuery, Result<List<EventSlimResponse>>> | ||
{ | ||
private readonly IAppQueryContext _appQueryContext; | ||
private readonly IDateTimeProvider _dateTimeProvider; | ||
|
||
public GetEventsQueryHandlers(IAppQueryContext appQueryContext, IDateTimeProvider dateTimeProvider) | ||
{ | ||
_appQueryContext = appQueryContext; | ||
_dateTimeProvider = dateTimeProvider; | ||
} | ||
|
||
public async Task<Result<List<EventSlimResponse>>> Handle(GetEventsQuery query, CancellationToken ct) | ||
{ | ||
var from = query.FromUtc ?? _dateTimeProvider.UtcNow; | ||
var team = await _appQueryContext.Teams | ||
.Select(team => new | ||
{ | ||
team.Id, | ||
Events = _appQueryContext.Events | ||
.Where(e => e.TeamId == team.Id && e.ToUtc > from) | ||
.Include(e => e.EventResponses) | ||
.Select(e => new EventSlimResponse | ||
{ | ||
Id = e.Id, | ||
Description = e.Description, | ||
FromUtc = e.FromUtc, | ||
ToUtc = e.ToUtc, | ||
Status = e.Status, | ||
MeetTime = e.MeetTime, | ||
ReplyClosingTimeBeforeMeetTime = e.ReplyClosingTimeBeforeMeetTime, | ||
ReplyCount = e.EventResponses | ||
.GroupBy(er => er.ReplyType) | ||
.Select(x => new ReplyCountResponse | ||
{ | ||
Type = x.Key, | ||
Count = x.Count() | ||
}) | ||
.ToList(), | ||
EventType = team.EventTypes.First(et => et.Id == e.EventTypeId).Name | ||
}) | ||
.OrderBy(e => e.FromUtc) | ||
.ToList(), | ||
Initiator = team.Members | ||
.Select(member => member.UserId) | ||
.FirstOrDefault(id => id == query.InitiatorId) | ||
}) | ||
.FirstOrDefaultAsync(team => team.Id == query.TeamId, ct); | ||
|
||
return team | ||
.EnsureNotNull(TeamErrors.TeamNotFound) | ||
.EnsureNotNull(team => team.Initiator, TeamErrors.NotMemberOfTeam) | ||
.Then(team => team.Events); | ||
} | ||
} |
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,14 @@ | ||
namespace TeamUp.Contracts.Events; | ||
|
||
public sealed class EventSlimResponse | ||
{ | ||
public required EventId Id { get; init; } | ||
public required string EventType { get; init; } | ||
public required DateTime FromUtc { get; init; } | ||
public required DateTime ToUtc { get; init; } | ||
public required string Description { get; init; } | ||
public required EventStatus Status { get; init; } | ||
public required TimeSpan MeetTime { get; init; } | ||
public required TimeSpan ReplyClosingTimeBeforeMeetTime { get; init; } | ||
public required List<ReplyCountResponse> ReplyCount { 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace TeamUp.Contracts.Events; | ||
|
||
public readonly struct ReplyCountResponse | ||
{ | ||
public required ReplyType Type { get; init; } | ||
public required int Count { 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
Oops, something went wrong.