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
1 change: 0 additions & 1 deletion nestjs-mongoose/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ module.exports = {
plugins: ['@typescript-eslint/eslint-plugin'],
extends: [
'plugin:@typescript-eslint/recommended',
'plugin:prettier/recommended',
],
root: true,
env: {
Expand Down
3 changes: 2 additions & 1 deletion nestjs-mongoose/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"class-transformer": "^0.5.1",
"class-validator": "^0.13.2",
"joi": "^17.7.0",
"mongodb-memory-server": "^8.11.4",
"mongoose": "^6.7.2",
"nats": "^2.6.1",
"reflect-metadata": "^0.1.13",
Expand Down Expand Up @@ -79,4 +80,4 @@
"coverageDirectory": "../coverage",
"testEnvironment": "node"
}
}
}
2 changes: 1 addition & 1 deletion nestjs-mongoose/src/app.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('AppController', () => {

describe('root', () => {
it('should return "Hello World!"', () => {
return 'Hello World!';
expect(appController.getHello()).toBe('Hello World!');
});
});
});
9 changes: 7 additions & 2 deletions nestjs-mongoose/src/app.controller.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { Controller } from '@nestjs/common';
import { Controller, Get } from '@nestjs/common';
import { AppService } from './app.service';

@Controller()
export class AppController {
constructor(private readonly appService: AppService) {}
constructor(private readonly appService: AppService) { }

@Get()
getHello(): string {
return this.appService.getHello();
}
}
6 changes: 5 additions & 1 deletion nestjs-mongoose/src/app.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { Injectable } from '@nestjs/common';

@Injectable()
export class AppService {}
export class AppService {
getHello(): string {
return 'Hello World!';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export function TransformPaginationResponse(
): MethodDecorator {
return function (
target: Record<string, any>,
propertyKey: string | Symbol,
propertyKey: string | symbol,
descriptor: PropertyDescriptor
): void {
const classTransformer: ClassTransformer = new ClassTransformer();
Expand Down
18 changes: 18 additions & 0 deletions nestjs-mongoose/src/common/schema/auditable-entity.schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Prop } from "@nestjs/mongoose";
import { now } from "mongoose";

export class AuditableEntity {

@Prop({ default: now() })
createdAt?: Date;

@Prop()
createdBy?: string;

@Prop({ default: now() })
updatedAt?: Date;

@Prop()
updatedBy?: string;

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { IsString, IsNotEmpty } from 'class-validator';

export class CreatePostsDto {
export class CreatePostDto {
@IsString()
@IsNotEmpty()
title: string;
Expand All @@ -10,4 +10,4 @@ export class CreatePostsDto {
content: string;
}

export default CreatePostsDto;
export default CreatePostDto;
6 changes: 6 additions & 0 deletions nestjs-mongoose/src/posts/dto/get-posts.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { DataWithPaginationDto, PaginationParamsDto } from "../../common/pagination/pagination.dto";
import { PostDto } from "./post.dto";

export class GetPostsDto extends PaginationParamsDto { }

export class GetPostsResponseDto extends DataWithPaginationDto<PostDto> { }
13 changes: 13 additions & 0 deletions nestjs-mongoose/src/posts/dto/patch-post.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { IsOptional, IsString } from 'class-validator';

export class PatchPostDto {
@IsString()
@IsOptional()
title?: string;

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

export default PatchPostDto;
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Expose, Transform } from '@nestjs/class-transformer';
import { DataWithPaginationDto, PaginationParamsDto } from '../../common/pagination/pagination.dto';

export class PostsDto {
export class PostDto {
@Expose()
id: string;

Expand All @@ -18,7 +18,3 @@ export class PostsDto {
)
createdAt: Date | null;
}

export class GetPostsDto extends PaginationParamsDto { }

export class GetPostsResponseDto extends DataWithPaginationDto<PostsDto> { }
12 changes: 2 additions & 10 deletions nestjs-mongoose/src/posts/dto/update-post.dto.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
import { IsOptional, IsString } from 'class-validator';
import CreatePostDto from './create-post.dto';

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

@IsString()
@IsOptional()
content: string;
}
export class UpdatePostDto extends CreatePostDto { }

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

This file was deleted.

10 changes: 4 additions & 6 deletions nestjs-mongoose/src/posts/post.schema.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { now, Document } from 'mongoose';
import { Document } from 'mongoose';
import { AuditableEntity } from '../common/schema/auditable-entity.schema';

export type PostDocument = Post & Document;

@Schema()
export class Post {
@Schema({ timestamps: true })
export class Post extends AuditableEntity {
@Prop()
title: string;

@Prop()
content: string;

@Prop({ default: now() })
createdAt: Date;
}

export const PostSchema = SchemaFactory.createForClass(Post);
Expand Down
27 changes: 14 additions & 13 deletions nestjs-mongoose/src/posts/posts.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,40 +11,41 @@ import {
Put,
Query,
} from '@nestjs/common';
import CreatePostsDto from './dto/create-posts.dto';
import CreatePostDto from './dto/create-post.dto';
import ParamsWithId from '../utils/paramsWithId';
import UpdatePostDto from './dto/update-post.dto';
import { UpdatePostsDto } from './dto/update-posts.dto';
import { PatchPostDto } from './dto/patch-post.dto';
import { TransformPlainToClass } from '@nestjs/class-transformer';
import { GetPostsDto, PostsDto } from './dto/posts.dto';
import { PostDto } from './dto/post.dto';
import { ApiBody, ApiTags } from '@nestjs/swagger';
import { swaggerConfig } from '../swagger.config';

import { TransformPaginationResponse } from '../common/pagination/transform-pagination-response.decorator';
import UpdatePostDto from './dto/update-post.dto';
import { GetPostsDto } from './dto/get-posts.dto';

@ApiTags('Posts')
@Controller('posts')
export default class PostsController {
constructor(private readonly postsService: PostsService) { }

@Get()
@TransformPaginationResponse(PostsDto, { excludeExtraneousValues: true })
@TransformPaginationResponse(PostDto, { excludeExtraneousValues: true })
async getAllPosts(@Query() query: GetPostsDto) {
return this.postsService.findAll(query);
}

@swaggerConfig.param.id
@Get(':id')
@TransformPlainToClass(PostsDto, { excludeExtraneousValues: true })
@TransformPlainToClass(PostDto, { excludeExtraneousValues: true })
async getPostById(@Param() { id }: ParamsWithId) {
return this.postsService.findOne(id);
}

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

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

@swaggerConfig.param.id
@ApiBody(swaggerConfig.body)
@Patch(':id')
@TransformPlainToClass(UpdatePostsDto, { excludeExtraneousValues: true })
@TransformPlainToClass(PostDto, { excludeExtraneousValues: true })
async partialUpdatePost(
@Param() { id }: ParamsWithId,
@Body() post: UpdatePostDto,
@Body() post: PatchPostDto,
) {
return this.postsService.partialUpdate(id, post);
}
Expand Down
Loading