feat(learning): 完成NestJS核心概念和自定义提供者的学习
- 引入并配置了Fastify替代Express作为HTTP服务器,提高应用性能。 - 实现了PostController及其CRUD操作,包括请求方法和参数处理。 - 创建并使用CreatePostDto和UpdatePostDto进行请求数据验证。 - 完成了多种自定义提供者的实现和应用,如值提供者、类提供者和工厂提供者。 - 添加了一些全局类型定义,如RecordAny和BaseType,用于增强代码的可读性和健壮性。 - 通过ConfigService添加和管理新的环境配置项。
This commit is contained in:
@@ -0,0 +1 @@
|
||||
export * from './post.controller';
|
||||
@@ -0,0 +1,60 @@
|
||||
import { Body, Controller, Delete, Get, Param, Patch, Post, ValidationPipe } from '@nestjs/common';
|
||||
|
||||
import { CreatePostDto, UpdatePostDto } from '../dtos';
|
||||
import { PostService } from '../services';
|
||||
|
||||
/**
|
||||
* 文章控制器
|
||||
* 负责处理与文章相关的请求,如获取文章列表、创建新文章等。
|
||||
*/
|
||||
@Controller('post')
|
||||
export class PostController {
|
||||
constructor(private postService: PostService) {}
|
||||
|
||||
@Get()
|
||||
async index() {
|
||||
return this.postService.findAll();
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
async show(@Param('id') id: number) {
|
||||
return this.postService.findOne(id);
|
||||
}
|
||||
|
||||
@Post()
|
||||
async store(
|
||||
@Body(
|
||||
new ValidationPipe({
|
||||
transform: true,
|
||||
forbidNonWhitelisted: true,
|
||||
forbidUnknownValues: true,
|
||||
validationError: { target: false },
|
||||
groups: ['create'],
|
||||
}),
|
||||
)
|
||||
data: CreatePostDto,
|
||||
) {
|
||||
return this.postService.create(data);
|
||||
}
|
||||
|
||||
@Patch()
|
||||
async update(
|
||||
@Body(
|
||||
new ValidationPipe({
|
||||
transform: true,
|
||||
forbidNonWhitelisted: true,
|
||||
forbidUnknownValues: true,
|
||||
validationError: { target: false },
|
||||
groups: ['update'],
|
||||
}),
|
||||
)
|
||||
data: UpdatePostDto,
|
||||
) {
|
||||
return this.postService.update(data);
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
async delete(@Param('id') id: number) {
|
||||
return this.postService.delete(id);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user