From 0945601b7087d14c435586d173d8fb8ea16ceaac Mon Sep 17 00:00:00 2001 From: J-hoplin1 Date: Sun, 30 Jun 2024 02:22:17 +0900 Subject: [PATCH] =?UTF-8?q?Feat:=20PaginationQuery,=20PaginationMetadata?= =?UTF-8?q?=20dto=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/common/dto/index.ts | 1 + src/common/dto/pagination.dto.ts | 66 ++++++++++++++++++++++++++++++++ src/common/index.ts | 1 + 3 files changed, 68 insertions(+) create mode 100644 src/common/dto/index.ts create mode 100644 src/common/dto/pagination.dto.ts diff --git a/src/common/dto/index.ts b/src/common/dto/index.ts new file mode 100644 index 0000000..5f10edb --- /dev/null +++ b/src/common/dto/index.ts @@ -0,0 +1 @@ +export * from './pagination.dto'; diff --git a/src/common/dto/pagination.dto.ts b/src/common/dto/pagination.dto.ts new file mode 100644 index 0000000..e3e877c --- /dev/null +++ b/src/common/dto/pagination.dto.ts @@ -0,0 +1,66 @@ +import { ApiProperty } from '@nestjs/swagger'; +import { Exclude, Expose, Type } from 'class-transformer'; +import { IsNumber, IsOptional, Min } from 'class-validator'; + +// Pagination 들어가는 API에 대해서 상속해주세용 +export class PaginationQuery { + @ApiProperty({ + description: 'Pagination - Page', + default: 1, + required: false, + }) + @Type(() => Number) + @IsOptional() + @IsNumber() + @Min(1) + readonly page = 1; + + @ApiProperty({ + description: 'Pagination - Limit', + default: 10, + required: false, + }) + @Type(() => Number) + @IsOptional() + @IsNumber() + @Min(1) + readonly limit = 10; +} + +// Pagination API Response의 Metadata로 추가해주세용 +export class PaginationMetadata { + @Exclude() + private _page: number; + + @Exclude() + private _limit: number; + + @Exclude() + private _total: number; + + @Exclude() + private _lastPage: number; + + @Exclude() + private _nextPage: number; + + constructor(page: number, limit: number, total: number) { + this._page = page; + this._limit = limit; + this._total = total; + this._lastPage = Math.ceil(this._total / this._limit); + this._nextPage = this._page < this._lastPage ? this._page + 1 : null; + } + + @Expose() + @ApiProperty() + get hasNext() { + return Boolean(this._nextPage); + } + + @Expose() + @ApiProperty() + get total() { + return this._total; + } +} diff --git a/src/common/index.ts b/src/common/index.ts index f30e00c..47c0f02 100644 --- a/src/common/index.ts +++ b/src/common/index.ts @@ -3,3 +3,4 @@ export * from './filter'; export * from './interceptor'; export * from './decorators'; export * from './utils'; +export * from './dto';