Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Next climbing log support #180

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/activities/services/activity-routes.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,12 @@ export class ActivityRoutesService {
});
}

if (params.routeTypes != null) {
builder.andWhere('r.route_type_id IN(:...routeTypes)', {
routeTypes: params.routeTypes,
});
}

if (params.publish != null) {
builder.andWhere('ar."publish" IN (:...publish)', {
publish: params.publish,
Expand Down
21 changes: 21 additions & 0 deletions src/crags/dtos/search-crags.input.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { InputType, Field, Int } from '@nestjs/graphql';
import { IsOptional } from 'class-validator';
import { OrderByInput } from '../../core/interfaces/order-by-input.interface';

@InputType()
export class SearchCragsInput {
@Field()
query?: string;

@Field({ nullable: true })
@IsOptional()
orderBy?: OrderByInput;

@Field(() => Int, { nullable: true })
@IsOptional()
pageNumber?: number;

@Field(() => Int, { nullable: true })
@IsOptional()
pageSize?: number;
}
25 changes: 25 additions & 0 deletions src/crags/dtos/search-routes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { InputType, Field, Int } from '@nestjs/graphql';
import { IsOptional } from 'class-validator';
import { OrderByInput } from '../../core/interfaces/order-by-input.interface';

@InputType()
export class SearchRoutesInput {
@Field()
query?: string;

@Field({ nullable: true })
@IsOptional()
cragId?: string;

@Field({ nullable: true })
@IsOptional()
orderBy?: OrderByInput;

@Field(() => Int, { nullable: true })
@IsOptional()
pageNumber?: number;

@Field(() => Int, { nullable: true })
@IsOptional()
pageSize?: number;
}
24 changes: 24 additions & 0 deletions src/crags/resolvers/search.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import { DataLoaderInterceptor } from '../../core/interceptors/data-loader.inter
import { User } from '../../users/entities/user.entity';
import { SearchService } from '../services/search.service';
import { SearchResults } from '../utils/search-results.class';
import { SearchCragsInput } from '../dtos/search-crags.input';
import { PaginatedCrags } from '../utils/paginated-crags';
import { PaginatedRoutes } from '../utils/paginated-routes';
import { SearchRoutesInput } from '../dtos/search-routes';

@Resolver(() => SearchResults)
@UseInterceptors(DataLoaderInterceptor)
Expand All @@ -24,4 +28,24 @@ export class SearchResolver {
): Promise<SearchResults> {
return this.searchService.find(input, user, gqlInfo);
}

@Query(() => PaginatedCrags)
@AllowAny()
@UseGuards(UserAuthGuard)
searchCrags(
@CurrentUser() user: User,
@Args('input') input?: SearchCragsInput,
): Promise<PaginatedCrags> {
return this.searchService.paginatedCrags(input, user);
}

@Query(() => PaginatedRoutes)
@AllowAny()
@UseGuards(UserAuthGuard)
searchRoutes(
@CurrentUser() user: User,
@Args('input') input?: SearchRoutesInput,
): Promise<PaginatedRoutes> {
return this.searchService.paginatedRoutes(input, user);
}
}
93 changes: 91 additions & 2 deletions src/crags/services/search.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ import { Comment } from '../entities/comment.entity';
import { User } from '../../users/entities/user.entity';
import { SearchResults } from '../utils/search-results.class';
import { FieldNode, GraphQLResolveInfo } from 'graphql';
import { SearchCragsInput } from '../dtos/search-crags.input';
import { PaginatedCrags } from '../utils/paginated-crags';
import { PaginationMeta } from '../../core/utils/pagination-meta.class';
import { SearchRoutesInput } from '../dtos/search-routes';
import { PaginatedRoutes } from '../utils/paginated-routes';

@Injectable()
export class SearchService {
Expand Down Expand Up @@ -65,7 +70,47 @@ export class SearchService {
return result;
}

async paginatedCrags(
params: SearchCragsInput,
currentUser: User,
): Promise<PaginatedCrags> {
const query = this.buildFindCragsQuery(params.query, currentUser != null);
const itemCount = await query.getCount();

const pagination = new PaginationMeta(
itemCount,
params.pageNumber,
params.pageSize,
);

if (params.orderBy != null) {
if (params.orderBy.field == 'popularity') {
query
.addSelect('count(c.id)', 'nrvisits')
.leftJoin('activity', 'ac', 'ac.crag_id = c.id')
.groupBy('c.id')
.orderBy('nrvisits', 'DESC');
}
}

query
.skip(pagination.pageSize * (pagination.pageNumber - 1))
.take(pagination.pageSize);

return Promise.resolve({
items: await query.getMany(),
meta: pagination,
});
}

findCrags(searchString: string, showHidden: boolean): Promise<Crag[]> {
return this.buildFindCragsQuery(searchString, showHidden).getMany();
}

buildFindCragsQuery(
searchString: string,
showHidden: boolean,
): SelectQueryBuilder<Crag> {
const builder = this.cragsRepository.createQueryBuilder('c');

if (!showHidden) {
Expand All @@ -76,10 +121,54 @@ export class SearchService {

this.tokenizeQueryToBuilder(builder, searchString, 'c');

return builder.getMany();
return builder;
}

async paginatedRoutes(
params: SearchRoutesInput,
currentUser: User,
): Promise<PaginatedRoutes> {
const query = this.buildFindRoutesQuery(params.query, currentUser != null);
const itemCount = await query.getCount();

const pagination = new PaginationMeta(
itemCount,
params.pageNumber,
params.pageSize,
);

if (params.orderBy != null) {
if (params.orderBy.field == 'popularity') {
query
.addSelect('count(r.id)', 'nrtries')
.leftJoin('activity_route', 'ar', 'ar.route_id = r.id')
.groupBy('r.id')
.orderBy('nrtries', 'DESC');
}
}

if (params.cragId) {
query.andWhere('r.crag_id = :cragId', { cragId: params.cragId });
}

query
.skip(pagination.pageSize * (pagination.pageNumber - 1))
.take(pagination.pageSize);

return Promise.resolve({
items: await query.getMany(),
meta: pagination,
});
}

findRoutes(searchString: string, showHidden: boolean): Promise<Route[]> {
return this.buildFindRoutesQuery(searchString, showHidden).getMany();
}

buildFindRoutesQuery(
searchString: string,
showHidden: boolean,
): SelectQueryBuilder<Route> {
const builder = this.routesRepository.createQueryBuilder('r');

builder.innerJoin('crag', 'c', 'c.id = r.crag_id');
Expand All @@ -92,7 +181,7 @@ export class SearchService {

this.tokenizeQueryToBuilder(builder, searchString, 'r');

return builder.getMany();
return builder;
}

findSectors(searchString: string, showHidden: boolean): Promise<Sector[]> {
Expand Down
12 changes: 12 additions & 0 deletions src/crags/utils/paginated-crags.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Field, ObjectType } from '@nestjs/graphql';
import { Pagination } from '../../core/interfaces/pagination.interface';
import { PaginationMeta } from '../../core/utils/pagination-meta.class';
import { Crag } from '../entities/crag.entity';

@ObjectType()
export class PaginatedCrags implements Pagination<Crag> {
@Field(() => [Crag])
items: Crag[];
@Field()
meta: PaginationMeta;
}
12 changes: 12 additions & 0 deletions src/crags/utils/paginated-routes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Field, ObjectType } from '@nestjs/graphql';
import { Pagination } from '../../core/interfaces/pagination.interface';
import { PaginationMeta } from '../../core/utils/pagination-meta.class';
import { Route } from '../entities/route.entity';

@ObjectType()
export class PaginatedRoutes implements Pagination<Route> {
@Field(() => [Route])
items: Route[];
@Field()
meta: PaginationMeta;
}