From 57c72e010b33828442578ed2384a6ad7411b8ce8 Mon Sep 17 00:00:00 2001 From: liuyi Date: Mon, 12 May 2025 15:26:49 +0800 Subject: [PATCH] add content service and core config --- src/modules/content/content.module.ts | 4 ++ .../content/controllers/post.controller.ts | 54 ++++------------- src/modules/content/services/post.service.ts | 58 +++++++++++++++++++ src/modules/core/services/config.service.ts | 31 ++++++++++ 4 files changed, 105 insertions(+), 42 deletions(-) create mode 100644 src/modules/content/services/post.service.ts create mode 100644 src/modules/core/services/config.service.ts diff --git a/src/modules/content/content.module.ts b/src/modules/content/content.module.ts index 2c52e1e..9402aa8 100644 --- a/src/modules/content/content.module.ts +++ b/src/modules/content/content.module.ts @@ -1,8 +1,12 @@ import { Module } from '@nestjs/common'; +import { PostService } from '@/modules/content/services/post.service'; + import { PostController } from './controllers/post.controller'; @Module({ controllers: [PostController], + providers: [PostService], + exports: [PostService], }) export class ContentModule {} diff --git a/src/modules/content/controllers/post.controller.ts b/src/modules/content/controllers/post.controller.ts index e6bbaac..9b2d01f 100644 --- a/src/modules/content/controllers/post.controller.ts +++ b/src/modules/content/controllers/post.controller.ts @@ -3,43 +3,30 @@ import { Controller, Delete, Get, - NotFoundException, Param, + ParseIntPipe, Patch, Post, ValidationPipe, } from '@nestjs/common'; -import { isNil } from '@nestjs/common/utils/shared.utils'; - import { CreatePostDto } from '@/modules/content/dtos/create-post.dto'; import { UpdatePostDto } from '@/modules/content/dtos/update-post.dto'; -import { PostEntity } from '../types'; - -let posts: PostEntity[] = [ - { title: '第一篇文章标题', body: '第一篇文章内容' }, - { title: '第二篇文章标题', body: '第二篇文章内容' }, - { title: '第三篇文章标题', body: '第三篇文章内容' }, - { title: '第四篇文章标题', body: '第四篇文章内容' }, - { title: '第五篇文章标题', body: '第五篇文章内容' }, - { title: '第六篇文章标题', body: '第六篇文章内容' }, -].map((v, id) => ({ ...v, id })); +import { PostService } from '@/modules/content/services/post.service'; @Controller('posts') export class PostController { + constructor(private postService: PostService) {} + @Get() async index() { - return posts; + return this.postService.findAll(); } @Get(':id') - async show(@Param('id') id: number) { - const post = posts.find((item) => item.id === Number(id)); - if (isNil(post)) { - throw new NotFoundException(`the post with id ${id} not exits!`); - } - return post; + async show(@Param('id', new ParseIntPipe()) id: number) { + return this.postService.findOne(id); } @Post() @@ -55,12 +42,7 @@ export class PostController { ) data: CreatePostDto, ) { - const newPost: PostEntity = { - id: Math.max(...posts.map(({ id }) => id + 1)), - ...data, - }; - posts.push(newPost); - return newPost; + return this.postService.create(data); } @Patch() @@ -74,25 +56,13 @@ export class PostController { groups: ['update'], }), ) - { id, ...data }: UpdatePostDto, + data: UpdatePostDto, ) { - let toUpdate = posts.find((item) => item.id === Number(id)); - if (isNil(toUpdate)) { - throw new NotFoundException(`the post with id ${id} not exits!`); - } - - toUpdate = { ...toUpdate, ...data }; - posts = posts.map((item) => (item.id === Number(id) ? toUpdate : item)); - return toUpdate; + return this.postService.update(data); } @Delete(':id') - async delete(@Param('id') id: number) { - const toDelete = posts.find((item) => item.id === Number(id)); - if (isNil(toDelete)) { - throw new NotFoundException(`the post with id ${id} not exits!`); - } - posts = posts.filter((item) => item.id !== Number(id)); - return toDelete; + async delete(@Param('id', new ParseIntPipe()) id: number) { + return this.postService.delete(id); } } diff --git a/src/modules/content/services/post.service.ts b/src/modules/content/services/post.service.ts new file mode 100644 index 0000000..7fc81a3 --- /dev/null +++ b/src/modules/content/services/post.service.ts @@ -0,0 +1,58 @@ +import { Injectable, NotFoundException } from '@nestjs/common'; +import { isNil } from '@nestjs/common/utils/shared.utils'; + +import { CreatePostDto } from '@/modules/content/dtos/create-post.dto'; +import { UpdatePostDto } from '@/modules/content/dtos/update-post.dto'; +import { PostEntity } from '@/modules/content/types'; + +@Injectable() +export class PostService { + protected posts: PostEntity[] = [ + { title: '第一篇文章标题', body: '第一篇文章内容' }, + { title: '第二篇文章标题', body: '第二篇文章内容' }, + { title: '第三篇文章标题', body: '第三篇文章内容' }, + { title: '第四篇文章标题', body: '第四篇文章内容' }, + { title: '第五篇文章标题', body: '第五篇文章内容' }, + { title: '第六篇文章标题', body: '第六篇文章内容' }, + ].map((v, id) => ({ ...v, id })); + + async findAll() { + return this.posts; + } + + async findOne(id: number) { + const post = this.posts.find((item) => item.id === id); + if (isNil(post)) { + throw new NotFoundException(`the post with id ${id} not exits!`); + } + return post; + } + + async create(data: CreatePostDto) { + const newPost: PostEntity = { + id: Math.max(...this.posts.map(({ id }) => id + 1)), + ...data, + }; + this.posts.push(newPost); + return newPost; + } + + async update(data: UpdatePostDto) { + let toUpdate = this.posts.find((item) => item.id === data.id); + if (isNil(toUpdate)) { + throw new NotFoundException(`the post with id ${data.id} not exits!`); + } + toUpdate = { ...toUpdate, ...data }; + this.posts = this.posts.filter((item) => (item.id === data.id ? toUpdate : item)); + return toUpdate; + } + + async delete(id: number) { + const toDelete = this.posts.find((item) => item.id === id); + if (isNil(toDelete)) { + throw new NotFoundException(`the post with id ${id} not exits!`); + } + this.posts = this.posts.filter((item) => item.id !== id); + return toDelete; + } +} diff --git a/src/modules/core/services/config.service.ts b/src/modules/core/services/config.service.ts new file mode 100644 index 0000000..52296aa --- /dev/null +++ b/src/modules/core/services/config.service.ts @@ -0,0 +1,31 @@ +import { Global, Injectable, Module } from '@nestjs/common'; +import { get } from 'lodash'; + +import { AppController } from '@/app.controller'; +import { AppService } from '@/app.service'; +import { ContentModule } from '@/modules/content/content.module'; + +const config: Record = { + name: 'ray', +}; + +@Injectable() +export class ConfigService { + get(key: string, defaultValue?: T): T | undefined { + return get(config, key, defaultValue); + } +} + +@Global() +@Module({ + providers: [ConfigService], + exports: [ConfigService], +}) +export class CoreModule {} + +@Module({ + imports: [ContentModule, CoreModule], + providers: [AppService], + controllers: [AppController], +}) +export class AppModule {}