Shows/controller: validate search parameters

This commit is contained in:
Toast 2025-02-01 20:33:18 +01:00
parent a2b747a823
commit 02c2f37379

View file

@ -14,6 +14,13 @@ import {
import { ShowsService } from './shows.service'; import { ShowsService } from './shows.service';
import { ShowDto } from './dto/show.dto'; import { ShowDto } from './dto/show.dto';
import { PaginationDto } from 'src/pagination.dto'; import { PaginationDto } from 'src/pagination.dto';
import { IsNotEmpty, IsString } from 'class-validator';
export class SearchDto {
@IsString()
@IsNotEmpty()
query: string;
}
@Controller('shows') @Controller('shows')
export class ShowsController { export class ShowsController {
@ -78,15 +85,16 @@ export class ShowsController {
} }
@Get('search') @Get('search')
async search(@Query('query') name: string) { async search(@Query() search: SearchDto) {
try { try {
const shows = await this.showsService.search(name); const { query } = search;
const shows = await this.showsService.search(query);
if (shows.length > 0) { if (shows.length > 0) {
return { status: 'Ok', show: shows, test: shows.length }; return { status: 'Ok', show: shows, test: shows.length };
} else { } else {
throw new NotFoundException({ throw new NotFoundException({
status: 'Error', status: 'Error',
message: `Can't find show matching ${name}`, message: `Can't find show matching ${query}`,
}); });
} }
} catch (error) { } catch (error) {