Когда в postman я хочу получить pagination
localhost:8000/api/albums/skip/0/take/9 указав только
localhost:8000/api/albums мне нужно обязательно указать /skip/0/take/9
import {
Controller,
Get,
Post,
Body,
Patch,
Param,
Delete,
} from '@nestjs/common';
import { Albums as AlbumsModel } from '@prisma/client';
import { AlbumsService } from './albums.service';
import { CreateAlbumDto } from './dto/create-album.dto';
import { UpdateAlbumDto } from './dto/update-album.dto';
@Controller('albums')
export class AlbumsController {
public constructor(private readonly albumsService: AlbumsService) {}
@Post()
public create(@Body() createAlbumDto: CreateAlbumDto) {
return this.albumsService.create(createAlbumDto);
}
@Get('skip/:skip/take/:take')
public async findAll(
@Param('skip') skip?: string,
@Param('take') take?: string,
//@Param('skip') skip: string = String('0'), default не задается
//@Param('take') take: string = String('9'),
): Promise<AlbumsModel[]> {
console.log(skip, take); //Ничего нет пока не укажу /api/albums/skip/9/take/11
return await this.albumsService.findAll({
skip: Number(skip),
take: Number(take),
});
}
@Get(':id')
public findOne(@Param('id') id: string) {
return this.albumsService.findOne(+id);
}
@Patch(':id')
public update(
@Param('id') id: string,
@Body() updateAlbumDto: UpdateAlbumDto,
) {
return this.albumsService.update(+id, updateAlbumDto);
}
@Delete(':id')
public remove(@Param('id') id: string) {
return this.albumsService.remove(+id);
}
}
{
"statusCode": 404,
"message": "Cannot GET /api/albums",
"error": "Not Found"
}
import { Injectable } from '@nestjs/common';
import { CreateAlbumDto } from './dto/create-album.dto';
import { UpdateAlbumDto } from './dto/update-album.dto';
import { PrismaService } from './../prisma.service';
import { Albums, Prisma } from '@prisma/client';
@Injectable()
export class AlbumsService {
public constructor(private readonly prismaService: PrismaService) {}
public create(createAlbumDto: CreateAlbumDto) {
return 'This action adds a new album';
}
public async findAll(params: {
skip?: number;
take?: number;
}): Promise<Albums[]> {
const { skip, take } = params;
return await this.prismaService.albums.findMany({
skip,
take,
});
//return `This action returns all albums`;
}
public findOne(id: number) {
return `This action returns a #${id} album`;
}
public update(id: number, updateAlbumDto: UpdateAlbumDto) {
return `This action updates a #${id} album`;
}
public remove(id: number) {
return `This action removes a #${id} album`;
}
}
Сиды есть
import { PrismaClient, Prisma } from '@prisma/client';
import * as faker from 'faker';
const prisma = new PrismaClient();
const count = 11;
const albumData: Prisma.AlbumsCreateInput[] = [];
async function main() {
console.log(`Start seeding ...`);
for (let i = 0; i < count; i++) {
const album = await prisma.albums.create({
data: {
image: faker.image.imageUrl(),
countTrack: faker.datatype.number({ min: 1, max: count }),
title: faker.name.title(),
},
});
console.log(`Created album with id: ${album.id}`);
}
console.log(`Seeding finished.`);
}
main()
.catch((e) => {
console.error(e);
process.exit(1);
})
.finally(async () => {
await prisma.$disconnect();
});