Fixed error with achievements and upgrade of spec to list challenges and proofs

This commit is contained in:
2026-05-18 14:25:56 +01:00
parent 75876b3ab3
commit bb1120f967
7 changed files with 42 additions and 46 deletions
@@ -2,12 +2,12 @@
using BeReadyBackend.Models;
using BeReadyBackend.Repositories;
using BeReadyBackend.Services;
using BeReadyBackend.Specifications.Users;
using BeReadyBackend.Specifications.RandomChallenges;
using FastEndpoints;
namespace BeReadyBackend.Endpoints.Users;
public class GetAllUserChallengesEndpoint(UsersRepository usersRepository, UserService userService, AutoMapper.IMapper mapper) : EndpointWithoutRequest<List<GetUserChallengeDto>>
public class GetAllUserChallengesEndpoint(UserRandomChallengesRepository userRandomChallengesRepository, UserService userService, AutoMapper.IMapper mapper) : EndpointWithoutRequest<List<GetUserChallengeDto>>
{
public override void Configure()
{
@@ -18,18 +18,11 @@ public class GetAllUserChallengesEndpoint(UsersRepository usersRepository, UserS
{
int userId = userService.GetUserIdFromToken();
User? user = await usersRepository.SingleOrDefaultAsync(new GetProofOrChallengeByUserIdSpec(userId), ct);
List<UserRandomChallenge>? userRandomChallenge = await userRandomChallengesRepository.ListAsync(new GetUserRandomChallengeByIdSpec(userId), ct);
if (user is null)
{
await Send.NotFoundAsync(ct);
return;
}
List<GetUserChallengeDto> challenges = user.UserRandomChallenges?
.Where(x => x.Proof is not null)
List<GetUserChallengeDto> challenges = userRandomChallenge
.Select(x => mapper.Map<GetUserChallengeDto>(x.RandomChallenge))
.ToList() ?? [];
.ToList() ;
await Send.OkAsync(challenges.OrderByDescending(x => x.ChallengeStartDate).ToList(), ct);
}
@@ -7,7 +7,7 @@ using FastEndpoints;
namespace BeReadyBackend.Endpoints.Users;
public class GetAllUserProofsEndpoint(UsersRepository usersRepository, UserService userService) : EndpointWithoutRequest<List<GetUserProofDto>>
public class GetAllUserProofsEndpoint(UserRandomChallengesRepository userRandomChallengesRepository, UserService userService, AutoMapper.IMapper mapper) : EndpointWithoutRequest<List<GetUserProofDto>>
{
public override void Configure()
{
@@ -18,22 +18,12 @@ public class GetAllUserProofsEndpoint(UsersRepository usersRepository, UserServi
{
int userId = userService.GetUserIdFromToken();
User? user = await usersRepository.SingleOrDefaultAsync(new GetProofOrChallengeByUserIdSpec(userId), ct);
if (user is null)
{
await Send.NotFoundAsync(ct);
return;
}
List<GetUserProofDto> proofs = user.UserRandomChallenges?
.Where(x => x.Proof is not null)
.Select(x => new GetUserProofDto
{
Proof = x.Proof
})
.ToList() ?? [];
List<UserRandomChallenge> userProofs = await userRandomChallengesRepository.ListAsync(new GetUserProofByUserIdSpec(userId), ct);
List<GetUserProofDto> proofs = userProofs
.Select( mapper.Map<GetUserProofDto>)
.ToList();
await Send.OkAsync(proofs, ct);
}
}
@@ -31,7 +31,6 @@ public class EntityToDtoMappings : Profile
CreateMap<User, GetUserStatsDto>();
CreateMap<UserGroup, GetUserProofDto>();
CreateMap<UserRandomChallenge, GetUserProofDto>();
CreateMap<RandomChallenge, GetUserChallengeDto>()
@@ -3,6 +3,7 @@ using BeReadyBackend.Repositories;
using BeReadyBackend.Specifications.Achievements;
using BeReadyBackend.Specifications.Friends;
using BeReadyBackend.Specifications.Posts;
using BeReadyBackend.Specifications.RandomChallenges;
using BeReadyBackend.Specifications.UserAchievements;
using BeReadyBackend.Specifications.Users;
@@ -13,7 +14,8 @@ public class AchievementService(
AchievementsRepository achievementsRepository,
UserAchievementsRepository userAchievementsRepository,
UserPostsRepository userPostsRepository,
UserFriendsRepository userFriendsRepository)
UserFriendsRepository userFriendsRepository,
UserRandomChallengesRepository userRandomChallengesRepository)
{
public async Task Unlock(int userId, int achievementId)
{
@@ -39,7 +41,7 @@ public class AchievementService(
User? user = await usersRepository.SingleOrDefaultAsync(new GetUserByIdSpec(userId));
if (user is null) return;
int challengesCounter = await usersRepository.CountAsync(new GetProofOrChallengeByUserIdSpec(userId));
int challengesCounter = await userRandomChallengesRepository.CountAsync(new GetUserRandomChallengeByIdSpec(userId));
int likedCounter = await userPostsRepository.CountAsync(new GetPostsLikedByUserSpec(userId));
int friendsCounter = await userFriendsRepository.CountAsync(new GetFriendsByUserIdSpec(userId));
int friendsRequestsCounter = await userFriendsRepository.CountAsync(new GetFriendRequestSpec(userId));
@@ -0,0 +1,14 @@
using Ardalis.Specification;
using BeReadyBackend.Models;
namespace BeReadyBackend.Specifications.RandomChallenges;
public class GetUserRandomChallengeByIdSpec : Specification<UserRandomChallenge>
{
public GetUserRandomChallengeByIdSpec(int userId)
{
Query
.Include(x => x.RandomChallenge)
.Where(x => x.UserId == userId && x.Proof != null);
}
}
@@ -1,15 +0,0 @@
using Ardalis.Specification;
using BeReadyBackend.Models;
namespace BeReadyBackend.Specifications.Users;
public class GetProofOrChallengeByUserIdSpec : SingleResultSpecification<User>
{
public GetProofOrChallengeByUserIdSpec(int userId)
{
Query
.Include(x => x.UserRandomChallenges!)
.ThenInclude(x => x.RandomChallenge)
.Where(x => x.Id == userId);
}
}
@@ -0,0 +1,13 @@
using Ardalis.Specification;
using BeReadyBackend.Models;
namespace BeReadyBackend.Specifications.Users;
public class GetUserProofByUserIdSpec : Specification<UserRandomChallenge>
{
public GetUserProofByUserIdSpec(int userId)
{
Query
.Where(x => x.UserId == userId && x.Proof != null);
}
}