Added saving of pictures on BDD with base64

This commit is contained in:
2026-03-29 19:21:49 +01:00
parent 815cbb0fc2
commit 47265d3c9c
2 changed files with 29 additions and 7 deletions
@@ -11,7 +11,7 @@ namespace BeReadyBackend.Endpoints.Groups;
public class UserProofRequest public class UserProofRequest
{ {
public int GroupId { get; set; } public int GroupId { get; set; }
public string? Proof { get; set; } public IFormFile? Proof { get; set; }
} }
public class PatchGroupUserProofEndpoint(UserGroupsRepository userGroupsRepository, UserService userService, IHubContext<GroupHub> hubContext) : Endpoint<UserProofRequest> public class PatchGroupUserProofEndpoint(UserGroupsRepository userGroupsRepository, UserService userService, IHubContext<GroupHub> hubContext) : Endpoint<UserProofRequest>
@@ -44,10 +44,21 @@ public class PatchGroupUserProofEndpoint(UserGroupsRepository userGroupsReposito
return; return;
} }
member.Proof = req.Proof; if (req.Proof == null || req.Proof.Length == 0)
{
await Send.StringAsync("Photo non fournie", 400, cancellation: ct);
return;
}
// Encodage base64
using MemoryStream memoryStream = new();
await req.Proof.CopyToAsync(memoryStream, ct);
byte[] proofBytes = memoryStream.ToArray();
member.Proof = Convert.ToBase64String(proofBytes);
await userGroupsRepository.SaveChangesAsync(ct); await userGroupsRepository.SaveChangesAsync(ct);
await hubContext.Clients.Group($"group-{req.GroupId}").SendAsync("ReceiveProof", req.Proof, cancellationToken: ct); await hubContext.Clients.Group($"group-{req.GroupId}").SendAsync("ReceiveProof", member.Proof, cancellationToken: ct);
await Send.NoContentAsync(ct); await Send.NoContentAsync(ct);
} }
@@ -10,15 +10,14 @@ namespace BeReadyBackend.Endpoints.RandomChallenges;
public class RandomChallengeProofRequest public class RandomChallengeProofRequest
{ {
public int RandomChallengeId { get; set; } public int RandomChallengeId { get; set; }
public string? Proof { get; set; } public IFormFile? Proof { get; set; }
} }
public class PatchProofEndpoint( public class PatchProofEndpoint(
UsersRepository usersRepository, UsersRepository usersRepository,
UserRandomChallengesRepository userRandomChallengesRepository, UserRandomChallengesRepository userRandomChallengesRepository,
UserService userService, UserService userService,
RandomChallengesRepository randomChallengesRepository, RandomChallengesRepository randomChallengesRepository) : Endpoint<RandomChallengeProofRequest>
AutoMapper.IMapper mapper) : Endpoint<RandomChallengeProofRequest>
{ {
public override void Configure() public override void Configure()
{ {
@@ -63,7 +62,19 @@ public class PatchProofEndpoint(
return; return;
} }
mapper.Map(req, userRandomChallenge); if (req.Proof == null || req.Proof.Length == 0)
{
await Send.StringAsync("Photo non fournie", 400, cancellation: ct);
return;
}
// Encodage en base64
using MemoryStream memoryStream = new();
await req.Proof.CopyToAsync(memoryStream, ct);
byte[] proofBytes = memoryStream.ToArray();
userRandomChallenge.Proof = Convert.ToBase64String(proofBytes);
user.Score++; // 1pts bonus user.Score++; // 1pts bonus
await userRandomChallengesRepository.SaveChangesAsync(ct); await userRandomChallengesRepository.SaveChangesAsync(ct);