mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-11-27 05:14:53 +00:00
community controller
This commit is contained in:
23
libs/common/src/dto/base.response.dto.ts
Normal file
23
libs/common/src/dto/base.response.dto.ts
Normal file
@ -0,0 +1,23 @@
|
||||
import { WithOptional } from '../type/optional.type';
|
||||
|
||||
export class BaseResponseDto {
|
||||
statusCode?: number;
|
||||
|
||||
message: string;
|
||||
|
||||
error?: string;
|
||||
|
||||
data?: any;
|
||||
|
||||
success?: boolean;
|
||||
|
||||
static wrap({
|
||||
data,
|
||||
statusCode = 200,
|
||||
message = 'Success',
|
||||
success = true,
|
||||
error = undefined,
|
||||
}: WithOptional<BaseResponseDto, 'message'>) {
|
||||
return { data, statusCode, success, message, error };
|
||||
}
|
||||
}
|
||||
87
libs/common/src/dto/pagination.request.dto.ts
Normal file
87
libs/common/src/dto/pagination.request.dto.ts
Normal file
@ -0,0 +1,87 @@
|
||||
import { IsDate, IsOptional } from 'class-validator';
|
||||
import { IsPageRequestParam } from '../validators/is-page-request-param.validator';
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
import { IsSizeRequestParam } from '../validators/is-size-request-param.validator';
|
||||
import { IsSortParam } from '../validators/is-sort-param.validator';
|
||||
import { Transform } from 'class-transformer';
|
||||
import { parseToDate } from '../util/parseToDate';
|
||||
|
||||
export class PaginationRequestGetListDto {
|
||||
@IsOptional()
|
||||
@IsPageRequestParam({
|
||||
message: 'Page must be bigger than 0',
|
||||
})
|
||||
@ApiProperty({
|
||||
name: 'page',
|
||||
required: false,
|
||||
description: 'Page request',
|
||||
})
|
||||
page?: number;
|
||||
|
||||
@IsOptional()
|
||||
@IsSizeRequestParam({
|
||||
message: 'Size must not be negative',
|
||||
})
|
||||
@ApiProperty({
|
||||
name: 'size',
|
||||
required: false,
|
||||
description: 'Size request',
|
||||
})
|
||||
size?: number;
|
||||
|
||||
@IsOptional()
|
||||
@IsSortParam({
|
||||
message:
|
||||
'Incorrect sorting condition format. Should be like this format propertyId:asc,createdDate:desc',
|
||||
})
|
||||
@ApiProperty({
|
||||
name: 'sort',
|
||||
required: false,
|
||||
description: 'Sort condition',
|
||||
})
|
||||
sort?: string;
|
||||
|
||||
@IsOptional()
|
||||
@ApiProperty({
|
||||
name: 'name',
|
||||
required: false,
|
||||
description: 'Name to be filtered',
|
||||
})
|
||||
name?: string;
|
||||
|
||||
@IsOptional()
|
||||
@ApiProperty({
|
||||
name: 'include',
|
||||
required: false,
|
||||
description: 'Fields to include',
|
||||
})
|
||||
include?: string;
|
||||
|
||||
@ApiProperty({
|
||||
name: 'from',
|
||||
required: false,
|
||||
type: Number,
|
||||
description: `Start time in UNIX timestamp format to filter`,
|
||||
example: 1674172800000,
|
||||
})
|
||||
@IsOptional()
|
||||
@Transform(({ value }) => parseToDate(value))
|
||||
@IsDate({
|
||||
message: `From must be in UNIX timestamp format in order to parse to Date instance`,
|
||||
})
|
||||
from?: Date;
|
||||
|
||||
@ApiProperty({
|
||||
name: 'to',
|
||||
required: false,
|
||||
type: Number,
|
||||
description: `End time in UNIX timestamp format to filter`,
|
||||
example: 1674259200000,
|
||||
})
|
||||
@IsOptional()
|
||||
@Transform(({ value }) => parseToDate(value))
|
||||
@IsDate({
|
||||
message: `To must be in UNIX timestamp format in order to parse to Date instance`,
|
||||
})
|
||||
to?: Date;
|
||||
}
|
||||
62
libs/common/src/dto/pagination.response.dto.ts
Normal file
62
libs/common/src/dto/pagination.response.dto.ts
Normal file
@ -0,0 +1,62 @@
|
||||
import { BaseResponseDto } from './base.response.dto';
|
||||
|
||||
export interface PageResponseDto {
|
||||
// Original paging information from the request ( or default )
|
||||
page: number;
|
||||
size: number;
|
||||
|
||||
// Useful for display (N Records found)
|
||||
totalItem: number;
|
||||
|
||||
// Use for display N Pages ( 0... N )
|
||||
totalPage: number;
|
||||
|
||||
// Has next is false when cursor is at last page
|
||||
hasNext: boolean;
|
||||
|
||||
// Has previous is false when cursor is at first page
|
||||
hasPrevious: boolean;
|
||||
}
|
||||
|
||||
export class PageResponse<T> implements BaseResponseDto, PageResponseDto {
|
||||
code?: number;
|
||||
|
||||
message: string;
|
||||
|
||||
data: Array<T>;
|
||||
|
||||
page: number;
|
||||
|
||||
size: number;
|
||||
|
||||
totalItem: number;
|
||||
|
||||
totalPage: number;
|
||||
|
||||
hasNext: boolean;
|
||||
|
||||
hasPrevious: boolean;
|
||||
|
||||
constructor(
|
||||
baseResponseDto: BaseResponseDto,
|
||||
pageResponseDto: PageResponseDto,
|
||||
) {
|
||||
if (baseResponseDto.statusCode) {
|
||||
this.code = baseResponseDto.statusCode;
|
||||
} else {
|
||||
this.code = 200;
|
||||
}
|
||||
|
||||
if (baseResponseDto.data) {
|
||||
this.data = baseResponseDto.data;
|
||||
}
|
||||
|
||||
this.message = baseResponseDto.message;
|
||||
this.page = pageResponseDto.page;
|
||||
this.size = pageResponseDto.size;
|
||||
this.totalItem = pageResponseDto.totalItem;
|
||||
this.totalPage = pageResponseDto.totalPage;
|
||||
this.hasNext = pageResponseDto.hasNext;
|
||||
this.hasPrevious = pageResponseDto.hasPrevious;
|
||||
}
|
||||
}
|
||||
29
libs/common/src/dto/success.response.dto.ts
Normal file
29
libs/common/src/dto/success.response.dto.ts
Normal file
@ -0,0 +1,29 @@
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
import { BaseResponseDto } from './base.response.dto';
|
||||
|
||||
export class SuccessResponseDto<Type> implements BaseResponseDto {
|
||||
@ApiProperty({
|
||||
example: 200,
|
||||
})
|
||||
statusCode: number;
|
||||
|
||||
data: Type;
|
||||
|
||||
@ApiProperty({
|
||||
example: 'Success message',
|
||||
})
|
||||
message: string;
|
||||
|
||||
@ApiProperty({
|
||||
example: true,
|
||||
description: 'Indicates that the operation was successful',
|
||||
})
|
||||
success: boolean;
|
||||
|
||||
constructor(input: BaseResponseDto) {
|
||||
if (input.statusCode) this.statusCode = input.statusCode;
|
||||
else this.statusCode = 200;
|
||||
if (input.data) this.data = input.data;
|
||||
this.success = true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user