diff --git a/back/src/album/album.controller.ts b/back/src/album/album.controller.ts index f5b15f7..067d77d 100644 --- a/back/src/album/album.controller.ts +++ b/back/src/album/album.controller.ts @@ -6,26 +6,26 @@ import { DefaultValuePipe, Delete, Get, - InternalServerErrorException, NotFoundException, Param, ParseIntPipe, Post, Query, Req, - StreamableFile, } from '@nestjs/common'; import { Plage } from 'src/models/plage'; import { CreateAlbumDto } from './dto/create-album.dto'; import { AlbumService } from './album.service'; import { Request } from 'express'; import { Prisma, Album } from '@prisma/client'; -import { createReadStream } from 'fs'; import { ApiTags } from '@nestjs/swagger'; +import { FilterQuery } from 'src/utils/filter.pipe'; @Controller('album') @ApiTags('album') export class AlbumController { + static filterableFields: string[] = ['+id', 'name', '+artistId']; + constructor(private readonly albumService: AlbumService) {} @Post() @@ -52,24 +52,17 @@ export class AlbumController { @Get() async findAll( @Req() req: Request, - @Query() filter: Prisma.AlbumWhereInput, + @FilterQuery(AlbumController.filterableFields) + where: Prisma.AlbumWhereInput, @Query('skip', new DefaultValuePipe(0), ParseIntPipe) skip: number, @Query('take', new DefaultValuePipe(20), ParseIntPipe) take: number, ): Promise> { - try { - const ret = await this.albumService.albums({ - skip, - take, - where: { - ...filter, - id: filter.id ? +filter.id : undefined, - }, - }); - return new Plage(ret, req); - } catch (e) { - console.log(e); - throw new BadRequestException(null, e?.toString()); - } + const ret = await this.albumService.albums({ + skip, + take, + where, + }); + return new Plage(ret, req); } @Get(':id') diff --git a/back/src/artist/artist.controller.ts b/back/src/artist/artist.controller.ts index 8077a9a..b1fde0d 100644 --- a/back/src/artist/artist.controller.ts +++ b/back/src/artist/artist.controller.ts @@ -22,11 +22,14 @@ import { ArtistService } from './artist.service'; import { Prisma, Artist } from '@prisma/client'; import { ApiTags } from '@nestjs/swagger'; import { createReadStream, existsSync } from 'fs'; +import { FilterQuery } from 'src/utils/filter.pipe'; @Controller('artist') @ApiTags('artist') export class ArtistController { - constructor(private readonly service: ArtistService) { } + static filterableFields = ['+id', 'name']; + + constructor(private readonly service: ArtistService) {} @Post() async create(@Body() dto: CreateArtistDto) { @@ -61,24 +64,17 @@ export class ArtistController { @Get() async findAll( @Req() req: Request, - @Query() filter: Prisma.SongWhereInput, + @FilterQuery(ArtistController.filterableFields) + where: Prisma.ArtistWhereInput, @Query('skip', new DefaultValuePipe(0), ParseIntPipe) skip: number, @Query('take', new DefaultValuePipe(20), ParseIntPipe) take: number, ): Promise> { - try { - const ret = await this.service.list({ - skip, - take, - where: { - ...filter, - id: filter.id ? +filter.id : undefined, - }, - }); - return new Plage(ret, req); - } catch (e) { - console.log(e); - throw new BadRequestException(null, e?.toString()); - } + const ret = await this.service.list({ + skip, + take, + where, + }); + return new Plage(ret, req); } @Get(':id') diff --git a/back/src/auth/auth.controller.ts b/back/src/auth/auth.controller.ts index 833ac97..db904d8 100644 --- a/back/src/auth/auth.controller.ts +++ b/back/src/auth/auth.controller.ts @@ -22,7 +22,6 @@ import { ApiBearerAuth, ApiBody, ApiOkResponse, - ApiParam, ApiTags, ApiUnauthorizedResponse, } from '@nestjs/swagger'; @@ -48,7 +47,8 @@ export class AuthController { try { const user = await this.usersService.createUser(registerDto) await this.settingsService.createUserSetting(user.id); - } catch { + } catch(e) { + console.error(e); throw new BadRequestException(); } } diff --git a/back/src/genre/genre.controller.ts b/back/src/genre/genre.controller.ts index c2d8288..6632e08 100644 --- a/back/src/genre/genre.controller.ts +++ b/back/src/genre/genre.controller.ts @@ -1,5 +1,4 @@ import { - BadRequestException, Body, ConflictException, Controller, @@ -22,11 +21,14 @@ import { GenreService } from './genre.service'; import { Prisma, Genre } from '@prisma/client'; import { ApiTags } from '@nestjs/swagger'; import { createReadStream, existsSync } from 'fs'; +import { FilterQuery } from 'src/utils/filter.pipe'; @Controller('genre') @ApiTags('genre') export class GenreController { - constructor(private readonly service: GenreService) { } + static filterableFields: string[] = ['+id', 'name']; + + constructor(private readonly service: GenreService) {} @Post() async create(@Body() dto: CreateGenreDto) { @@ -61,24 +63,17 @@ export class GenreController { @Get() async findAll( @Req() req: Request, - @Query() filter: Prisma.SongWhereInput, + @FilterQuery(GenreController.filterableFields) + where: Prisma.GenreWhereInput, @Query('skip', new DefaultValuePipe(0), ParseIntPipe) skip: number, @Query('take', new DefaultValuePipe(20), ParseIntPipe) take: number, ): Promise> { - try { - const ret = await this.service.list({ - skip, - take, - where: { - ...filter, - id: filter.id ? +filter.id : undefined, - }, - }); - return new Plage(ret, req); - } catch (e) { - console.log(e); - throw new BadRequestException(null, e?.toString()); - } + const ret = await this.service.list({ + skip, + take, + where, + }); + return new Plage(ret, req); } @Get(':id') diff --git a/back/src/lesson/lesson.controller.ts b/back/src/lesson/lesson.controller.ts index 1b54bff..3104eb8 100644 --- a/back/src/lesson/lesson.controller.ts +++ b/back/src/lesson/lesson.controller.ts @@ -1,7 +1,6 @@ import { Controller, Get, - Res, Query, Req, Request, @@ -18,6 +17,7 @@ import { Plage } from 'src/models/plage'; import { LessonService } from './lesson.service'; import { ApiOperation, ApiProperty, ApiTags } from '@nestjs/swagger'; import { Prisma, Skill } from '@prisma/client'; +import { FilterQuery } from 'src/utils/filter.pipe'; export class Lesson { @ApiProperty() @@ -35,6 +35,13 @@ export class Lesson { @ApiTags('lessons') @Controller('lesson') export class LessonController { + static filterableFields: string[] = [ + '+id', + 'name', + '+requiredLevel', + 'mainSkill', + ]; + constructor(private lessonService: LessonService) {} @ApiOperation({ @@ -43,26 +50,17 @@ export class LessonController { @Get() async getAll( @Req() request: Request, - @Query() filter: Prisma.LessonWhereInput, + @FilterQuery(LessonController.filterableFields) + where: Prisma.LessonWhereInput, @Query('skip', new DefaultValuePipe(0), ParseIntPipe) skip: number, @Query('take', new DefaultValuePipe(20), ParseIntPipe) take: number, ): Promise> { - try { - const ret = await this.lessonService.getAll({ - skip, - take, - where: { - ...filter, - requiredLevel: filter.requiredLevel - ? +filter.requiredLevel - : undefined, - }, - }); - return new Plage(ret, request); - } catch (e) { - console.log(e); - throw new BadRequestException(null, e?.toString()); - } + const ret = await this.lessonService.getAll({ + skip, + take, + where, + }); + return new Plage(ret, request); } @ApiOperation({