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 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>
@@ -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);
}
@@ -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<RandomChallengeProofRequest>
RandomChallengesRepository randomChallengesRepository) : Endpoint<RandomChallengeProofRequest>
{
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);