Added endpoint to add user in group

This commit is contained in:
2026-03-11 18:25:02 +01:00
parent 4f52093742
commit d6e6fbd70f
2 changed files with 49 additions and 1 deletions
@@ -0,0 +1,48 @@
using BeReadyBackend.Models;
using BeReadyBackend.Repositories;
using BeReadyBackend.Services;
using BeReadyBackend.Specifications.Groups;
using FastEndpoints;
namespace BeReadyBackend.Endpoints.Groups;
public class AddUserToGroupRequest
{
public int UserId { get; set; }
public int GroupId { get; set; }
}
public class AddUserToGroupEndpoint(UserService userService, UserGroupsRepository userGroupsRepository, AutoMapper.IMapper mapper) : Endpoint<AddUserToGroupRequest>
{
public override void Configure()
{
Post("/Groups/{@GroupId}/Users/{@UserId}/", x => new { x.GroupId, x.UserId });
}
public override async Task HandleAsync(AddUserToGroupRequest req, CancellationToken ct)
{
int userId = userService.GetUserIdFromToken();
UserGroup? member = await userGroupsRepository.SingleOrDefaultAsync(new GetUserInGroupByIdsSpec(req.GroupId, userId), ct);
if (member is null)
{
await Send.StringAsync("Vous n'êtes pas dans ce groupe", 400, cancellation: ct);
return;
}
UserGroup? userGroup = await userGroupsRepository.SingleOrDefaultAsync(new GetUserInGroupByIdsSpec(req.GroupId, req.UserId), ct);
if (userGroup is not null)
{
await Send.StringAsync("L'utilisateur fait déjà partie du groupe", 400, cancellation: ct);
return;
}
userGroup = mapper.Map<UserGroup>(req);
userGroup.Grade = "Member";
userGroup.Score = 0;
await userGroupsRepository.AddAsync(userGroup, ct);
await Send.NoContentAsync(ct);
}
}
@@ -24,7 +24,7 @@ public class CreateGroupEndpoint(
{
int userId = userService.GetUserIdFromToken();
Group? group = new();
Group group = new();
mapper.Map(req, group);
group.IsFinished = false;
group.CreationDate = DateTime.Now;