Use the new filter pipe for every resources

This commit is contained in:
2023-05-28 14:44:48 +09:00
parent 1a5ab5d099
commit 93ae411ebe
5 changed files with 53 additions and 71 deletions

View File

@@ -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<Plage<Album>> {
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')

View File

@@ -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<Plage<Artist>> {
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')

View File

@@ -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();
}
}

View File

@@ -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<Plage<Genre>> {
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')

View File

@@ -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<Plage<Lesson>> {
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({