add content service and core config
This commit is contained in:
parent
9ac2fd8f44
commit
57c72e010b
@ -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 {}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
58
src/modules/content/services/post.service.ts
Normal file
58
src/modules/content/services/post.service.ts
Normal file
@ -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;
|
||||
}
|
||||
}
|
31
src/modules/core/services/config.service.ts
Normal file
31
src/modules/core/services/config.service.ts
Normal file
@ -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<string, any> = {
|
||||
name: 'ray',
|
||||
};
|
||||
|
||||
@Injectable()
|
||||
export class ConfigService {
|
||||
get<T>(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 {}
|
Loading…
Reference in New Issue
Block a user