Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions nestjs-mongoose/src/common/pagination/pagination.dto.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { IsInt, IsOptional, Min } from 'class-validator';
import { ClassConstructor, ClassTransformOptions, Expose, plainToClass } from '@nestjs/class-transformer';
import {
ClassConstructor,
ClassTransformOptions,
Expose,
plainToClass,
} from '@nestjs/class-transformer';
import { Type } from 'class-transformer';
import { ApiPropertyOptional } from '@nestjs/swagger';

Expand All @@ -26,7 +31,6 @@ export class PaginationParamsDto {
}

export class DataWithPaginationDto<T> {

@Expose()
data: T[];

Expand All @@ -38,8 +42,11 @@ export class DataWithPaginationDto<T> {
};
}

export function transformPlainToDataWithPaginationDto<T>(cls: ClassConstructor<T>,
plain: DataWithPaginationDto<T>, options?: ClassTransformOptions): DataWithPaginationDto<T> {
export function transformPlainToDataWithPaginationDto<T>(
cls: ClassConstructor<T>,
plain: DataWithPaginationDto<T>,
options?: ClassTransformOptions,
): DataWithPaginationDto<T> {
const { data, meta } = plain;
const transformedData = data.map((item) => {
return plainToClass(cls, item, options);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import { ClassConstructor, ClassTransformer, ClassTransformOptions } from "@nestjs/class-transformer";
import { transformPlainToDataWithPaginationDto } from "./pagination.dto";
import {
ClassConstructor,
ClassTransformOptions,
} from '@nestjs/class-transformer';
import { transformPlainToDataWithPaginationDto } from './pagination.dto';

export function TransformPaginationResponse(
classType: ClassConstructor<any>,
params?: ClassTransformOptions
params?: ClassTransformOptions,
): MethodDecorator {
return function (
target: Record<string, any>,
propertyKey: string | Symbol,
descriptor: PropertyDescriptor
propertyKey: string | symbol,
descriptor: PropertyDescriptor,
): void {
const classTransformer: ClassTransformer = new ClassTransformer();
const originalMethod = descriptor.value;

descriptor.value = function (...args: any[]): Record<string, any> {
const result: any = originalMethod.apply(this, args);
const isPromise =
Expand All @@ -21,10 +22,9 @@ export function TransformPaginationResponse(
typeof result.then === 'function';
return isPromise
? result.then((data: any) =>
transformPlainToDataWithPaginationDto(classType, data, params)
)
transformPlainToDataWithPaginationDto(classType, data, params),
)
: transformPlainToDataWithPaginationDto(classType, result, params);
};
};
}

13 changes: 0 additions & 13 deletions nestjs-mongoose/src/posts/dto/create-posts.dto.ts

This file was deleted.

26 changes: 26 additions & 0 deletions nestjs-mongoose/src/posts/dto/create-update-posts.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { IsOptional, IsString, IsNotEmpty } from 'class-validator';

interface PostDto {
title: string;
content: string;
}

export class UpdatePostDto implements PostDto {
@IsString()
@IsOptional()
title: string;

@IsString()
@IsOptional()
content: string;
}

export class CreatePostDto implements PostDto {
@IsString()
@IsNotEmpty()
title: string;

@IsString()
@IsNotEmpty()
content: string;
}
4 changes: 1 addition & 3 deletions nestjs-mongoose/src/posts/dto/posts.dto.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Expose, Transform } from '@nestjs/class-transformer';
import { DataWithPaginationDto, PaginationParamsDto } from '../../common/pagination/pagination.dto';
import { PaginationParamsDto } from '../../common/pagination/pagination.dto';

export class PostsDto {
@Expose()
Expand All @@ -20,5 +20,3 @@ export class PostsDto {
}

export class GetPostsDto extends PaginationParamsDto { }

export class GetPostsResponseDto extends DataWithPaginationDto<PostsDto> { }
13 changes: 0 additions & 13 deletions nestjs-mongoose/src/posts/dto/update-post.dto.ts

This file was deleted.

16 changes: 0 additions & 16 deletions nestjs-mongoose/src/posts/dto/update-posts.dto.ts

This file was deleted.

17 changes: 6 additions & 11 deletions nestjs-mongoose/src/posts/posts.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@ import {
Put,
Query,
} from '@nestjs/common';
import CreatePostsDto from './dto/create-posts.dto';
import ParamsWithId from '../utils/paramsWithId';
import UpdatePostDto from './dto/update-post.dto';
import { UpdatePostsDto } from './dto/update-posts.dto';
import { UpdatePostDto, CreatePostDto } from './dto/create-update-posts.dto';
import { TransformPlainToClass } from '@nestjs/class-transformer';
import { GetPostsDto, PostsDto } from './dto/posts.dto';
import { ApiBody, ApiTags } from '@nestjs/swagger';
Expand All @@ -41,10 +39,10 @@ export default class PostsController {
}

@Post()
@ApiBody({ type: CreatePostsDto })
@ApiBody({ type: CreatePostDto })
@ApiBody(swaggerConfig.body)
@TransformPlainToClass(PostsDto, { excludeExtraneousValues: true })
async createPost(@Body() post: CreatePostsDto) {
async createPost(@Body() post: CreatePostDto) {
return this.postsService.create(post);
}

Expand All @@ -57,18 +55,15 @@ export default class PostsController {
@swaggerConfig.param.id
@ApiBody(swaggerConfig.body)
@Put(':id')
@TransformPlainToClass(UpdatePostsDto, { excludeExtraneousValues: true })
async updatePost(
@Param() { id }: ParamsWithId,
@Body() post: CreatePostsDto,
) {
@TransformPlainToClass(PostsDto, { excludeExtraneousValues: true })
async updatePost(@Param() { id }: ParamsWithId, @Body() post: CreatePostDto) {
return this.postsService.update(id, post);
}

@swaggerConfig.param.id
@ApiBody(swaggerConfig.body)
@Patch(':id')
@TransformPlainToClass(UpdatePostsDto, { excludeExtraneousValues: true })
@TransformPlainToClass(PostsDto, { excludeExtraneousValues: true })
async partialUpdatePost(
@Param() { id }: ParamsWithId,
@Body() post: UpdatePostDto,
Expand Down
10 changes: 5 additions & 5 deletions nestjs-mongoose/src/posts/posts.service.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { Model } from 'mongoose';
import { Injectable, NotFoundException } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import CreatePostsDto from './dto/create-posts.dto';
import { CreatePostDto, UpdatePostDto } from './dto/create-update-posts.dto';
import { Post, PostDocument } from './post.schema';
import { paginate } from '../common/pagination/pagination-mongoose.helper';
import { PaginationParamsDto } from '../common/pagination/pagination.dto';

@Injectable()
class PostsService {
constructor(@InjectModel(Post.name) private postModel: Model<PostDocument>) { }
constructor(@InjectModel(Post.name) private postModel: Model<PostDocument>) {}

async findAll(paginationParams: PaginationParamsDto) {
return paginate(this.postModel.find(), paginationParams);
Expand All @@ -22,12 +22,12 @@ class PostsService {
return post;
}

create(postData: CreatePostsDto) {
create(postData: CreatePostDto) {
const createdPost = new this.postModel(postData);
return createdPost.save();
}

async update(id: string, postData: CreatePostsDto) {
async update(id: string, postData: CreatePostDto) {
const post = await this.postModel
.findByIdAndUpdate(id, postData)
.setOptions({ overwrite: true, new: true });
Expand All @@ -37,7 +37,7 @@ class PostsService {
return post;
}

async partialUpdate(id: string, postData: CreatePostsDto) {
async partialUpdate(id: string, postData: UpdatePostDto) {
const post = await this.postModel
.findByIdAndUpdate(id, {
$set: postData,
Expand Down