feat(center-nodes): add paginated node listing
This commit is contained in:
@@ -2,9 +2,12 @@ import {
|
||||
Body,
|
||||
ConflictException,
|
||||
Controller,
|
||||
DefaultValuePipe,
|
||||
ForbiddenException,
|
||||
Get,
|
||||
ParseIntPipe,
|
||||
Post,
|
||||
Query,
|
||||
} from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Repository } from 'typeorm';
|
||||
@@ -59,10 +62,25 @@ export class NodesController {
|
||||
}
|
||||
|
||||
@Get()
|
||||
async list() {
|
||||
const items = await this.nodeRepo.find({
|
||||
async list(
|
||||
@Query('page', new DefaultValuePipe(1), ParseIntPipe) page: number,
|
||||
@Query('pageSize', new DefaultValuePipe(20), ParseIntPipe) pageSize: number,
|
||||
) {
|
||||
const safePage = page < 1 ? 1 : page;
|
||||
const safePageSize = pageSize < 1 ? 20 : Math.min(pageSize, 100);
|
||||
|
||||
const [items, total] = await this.nodeRepo.findAndCount({
|
||||
order: { createdAt: 'DESC' },
|
||||
skip: (safePage - 1) * safePageSize,
|
||||
take: safePageSize,
|
||||
});
|
||||
return { items };
|
||||
|
||||
return {
|
||||
items,
|
||||
page: safePage,
|
||||
pageSize: safePageSize,
|
||||
total,
|
||||
totalPages: Math.max(1, Math.ceil(total / safePageSize)),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user