diff --git a/BeReadyBackend/Endpoints/Groups/DeleteUserFromGroupEndpoint.cs b/BeReadyBackend/Endpoints/Groups/DeleteUserFromGroupEndpoint.cs index fc507d0..2cf2add 100644 --- a/BeReadyBackend/Endpoints/Groups/DeleteUserFromGroupEndpoint.cs +++ b/BeReadyBackend/Endpoints/Groups/DeleteUserFromGroupEndpoint.cs @@ -24,14 +24,15 @@ public class DeleteUserFromGroupEndpoint(UserGroupsRepository userGroupsReposito { 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) + if (member is null || user is null) { await Send.NotFoundAsync(ct); return; } - if (member.Grade != "Admin" && userId != req.UserId) + if (user.Grade != "Admin") { await Send.StringAsync("Vous n'avez pas les droits pour supprimer ce membre du groupe", 400, cancellation: ct); return; diff --git a/BeReadyBackend/Endpoints/Groups/LeaveGroupEndpoint.cs b/BeReadyBackend/Endpoints/Groups/LeaveGroupEndpoint.cs new file mode 100644 index 0000000..70a6f4e --- /dev/null +++ b/BeReadyBackend/Endpoints/Groups/LeaveGroupEndpoint.cs @@ -0,0 +1,37 @@ +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 +{ + 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); + } +} \ No newline at end of file diff --git a/BeReadyBackend/Endpoints/Groups/PatchGroupUserRoleEndpoint.cs b/BeReadyBackend/Endpoints/Groups/PatchGroupUserRoleEndpoint.cs index 7ea8212..b1a4597 100644 --- a/BeReadyBackend/Endpoints/Groups/PatchGroupUserRoleEndpoint.cs +++ b/BeReadyBackend/Endpoints/Groups/PatchGroupUserRoleEndpoint.cs @@ -1,4 +1,5 @@ -using BeReadyBackend.Models; +using BeReadyBackend.DTO.Groups; +using BeReadyBackend.Models; using BeReadyBackend.Repositories; using BeReadyBackend.Services; using BeReadyBackend.Specifications.Groups; @@ -12,25 +13,28 @@ public class UserRoleRequest public int UserId { get; set; } } -public class PatchGroupUserRoleEndpoint(UserGroupsRepository userGroupsRepository, UserService userService) : Endpoint +public class PatchGroupUserRoleEndpoint(UserGroupsRepository userGroupsRepository, UserService userService, GroupsRepository groupsRepository) + : Endpoint { public override void Configure() { Patch("/Groups/{@GroupId}/Users/{@UserId}/Role/", x => new { x.GroupId, x.UserId }); + Description(x => x.Accepts()); } 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) + if (member is null || user is null) { await Send.NotFoundAsync(ct); return; } - if (member.Grade != "Admin" && userId != req.UserId) + 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; @@ -38,6 +42,6 @@ public class PatchGroupUserRoleEndpoint(UserGroupsRepository userGroupsRepositor member.Grade = "Admin"; await userGroupsRepository.SaveChangesAsync(ct); - await Send.NoContentAsync(ct); + await Send.OkAsync(await groupsRepository.ProjectToSingleAsync(new GetGroupByIdSpec(req.GroupId), ct), ct); } } \ No newline at end of file