37 lines
1.2 KiB
C#
37 lines
1.2 KiB
C#
using BeReadyBackend.Models;
|
|
using BeReadyBackend.Repositories;
|
|
using BeReadyBackend.Services;
|
|
using BeReadyBackend.Specifications.Groups;
|
|
using FastEndpoints;
|
|
using Group = FastEndpoints.Group;
|
|
|
|
namespace BeReadyBackend.Endpoints.Groups;
|
|
|
|
public class GroupLeavedRequest
|
|
{
|
|
public int Id { get; set; }
|
|
}
|
|
|
|
public class LeaveGroupEndpoint(UserService userService, GroupsRepository groupsRepository, UserGroupsRepository userGroupsRepository) : Endpoint<GroupLeavedRequest>
|
|
{
|
|
public override void Configure()
|
|
{
|
|
Delete("/Groups/{@Id}/User", x => new { x.Id });
|
|
}
|
|
|
|
public override async Task HandleAsync(GroupLeavedRequest req, CancellationToken ct)
|
|
{
|
|
int userId = userService.GetUserIdFromToken();
|
|
Models.Group? group = await groupsRepository.SingleOrDefaultAsync(new GetGroupByIdSpec(req.Id), ct);
|
|
UserGroup? user = await userGroupsRepository.SingleOrDefaultAsync(new GetUserInGroupByIdsSpec(req.Id, userId), ct);
|
|
|
|
if (group is null || user is null)
|
|
{
|
|
await Send.NotFoundAsync(ct);
|
|
return;
|
|
}
|
|
|
|
await userGroupsRepository.DeleteAsync(user, ct);
|
|
await Send.NoContentAsync(ct);
|
|
}
|
|
} |