Files
BeReadyBackend/BeReadyBackend/Endpoints/Groups/PatchGroupUserProofEndpoint.cs
T
2026-03-17 11:33:58 +01:00

54 lines
1.7 KiB
C#

using BeReadyBackend.Hubs;
using BeReadyBackend.Models;
using BeReadyBackend.Repositories;
using BeReadyBackend.Services;
using BeReadyBackend.Specifications.Groups;
using FastEndpoints;
using Microsoft.AspNetCore.SignalR;
namespace BeReadyBackend.Endpoints.Groups;
public class UserProofRequest
{
public int GroupId { get; set; }
public string? Proof { get; set; }
}
public class PatchGroupUserProofEndpoint(UserGroupsRepository userGroupsRepository, UserService userService, IHubContext<GroupHub> hubContext) : Endpoint<UserProofRequest>
{
public override void Configure()
{
Patch("/Groups/{@GroupId}/Users/Proof/", x => new { x.GroupId });
}
public override async Task HandleAsync(UserProofRequest req, CancellationToken ct)
{
int userId = userService.GetUserIdFromToken();
UserGroup? member = await userGroupsRepository.SingleOrDefaultAsync(new GetUserInGroupByIdsSpec(req.GroupId, userId), ct);
if (member is null)
{
await Send.NotFoundAsync(ct);
return;
}
if (member.Proof is not null)
{
await Send.StringAsync("Vous avez déjà déposé une photo", 400, cancellation: ct);
return;
}
if (member.Group != null && member.Group.IsFinished)
{
await Send.StringAsync("Ce défi est terminé", 400, cancellation: ct);
return;
}
member.Proof = req.Proof;
await userGroupsRepository.SaveChangesAsync(ct);
await hubContext.Clients.Group($"group-{req.GroupId}").SendAsync("ReceiveProof", req.Proof, cancellationToken: ct);
await Send.NoContentAsync(ct);
}
}