From 47265d3c9cabad9fa0e5e4fe30e01b0c60fc1909 Mon Sep 17 00:00:00 2001 From: sanchezvem Date: Sun, 29 Mar 2026 19:21:49 +0100 Subject: [PATCH] Added saving of pictures on BDD with base64 --- .../Groups/PatchGroupUserProofEndpoint.cs | 17 ++++++++++++++--- .../RandomChallenges/PatchProofEndpoint.cs | 19 +++++++++++++++---- 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/BeReadyBackend/Endpoints/Groups/PatchGroupUserProofEndpoint.cs b/BeReadyBackend/Endpoints/Groups/PatchGroupUserProofEndpoint.cs index 886be20..d945e10 100644 --- a/BeReadyBackend/Endpoints/Groups/PatchGroupUserProofEndpoint.cs +++ b/BeReadyBackend/Endpoints/Groups/PatchGroupUserProofEndpoint.cs @@ -11,7 +11,7 @@ namespace BeReadyBackend.Endpoints.Groups; public class UserProofRequest { public int GroupId { get; set; } - public string? Proof { get; set; } + public IFormFile? Proof { get; set; } } public class PatchGroupUserProofEndpoint(UserGroupsRepository userGroupsRepository, UserService userService, IHubContext hubContext) : Endpoint @@ -44,10 +44,21 @@ public class PatchGroupUserProofEndpoint(UserGroupsRepository userGroupsReposito 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 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); } diff --git a/BeReadyBackend/Endpoints/RandomChallenges/PatchProofEndpoint.cs b/BeReadyBackend/Endpoints/RandomChallenges/PatchProofEndpoint.cs index 0d2709c..79b9a69 100644 --- a/BeReadyBackend/Endpoints/RandomChallenges/PatchProofEndpoint.cs +++ b/BeReadyBackend/Endpoints/RandomChallenges/PatchProofEndpoint.cs @@ -10,15 +10,14 @@ namespace BeReadyBackend.Endpoints.RandomChallenges; public class RandomChallengeProofRequest { public int RandomChallengeId { get; set; } - public string? Proof { get; set; } + public IFormFile? Proof { get; set; } } public class PatchProofEndpoint( UsersRepository usersRepository, UserRandomChallengesRepository userRandomChallengesRepository, UserService userService, - RandomChallengesRepository randomChallengesRepository, - AutoMapper.IMapper mapper) : Endpoint + RandomChallengesRepository randomChallengesRepository) : Endpoint { public override void Configure() { @@ -63,7 +62,19 @@ public class PatchProofEndpoint( 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 await userRandomChallengesRepository.SaveChangesAsync(ct);