-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: PaginationQuery, PaginationMetadata dto 추가
- Loading branch information
Showing
3 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './pagination.dto'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters