From 0b8c59bf96084b5cfeab340c6b5e9c8b0c7ceff0 Mon Sep 17 00:00:00 2001 From: salamca Date: Sat, 29 Jun 2024 13:14:06 +0200 Subject: [PATCH] Add search/filter-param to diff votes and star votes field resolvers. --- src/crags/dtos/find-difficulty-votes.input.ts | 9 +++++++++ src/crags/dtos/find-star-rating-votes.input.ts | 9 +++++++++ src/crags/resolvers/routes.resolver.ts | 16 ++++++++++++---- src/crags/services/difficulty-votes.service.ts | 13 +++++++++++-- src/crags/services/star-rating-votes.service.ts | 13 +++++++++++-- 5 files changed, 52 insertions(+), 8 deletions(-) create mode 100644 src/crags/dtos/find-difficulty-votes.input.ts create mode 100644 src/crags/dtos/find-star-rating-votes.input.ts diff --git a/src/crags/dtos/find-difficulty-votes.input.ts b/src/crags/dtos/find-difficulty-votes.input.ts new file mode 100644 index 0000000..35c95ce --- /dev/null +++ b/src/crags/dtos/find-difficulty-votes.input.ts @@ -0,0 +1,9 @@ +import { InputType, Field } from '@nestjs/graphql'; +import { IsOptional } from 'class-validator'; + +@InputType() +export class FindDifficultyVotesInput { + @Field({ nullable: true }) + @IsOptional() + userId?: string; +} diff --git a/src/crags/dtos/find-star-rating-votes.input.ts b/src/crags/dtos/find-star-rating-votes.input.ts new file mode 100644 index 0000000..32148c4 --- /dev/null +++ b/src/crags/dtos/find-star-rating-votes.input.ts @@ -0,0 +1,9 @@ +import { InputType, Field } from '@nestjs/graphql'; +import { IsOptional } from 'class-validator'; + +@InputType() +export class FindStarRatingVotesInput { + @Field({ nullable: true }) + @IsOptional() + userId?: string; +} diff --git a/src/crags/resolvers/routes.resolver.ts b/src/crags/resolvers/routes.resolver.ts index 995f1eb..aecd72a 100644 --- a/src/crags/resolvers/routes.resolver.ts +++ b/src/crags/resolvers/routes.resolver.ts @@ -54,6 +54,8 @@ import { ActivityRoutesService } from '../../activities/services/activity-routes import { FindActivityRoutesInput } from '../../activities/dtos/find-activity-routes.input'; import { StarRatingVotesService } from '../services/star-rating-votes.service'; import { StarRatingVote } from '../entities/star-rating-vote.entity'; +import { FindDifficultyVotesInput } from '../dtos/find-difficulty-votes.input'; +import { FindStarRatingVotesInput } from '../dtos/find-star-rating-votes.input'; @Resolver(() => Route) @UseInterceptors(DataLoaderInterceptor) @@ -256,13 +258,19 @@ export class RoutesResolver { } @ResolveField('difficultyVotes', () => [DifficultyVote]) - async difficultyVotes(@Parent() route: Route): Promise { - return this.difficultyVotesService.findByRouteId(route.id); + async difficultyVotes( + @Parent() route: Route, + @Args('input', { nullable: true }) input: FindDifficultyVotesInput = {}, + ): Promise { + return this.difficultyVotesService.findByRouteId(route.id, input); } @ResolveField('starRatingVotes', () => [StarRatingVote]) - async starRatingVotes(@Parent() route: Route): Promise { - return this.starRatingVotesService.findByRouteId(route.id); + async starRatingVotes( + @Parent() route: Route, + @Args('input', { nullable: true }) input: FindStarRatingVotesInput = {}, + ): Promise { + return this.starRatingVotesService.findByRouteId(route.id, input); } @ResolveField('crag', () => Crag) diff --git a/src/crags/services/difficulty-votes.service.ts b/src/crags/services/difficulty-votes.service.ts index fff6bf5..6de81b3 100644 --- a/src/crags/services/difficulty-votes.service.ts +++ b/src/crags/services/difficulty-votes.service.ts @@ -7,6 +7,7 @@ import { setBuilderCache } from '../../core/utils/entity-cache/entity-cache-help import { LatestDifficultyVotesInputServiceInput } from '../dtos/latest-difficulty-votes-service.input'; import { DifficultyVote } from '../entities/difficulty-vote.entity'; import { PaginatedDifficultyVotes } from '../utils/paginated-difficulty-votes'; +import { FindDifficultyVotesInput } from '../dtos/find-difficulty-votes.input'; @Injectable() export class DifficultyVotesService { @@ -15,9 +16,17 @@ export class DifficultyVotesService { private difficultyVoteRepository: Repository, ) {} - async findByRouteId(routeId: string): Promise { + async findByRouteId( + routeId: string, + input: FindDifficultyVotesInput = {}, + ): Promise { + const where = { + ...(input.userId && { userId: input.userId }), + ...{ routeId }, + }; + const grades = this.difficultyVoteRepository.find({ - where: { routeId: routeId }, + where, order: { difficulty: 'ASC' }, }); diff --git a/src/crags/services/star-rating-votes.service.ts b/src/crags/services/star-rating-votes.service.ts index 0bea768..a921868 100644 --- a/src/crags/services/star-rating-votes.service.ts +++ b/src/crags/services/star-rating-votes.service.ts @@ -3,6 +3,7 @@ import { InjectRepository } from '@nestjs/typeorm'; import { Repository } from 'typeorm'; import { User } from '../../users/entities/user.entity'; import { StarRatingVote } from '../entities/star-rating-vote.entity'; +import { FindStarRatingVotesInput } from '../dtos/find-star-rating-votes.input'; @Injectable() export class StarRatingVotesService { @@ -19,9 +20,17 @@ export class StarRatingVotesService { .getMany(); } - async findByRouteId(routeId: string): Promise { + async findByRouteId( + routeId: string, + input: FindStarRatingVotesInput = {}, + ): Promise { + const where = { + ...(input.userId && { userId: input.userId }), + ...{ routeId }, + }; + return this.starRatingVoteRepository.find({ - where: { routeId: routeId }, + where, order: { stars: 'ASC' }, }); }