Files
BeReadyBackend/BeReadyBackend/Endpoints/Groups/PatchGroupUserRoleEndpoint.cs
T
2026-04-26 16:03:56 +01:00

47 lines
1.7 KiB
C#

using BeReadyBackend.DTO.Groups;
using BeReadyBackend.Models;
using BeReadyBackend.Repositories;
using BeReadyBackend.Services;
using BeReadyBackend.Specifications.Groups;
using FastEndpoints;
namespace BeReadyBackend.Endpoints.Groups;
public class UserRoleRequest
{
public int GroupId { get; set; }
public int UserId { get; set; }
}
public class PatchGroupUserRoleEndpoint(UserGroupsRepository userGroupsRepository, UserService userService, GroupsRepository groupsRepository)
: Endpoint<UserRoleRequest, GetGroupDetailsDto>
{
public override void Configure()
{
Patch("/Groups/{@GroupId}/Users/{@UserId}/Role/", x => new { x.GroupId, x.UserId });
Description(x => x.Accepts<UserRoleRequest>());
}
public override async Task HandleAsync(UserRoleRequest req, CancellationToken ct)
{
int userId = userService.GetUserIdFromToken();
UserGroup? member = await userGroupsRepository.SingleOrDefaultAsync(new GetUserInGroupByIdsSpec(req.GroupId, req.UserId), ct);
UserGroup? user = await userGroupsRepository.SingleOrDefaultAsync(new GetUserInGroupByIdsSpec(req.GroupId, userId), ct);
if (member is null || user is null)
{
await Send.NotFoundAsync(ct);
return;
}
if (user.Grade != "Admin")
{
await Send.StringAsync("Vous n'avez pas les droits pour changer le rôle de ce membre", 400, cancellation: ct);
return;
}
member.Grade = "Admin";
await userGroupsRepository.SaveChangesAsync(ct);
await Send.OkAsync(await groupsRepository.ProjectToSingleAsync<GetGroupDetailsDto>(new GetGroupByIdSpec(req.GroupId), ct), ct);
}
}