Create a filter pipe
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
import {
|
||||
BadRequestException,
|
||||
Body,
|
||||
ConflictException,
|
||||
Controller,
|
||||
@@ -26,14 +25,23 @@ import { createReadStream, existsSync } from 'fs';
|
||||
import { ApiTags, ApiUnauthorizedResponse } from '@nestjs/swagger';
|
||||
import { HistoryService } from 'src/history/history.service';
|
||||
import { JwtAuthGuard } from 'src/auth/jwt-auth.guard';
|
||||
import { FilterQuery } from 'src/utils/filter.pipe';
|
||||
|
||||
@Controller('song')
|
||||
@ApiTags('song')
|
||||
export class SongController {
|
||||
static filterableFields: string[] = [
|
||||
'+id',
|
||||
'name',
|
||||
'+artistId',
|
||||
'+albumId',
|
||||
'+genreId',
|
||||
];
|
||||
|
||||
constructor(
|
||||
private readonly songService: SongService,
|
||||
private readonly historyService: HistoryService,
|
||||
) { }
|
||||
) {}
|
||||
|
||||
@Get(':id/midi')
|
||||
async getMidi(@Param('id', ParseIntPipe) id: number) {
|
||||
@@ -104,24 +112,16 @@ export class SongController {
|
||||
@Get()
|
||||
async findAll(
|
||||
@Req() req: Request,
|
||||
@Query() filter: Prisma.SongWhereInput,
|
||||
@FilterQuery(SongController.filterableFields) where: Prisma.SongWhereInput,
|
||||
@Query('skip', new DefaultValuePipe(0), ParseIntPipe) skip: number,
|
||||
@Query('take', new DefaultValuePipe(20), ParseIntPipe) take: number,
|
||||
): Promise<Plage<Song>> {
|
||||
try {
|
||||
const ret = await this.songService.songs({
|
||||
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.songService.songs({
|
||||
skip,
|
||||
take,
|
||||
where,
|
||||
});
|
||||
return new Plage(ret, req);
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
import {
|
||||
BadRequestException,
|
||||
Injectable,
|
||||
PipeTransform,
|
||||
Query,
|
||||
} from '@nestjs/common';
|
||||
|
||||
@Injectable()
|
||||
export class FilterPipe implements PipeTransform {
|
||||
constructor(private readonly fields: string[]) {}
|
||||
|
||||
transform(value: Record<string, string>) {
|
||||
const filter = {};
|
||||
for (const fieldIdentifier of this.fields) {
|
||||
const field = fieldIdentifier.startsWith('+')
|
||||
? fieldIdentifier.slice(1)
|
||||
: fieldIdentifier;
|
||||
|
||||
if (value[field] === undefined) continue;
|
||||
|
||||
if (fieldIdentifier.startsWith('+')) {
|
||||
filter[field] = parseInt(value[field]);
|
||||
if (isNaN(filter[field]))
|
||||
throw new BadRequestException(
|
||||
`Invalid filter, ${field} should be a number but it was ${value[field]}.`,
|
||||
);
|
||||
} else filter[field] = value[field];
|
||||
}
|
||||
return filter;
|
||||
}
|
||||
}
|
||||
|
||||
export function FilterQuery(fields: string[]) {
|
||||
return Query(new FilterPipe(fields));
|
||||
}
|
||||
Reference in New Issue
Block a user