From a701909d9deef7935a253b4f354929dd85effc0a Mon Sep 17 00:00:00 2001 From: sanchezvem Date: Sat, 21 Feb 2026 21:58:43 +0100 Subject: [PATCH] Created endpoints to see all challenges and proofs of the user in her personnal page --- .../DTO/Users/GetUserChallengeDto.cs | 4 +- .../Users/GetAllUserChallengesEndpoint.cs | 37 +++++++++++++++++++ .../Users/GetAllUserProofsEndpoint.cs | 37 +++++++++++++++++++ .../Endpoints/Users/GetUserDetailsEndpoint.cs | 31 ++++++++++++++++ .../Endpoints/Users/GetUserEndpoint.cs | 34 +++++++++++++++++ .../MappingProfiles/EntityToDtoMappings.cs | 12 +++++- .../Users/GetProofOrChallengeByUserIdSpec.cs | 17 +++++++++ .../Specifications/Users/GetUserByIdSpec.cs | 2 +- 8 files changed, 170 insertions(+), 4 deletions(-) create mode 100644 BeReadyBackend/Endpoints/Users/GetAllUserChallengesEndpoint.cs create mode 100644 BeReadyBackend/Endpoints/Users/GetAllUserProofsEndpoint.cs create mode 100644 BeReadyBackend/Endpoints/Users/GetUserDetailsEndpoint.cs create mode 100644 BeReadyBackend/Endpoints/Users/GetUserEndpoint.cs create mode 100644 BeReadyBackend/Specifications/Users/GetProofOrChallengeByUserIdSpec.cs diff --git a/BeReadyBackend/DTO/Users/GetUserChallengeDto.cs b/BeReadyBackend/DTO/Users/GetUserChallengeDto.cs index a69e801..e7beb1e 100644 --- a/BeReadyBackend/DTO/Users/GetUserChallengeDto.cs +++ b/BeReadyBackend/DTO/Users/GetUserChallengeDto.cs @@ -2,5 +2,7 @@ public class GetUserChallengeDto { - + public string? ChallengeTitle { get; set; } + public string? ChallengeDescription { get; set; } + public int ChallengeDuration { get; set; } } \ No newline at end of file diff --git a/BeReadyBackend/Endpoints/Users/GetAllUserChallengesEndpoint.cs b/BeReadyBackend/Endpoints/Users/GetAllUserChallengesEndpoint.cs new file mode 100644 index 0000000..ead750b --- /dev/null +++ b/BeReadyBackend/Endpoints/Users/GetAllUserChallengesEndpoint.cs @@ -0,0 +1,37 @@ +using BeReadyBackend.DTO.Users; +using BeReadyBackend.Models; +using BeReadyBackend.Repositories; +using BeReadyBackend.Services; +using BeReadyBackend.Specifications.Users; +using FastEndpoints; + +namespace BeReadyBackend.Endpoints.Users; + +public class GetAllUserChallengesEndpoint(UsersRepository usersRepository, UserService userService, AutoMapper.IMapper mapper) : EndpointWithoutRequest> +{ + public override void Configure() + { + Get("/Users/Challenges/"); + } + + public override async Task HandleAsync(CancellationToken ct) + { + int userId = userService.GetUserIdFromToken(); + + User? user = await usersRepository.SingleOrDefaultAsync(new GetProofOrChallengeByUserIdSpec(userId), ct); + + if (user is null) + { + await Send.NotFoundAsync(ct); + return; + } + + List challenges = []; + if (user.UserRandomChallenges is not null) + challenges.AddRange(user.UserRandomChallenges.Select(x => mapper.Map(x.RandomChallenge))); + if (user.UserGroups is not null) + challenges.AddRange(user.UserGroups.Select(x => mapper.Map(x.Group))); + + await Send.OkAsync(challenges, ct); + } +} \ No newline at end of file diff --git a/BeReadyBackend/Endpoints/Users/GetAllUserProofsEndpoint.cs b/BeReadyBackend/Endpoints/Users/GetAllUserProofsEndpoint.cs new file mode 100644 index 0000000..93fdc64 --- /dev/null +++ b/BeReadyBackend/Endpoints/Users/GetAllUserProofsEndpoint.cs @@ -0,0 +1,37 @@ +using BeReadyBackend.DTO.Users; +using BeReadyBackend.Models; +using BeReadyBackend.Repositories; +using BeReadyBackend.Services; +using BeReadyBackend.Specifications.Users; +using FastEndpoints; + +namespace BeReadyBackend.Endpoints.Users; + +public class GetAllUserProofsEndpoint(UsersRepository usersRepository, UserService userService, AutoMapper.IMapper mapper) : EndpointWithoutRequest> +{ + public override void Configure() + { + Get("/Users/Proofs/"); + } + + public override async Task HandleAsync(CancellationToken ct) + { + int userId = userService.GetUserIdFromToken(); + + User? user = await usersRepository.SingleOrDefaultAsync(new GetProofOrChallengeByUserIdSpec(userId), ct); + + if (user is null) + { + await Send.NotFoundAsync(ct); + return; + } + + List proofs = []; + if (user.UserRandomChallenges is not null) + proofs.AddRange(user.UserRandomChallenges.Select(x => mapper.Map(x.RandomChallenge))); + if (user.UserGroups is not null) + proofs.AddRange(user.UserGroups.Select(x => mapper.Map(x.Group))); + + await Send.OkAsync(proofs, ct); + } +} \ No newline at end of file diff --git a/BeReadyBackend/Endpoints/Users/GetUserDetailsEndpoint.cs b/BeReadyBackend/Endpoints/Users/GetUserDetailsEndpoint.cs new file mode 100644 index 0000000..6663b7a --- /dev/null +++ b/BeReadyBackend/Endpoints/Users/GetUserDetailsEndpoint.cs @@ -0,0 +1,31 @@ +using BeReadyBackend.DTO.Users; +using BeReadyBackend.Models; +using BeReadyBackend.Repositories; +using BeReadyBackend.Services; +using BeReadyBackend.Specifications.Users; +using FastEndpoints; + +namespace BeReadyBackend.Endpoints.Users; + +public class GetUserDetailsEndpoint(UsersRepository usersRepository, UserService userService, AutoMapper.IMapper mapper) : EndpointWithoutRequest +{ + public override void Configure() + { + Get("/Users/Details"); + } + + public override async Task HandleAsync(CancellationToken ct) + { + int userId = userService.GetUserIdFromToken(); + + User? user = await usersRepository.SingleOrDefaultAsync(new GetUserByIdSpec(userId), ct); + + if (user is null) + { + await Send.NotFoundAsync(ct); + return; + } + + await Send.OkAsync(mapper.Map(user), ct); + } +} \ No newline at end of file diff --git a/BeReadyBackend/Endpoints/Users/GetUserEndpoint.cs b/BeReadyBackend/Endpoints/Users/GetUserEndpoint.cs new file mode 100644 index 0000000..e207d2a --- /dev/null +++ b/BeReadyBackend/Endpoints/Users/GetUserEndpoint.cs @@ -0,0 +1,34 @@ +using BeReadyBackend.DTO.Users; +using BeReadyBackend.Models; +using BeReadyBackend.Repositories; +using BeReadyBackend.Services; +using BeReadyBackend.Specifications.Users; +using FastEndpoints; + +namespace BeReadyBackend.Endpoints.Users; + +public class UserRequest +{ + public int Id { get; set; } +} + +public class GetUserEndpoint(UsersRepository usersRepository, UserService userService, AutoMapper.IMapper mapper) : Endpoint +{ + public override void Configure() + { + Get("/Users/{@Id}/", x => new {x.Id}); + } + + public override async Task HandleAsync(UserRequest req, CancellationToken ct) + { + User? user = await usersRepository.SingleOrDefaultAsync(new GetUserByIdSpec(req.Id), ct); + + if (user is null) + { + await Send.NotFoundAsync(ct); + return; + } + + await Send.OkAsync(mapper.Map(user), ct); + } +} \ No newline at end of file diff --git a/BeReadyBackend/MappingProfiles/EntityToDtoMappings.cs b/BeReadyBackend/MappingProfiles/EntityToDtoMappings.cs index b2d5eb7..50761c7 100644 --- a/BeReadyBackend/MappingProfiles/EntityToDtoMappings.cs +++ b/BeReadyBackend/MappingProfiles/EntityToDtoMappings.cs @@ -24,8 +24,16 @@ public class EntityToDtoMappings : Profile CreateMap(); - CreateMap(); + CreateMap(); + CreateMap(); + + CreateMap() + .ForMember(dest => dest.ChallengeTitle, opt => opt.MapFrom(src => src.Libelle)) + .ForMember(dest => dest.ChallengeDuration, opt => opt.MapFrom(src => src.Duration)); - CreateMap(); + CreateMap() + .ForMember(dest => dest.ChallengeTitle, opt => opt.MapFrom(src => src.Title)) + .ForMember(dest => dest.ChallengeDescription, opt => opt.MapFrom(src => src.Description)) + .ForMember(dest => dest.ChallengeDuration, opt => opt.MapFrom(src => src.Duration)); } } \ No newline at end of file diff --git a/BeReadyBackend/Specifications/Users/GetProofOrChallengeByUserIdSpec.cs b/BeReadyBackend/Specifications/Users/GetProofOrChallengeByUserIdSpec.cs new file mode 100644 index 0000000..3a2ed43 --- /dev/null +++ b/BeReadyBackend/Specifications/Users/GetProofOrChallengeByUserIdSpec.cs @@ -0,0 +1,17 @@ +using Ardalis.Specification; +using BeReadyBackend.Models; + +namespace BeReadyBackend.Specifications.Users; + +public class GetProofOrChallengeByUserIdSpec : SingleResultSpecification +{ + public GetProofOrChallengeByUserIdSpec(int userId) + { + Query + .Include(x => x.UserRandomChallenges!) + .ThenInclude(x => x.RandomChallenge) + .Include(x => x.UserGroups!) + .ThenInclude(x => x.Group) + .Where(x => x.Id == userId); + } +} \ No newline at end of file diff --git a/BeReadyBackend/Specifications/Users/GetUserByIdSpec.cs b/BeReadyBackend/Specifications/Users/GetUserByIdSpec.cs index 2a51d31..dce4a91 100644 --- a/BeReadyBackend/Specifications/Users/GetUserByIdSpec.cs +++ b/BeReadyBackend/Specifications/Users/GetUserByIdSpec.cs @@ -3,7 +3,7 @@ using BeReadyBackend.Models; namespace BeReadyBackend.Specifications.Users; -public class GetUserByIdSpec : SingleResultSpecification +public class GetUserByIdSpec : SingleResultSpecification { public GetUserByIdSpec(int userId) {