Files
BeReadyBackend/BeReadyBackend/Endpoints/Users/GetAllUserChallengesEndpoint.cs
T
2026-04-21 11:04:25 +02:00

39 lines
1.3 KiB
C#

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<List<GetUserChallengeDto>>
{
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<GetUserChallengeDto> challenges = [];
if (user.UserRandomChallenges is not null)
challenges.AddRange(
user.UserRandomChallenges
.Where(x => x.Proof is not null)
.Select(x => mapper.Map<GetUserChallengeDto>(x.RandomChallenge))
);
await Send.OkAsync(challenges.OrderByDescending(x => x.ChallengeStartDate).ToList(), ct);
}
}