Skip to content

Commit

Permalink
Add search/filter-param to diff votes and star votes field resolvers.
Browse files Browse the repository at this point in the history
  • Loading branch information
salamca committed Jun 29, 2024
1 parent d290266 commit 0b8c59b
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 8 deletions.
9 changes: 9 additions & 0 deletions src/crags/dtos/find-difficulty-votes.input.ts
Original file line number Diff line number Diff line change
@@ -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;
}
9 changes: 9 additions & 0 deletions src/crags/dtos/find-star-rating-votes.input.ts
Original file line number Diff line number Diff line change
@@ -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;
}
16 changes: 12 additions & 4 deletions src/crags/resolvers/routes.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -256,13 +258,19 @@ export class RoutesResolver {
}

@ResolveField('difficultyVotes', () => [DifficultyVote])
async difficultyVotes(@Parent() route: Route): Promise<DifficultyVote[]> {
return this.difficultyVotesService.findByRouteId(route.id);
async difficultyVotes(
@Parent() route: Route,
@Args('input', { nullable: true }) input: FindDifficultyVotesInput = {},
): Promise<DifficultyVote[]> {
return this.difficultyVotesService.findByRouteId(route.id, input);
}

@ResolveField('starRatingVotes', () => [StarRatingVote])
async starRatingVotes(@Parent() route: Route): Promise<StarRatingVote[]> {
return this.starRatingVotesService.findByRouteId(route.id);
async starRatingVotes(
@Parent() route: Route,
@Args('input', { nullable: true }) input: FindStarRatingVotesInput = {},
): Promise<StarRatingVote[]> {
return this.starRatingVotesService.findByRouteId(route.id, input);
}

@ResolveField('crag', () => Crag)
Expand Down
13 changes: 11 additions & 2 deletions src/crags/services/difficulty-votes.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -15,9 +16,17 @@ export class DifficultyVotesService {
private difficultyVoteRepository: Repository<DifficultyVote>,
) {}

async findByRouteId(routeId: string): Promise<DifficultyVote[]> {
async findByRouteId(
routeId: string,
input: FindDifficultyVotesInput = {},
): Promise<DifficultyVote[]> {
const where = {
...(input.userId && { userId: input.userId }),
...{ routeId },
};

const grades = this.difficultyVoteRepository.find({
where: { routeId: routeId },
where,
order: { difficulty: 'ASC' },
});

Expand Down
13 changes: 11 additions & 2 deletions src/crags/services/star-rating-votes.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -19,9 +20,17 @@ export class StarRatingVotesService {
.getMany();
}

async findByRouteId(routeId: string): Promise<StarRatingVote[]> {
async findByRouteId(
routeId: string,
input: FindStarRatingVotesInput = {},
): Promise<StarRatingVote[]> {
const where = {
...(input.userId && { userId: input.userId }),
...{ routeId },
};

return this.starRatingVoteRepository.find({
where: { routeId: routeId },
where,
order: { stars: 'ASC' },
});
}
Expand Down

0 comments on commit 0b8c59b

Please sign in to comment.