From 301501dbac4bb3d7bdbdca5137fecfed26f283bd Mon Sep 17 00:00:00 2001 From: renolation Date: Thu, 15 May 2025 14:44:16 +0700 Subject: [PATCH] error validation --- src/app.module.ts | 4 ++- .../authentication.controller.ts | 5 ++- src/main.ts | 12 ++++++- src/posts/dto/create-post.dto.ts | 1 + src/posts/dto/update-post.dto.ts | 4 +++ src/posts/entities/post.entity.ts | 1 + src/posts/exception/postNotFund.exception.ts | 7 ++++ src/posts/posts.controller.spec.ts | 20 +++++++++++ src/posts/posts.controller.ts | 34 +++++++++++++++++++ src/posts/posts.module.ts | 9 +++++ src/posts/posts.service.spec.ts | 18 ++++++++++ src/posts/posts.service.ts | 26 ++++++++++++++ src/users/user.entity.ts | 4 ++- src/utils/exceptionsLogger.filter.ts | 10 ++++++ 14 files changed, 151 insertions(+), 4 deletions(-) create mode 100644 src/posts/dto/create-post.dto.ts create mode 100644 src/posts/dto/update-post.dto.ts create mode 100644 src/posts/entities/post.entity.ts create mode 100644 src/posts/exception/postNotFund.exception.ts create mode 100644 src/posts/posts.controller.spec.ts create mode 100644 src/posts/posts.controller.ts create mode 100644 src/posts/posts.module.ts create mode 100644 src/posts/posts.service.spec.ts create mode 100644 src/posts/posts.service.ts create mode 100644 src/utils/exceptionsLogger.filter.ts diff --git a/src/app.module.ts b/src/app.module.ts index cbbc5d2..c0b89fc 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -6,6 +6,7 @@ import { ConfigModule, ConfigService } from '@nestjs/config'; import * as Joi from 'joi'; import {UsersModule} from "./users/user.module"; import {AuthenticationModule} from "./authentication/authentication.module"; +import { PostsModule } from './posts/posts.module'; @Module({ imports: [ @@ -33,7 +34,8 @@ import {AuthenticationModule} from "./authentication/authentication.module"; }) }), UsersModule, - AuthenticationModule + AuthenticationModule, + PostsModule ], controllers: [AppController], providers: [AppService], diff --git a/src/authentication/authentication.controller.ts b/src/authentication/authentication.controller.ts index 38228da..373e955 100644 --- a/src/authentication/authentication.controller.ts +++ b/src/authentication/authentication.controller.ts @@ -7,7 +7,7 @@ import { UseGuards, Get, ClassSerializerInterceptor, - UseInterceptors, + UseInterceptors, SerializeOptions, } from '@nestjs/common'; import {AuthenticationService} from './authentication.service'; import RegisterDto from './dto/register.dto'; @@ -21,6 +21,9 @@ import { UsersService } from 'src/users/user.service'; @Controller('authentication') @UseInterceptors(ClassSerializerInterceptor) +@SerializeOptions({ + strategy: 'excludeAll' +}) export class AuthenticationController { constructor( private readonly authenticationService: AuthenticationService, diff --git a/src/main.ts b/src/main.ts index 2a4c51b..cad4dc4 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,10 +1,20 @@ -import {NestFactory} from '@nestjs/core'; +import {HttpAdapterHost, NestFactory, Reflector} from '@nestjs/core'; import {AppModule} from './app.module'; import * as cookieParser from 'cookie-parser'; +import {ClassSerializerInterceptor, ValidationPipe} from "@nestjs/common"; +import {ExceptionsLoggerFilter} from "./utils/exceptionsLogger.filter"; async function bootstrap() { const app = await NestFactory.create(AppModule); app.use(cookieParser()); + + app.useGlobalPipes(new ValidationPipe()); + const {httpAdapter} = app.get(HttpAdapterHost); + app.useGlobalFilters(new ExceptionsLoggerFilter(httpAdapter)); + + app.useGlobalInterceptors(new ClassSerializerInterceptor( + app.get(Reflector)) + ); await app.listen(process.env.PORT ?? 3000); } diff --git a/src/posts/dto/create-post.dto.ts b/src/posts/dto/create-post.dto.ts new file mode 100644 index 0000000..1a2b3c5 --- /dev/null +++ b/src/posts/dto/create-post.dto.ts @@ -0,0 +1 @@ +export class CreatePostDto {} diff --git a/src/posts/dto/update-post.dto.ts b/src/posts/dto/update-post.dto.ts new file mode 100644 index 0000000..7545cd5 --- /dev/null +++ b/src/posts/dto/update-post.dto.ts @@ -0,0 +1,4 @@ +import { PartialType } from '@nestjs/swagger'; +import { CreatePostDto } from './create-post.dto'; + +export class UpdatePostDto extends PartialType(CreatePostDto) {} diff --git a/src/posts/entities/post.entity.ts b/src/posts/entities/post.entity.ts new file mode 100644 index 0000000..54fbf02 --- /dev/null +++ b/src/posts/entities/post.entity.ts @@ -0,0 +1 @@ +export class Post {} diff --git a/src/posts/exception/postNotFund.exception.ts b/src/posts/exception/postNotFund.exception.ts new file mode 100644 index 0000000..11c6935 --- /dev/null +++ b/src/posts/exception/postNotFund.exception.ts @@ -0,0 +1,7 @@ +import { NotFoundException } from '@nestjs/common'; + +class PostNotFoundException extends NotFoundException { + constructor(postId: number) { + super(`Post with id ${postId} not found`); + } +} \ No newline at end of file diff --git a/src/posts/posts.controller.spec.ts b/src/posts/posts.controller.spec.ts new file mode 100644 index 0000000..7027224 --- /dev/null +++ b/src/posts/posts.controller.spec.ts @@ -0,0 +1,20 @@ +import { Test, TestingModule } from '@nestjs/testing'; +import { PostsController } from './posts.controller'; +import { PostsService } from './posts.service'; + +describe('PostsController', () => { + let controller: PostsController; + + beforeEach(async () => { + const module: TestingModule = await Test.createTestingModule({ + controllers: [PostsController], + providers: [PostsService], + }).compile(); + + controller = module.get(PostsController); + }); + + it('should be defined', () => { + expect(controller).toBeDefined(); + }); +}); diff --git a/src/posts/posts.controller.ts b/src/posts/posts.controller.ts new file mode 100644 index 0000000..6e719cd --- /dev/null +++ b/src/posts/posts.controller.ts @@ -0,0 +1,34 @@ +import { Controller, Get, Post, Body, Patch, Param, Delete } from '@nestjs/common'; +import { PostsService } from './posts.service'; +import { CreatePostDto } from './dto/create-post.dto'; +import { UpdatePostDto } from './dto/update-post.dto'; + +@Controller('posts') +export class PostsController { + constructor(private readonly postsService: PostsService) {} + + @Post() + create(@Body() createPostDto: CreatePostDto) { + return this.postsService.create(createPostDto); + } + + @Get() + findAll() { + return this.postsService.findAll(); + } + + @Get(':id') + findOne(@Param('id') id: string) { + return this.postsService.findOne(+id); + } + + @Patch(':id') + update(@Param('id') id: string, @Body() updatePostDto: UpdatePostDto) { + return this.postsService.update(+id, updatePostDto); + } + + @Delete(':id') + remove(@Param('id') id: string) { + return this.postsService.remove(+id); + } +} diff --git a/src/posts/posts.module.ts b/src/posts/posts.module.ts new file mode 100644 index 0000000..0a83378 --- /dev/null +++ b/src/posts/posts.module.ts @@ -0,0 +1,9 @@ +import { Module } from '@nestjs/common'; +import { PostsService } from './posts.service'; +import { PostsController } from './posts.controller'; + +@Module({ + controllers: [PostsController], + providers: [PostsService], +}) +export class PostsModule {} diff --git a/src/posts/posts.service.spec.ts b/src/posts/posts.service.spec.ts new file mode 100644 index 0000000..e152158 --- /dev/null +++ b/src/posts/posts.service.spec.ts @@ -0,0 +1,18 @@ +import { Test, TestingModule } from '@nestjs/testing'; +import { PostsService } from './posts.service'; + +describe('PostsService', () => { + let service: PostsService; + + beforeEach(async () => { + const module: TestingModule = await Test.createTestingModule({ + providers: [PostsService], + }).compile(); + + service = module.get(PostsService); + }); + + it('should be defined', () => { + expect(service).toBeDefined(); + }); +}); diff --git a/src/posts/posts.service.ts b/src/posts/posts.service.ts new file mode 100644 index 0000000..bc90f24 --- /dev/null +++ b/src/posts/posts.service.ts @@ -0,0 +1,26 @@ +import { Injectable } from '@nestjs/common'; +import { CreatePostDto } from './dto/create-post.dto'; +import { UpdatePostDto } from './dto/update-post.dto'; + +@Injectable() +export class PostsService { + create(createPostDto: CreatePostDto) { + return 'This action adds a new post'; + } + + findAll() { + return `This action returns all posts`; + } + + findOne(id: number) { + return `This action returns a #${id} post`; + } + + update(id: number, updatePostDto: UpdatePostDto) { + return `This action updates a #${id} post`; + } + + remove(id: number) { + return `This action removes a #${id} post`; + } +} diff --git a/src/users/user.entity.ts b/src/users/user.entity.ts index b99ea5d..520b99a 100644 --- a/src/users/user.entity.ts +++ b/src/users/user.entity.ts @@ -1,5 +1,5 @@ import {Column, Entity, PrimaryGeneratedColumn} from 'typeorm'; -import {Exclude} from 'class-transformer'; +import {Exclude, Expose} from 'class-transformer'; @Entity() class User { @@ -7,9 +7,11 @@ class User { public id?: number; @Column({unique: true}) + @Expose() public email: string; @Column() + @Expose() public name: string; @Column() diff --git a/src/utils/exceptionsLogger.filter.ts b/src/utils/exceptionsLogger.filter.ts new file mode 100644 index 0000000..87dbeb7 --- /dev/null +++ b/src/utils/exceptionsLogger.filter.ts @@ -0,0 +1,10 @@ +import { Catch, ArgumentsHost } from '@nestjs/common'; +import { BaseExceptionFilter } from '@nestjs/core'; + +@Catch() +export class ExceptionsLoggerFilter extends BaseExceptionFilter { + catch(exception: unknown, host: ArgumentsHost) { + console.log('Exception thrown', exception); + super.catch(exception, host); + } +} \ No newline at end of file