Added endpoint to add user in group
This commit is contained in:
@@ -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();
|
int userId = userService.GetUserIdFromToken();
|
||||||
|
|
||||||
Group? group = new();
|
Group group = new();
|
||||||
mapper.Map(req, group);
|
mapper.Map(req, group);
|
||||||
group.IsFinished = false;
|
group.IsFinished = false;
|
||||||
group.CreationDate = DateTime.Now;
|
group.CreationDate = DateTime.Now;
|
||||||
|
|||||||
Reference in New Issue
Block a user