Fixed errors
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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<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);
|
||||
}
|
||||
}
|
||||
@@ -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<UserRoleRequest>
|
||||
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)
|
||||
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<GetGroupDetailsDto>(new GetGroupByIdSpec(req.GroupId), ct), ct);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user