Skip to content
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
20 changes: 20 additions & 0 deletions src/search/dto/pagination.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Type } from 'class-transformer';
import { IsInt, IsOptional, Max, Min } from 'class-validator';
import { ApiPropertyOptional } from '@nestjs/swagger';

export class PaginationDto {
@ApiPropertyOptional({ description: 'The page number', default: 1, minimum: 1 })
@IsOptional()
@Type(() => Number)
@IsInt()
@Min(1)
page?: number = 1;

@ApiPropertyOptional({ description: 'The maximum number of results (max 100)', default: 20, minimum: 1, maximum: 100 })

Check failure on line 13 in src/search/dto/pagination.dto.ts

View workflow job for this annotation

GitHub Actions / validate

Replace `·description:·'The·maximum·number·of·results·(max·100)',·default:·20,·minimum:·1,·maximum:·100` with `⏎····description:·'The·maximum·number·of·results·(max·100)',⏎····default:·20,⏎····minimum:·1,⏎····maximum:·100,⏎·`
@IsOptional()
@Type(() => Number)
@IsInt()
@Min(1)
@Max(100)
limit?: number = 20;
}
12 changes: 6 additions & 6 deletions src/search/search.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { Throttle } from '@nestjs/throttler';
import { THROTTLE } from '../common/constants/throttle.constants';
import { SearchService } from './search.service';
import { PaginationDto } from './dto/pagination.dto';

@ApiTags('Search')
@Throttle({ default: THROTTLE.SEARCH })
Expand All @@ -29,8 +30,8 @@
example: '{"category":"programming","level":"beginner"}',
})
@ApiQuery({ name: 'sort', required: false, example: 'relevance' })
@ApiQuery({ name: 'page', required: false, example: 1 })
@ApiQuery({ name: 'limit', required: false, example: 20 })
@ApiQuery({ name: 'page', required: false, description: 'The page number. Default is 1.', example: 1, type: Number })

Check failure on line 33 in src/search/search.controller.ts

View workflow job for this annotation

GitHub Actions / validate

Replace `·name:·'page',·required:·false,·description:·'The·page·number.·Default·is·1.',·example:·1,·type:·Number` with `⏎····name:·'page',⏎····required:·false,⏎····description:·'The·page·number.·Default·is·1.',⏎····example:·1,⏎····type:·Number,⏎·`
@ApiQuery({ name: 'limit', required: false, description: 'The maximum number of results. Default is 20. Max is 100.', example: 20, type: Number })

Check failure on line 34 in src/search/search.controller.ts

View workflow job for this annotation

GitHub Actions / validate

Replace `·name:·'limit',·required:·false,·description:·'The·maximum·number·of·results.·Default·is·20.·Max·is·100.',·example:·20,·type:·Number` with `⏎····name:·'limit',⏎····required:·false,⏎····description:·'The·maximum·number·of·results.·Default·is·20.·Max·is·100.',⏎····example:·20,⏎····type:·Number,⏎·`
@ApiResponse({
status: 200,
description: 'Search results',
Expand All @@ -49,10 +50,9 @@
@ApiResponse({ status: 503, description: 'Search is temporarily unavailable' })
async search(
@Query('q') query: string,
@Query() paginationDto: PaginationDto,
@Query('filters') filters?: string,
@Query('sort') sort?: string,
@Query('page') page?: string,
@Query('limit') limit?: string,
): Promise<any> {
let parsedFilters: Record<string, any> = {};
if (filters) {
Expand All @@ -63,8 +63,8 @@
}
}

const pageNum = page ? parseInt(page, 10) : 1;
const limitNum = limit ? parseInt(limit, 10) : 20;
const pageNum = paginationDto.page ?? 1;
const limitNum = paginationDto.limit ?? 20;

return this.searchService.search(query, parsedFilters, sort, pageNum, limitNum);
}
Expand Down
Loading