add content
This commit is contained in:
+2
-1
@@ -1,8 +1,9 @@
|
||||
import { NestFactory } from '@nestjs/core';
|
||||
|
||||
import { AppModule } from './app.module';
|
||||
import { FastifyAdapter, NestFastifyApplication } from '@nestjs/platform-fastify';
|
||||
|
||||
import { AppModule } from './app.module';
|
||||
|
||||
async function bootstrap() {
|
||||
const app = await NestFactory.create<NestFastifyApplication>(AppModule, new FastifyAdapter(), {
|
||||
cors: true,
|
||||
|
||||
@@ -1,8 +1,19 @@
|
||||
import { Controller, Get } from '@nestjs/common';
|
||||
import {
|
||||
Body,
|
||||
Controller,
|
||||
Delete,
|
||||
Get,
|
||||
NotFoundException,
|
||||
Param,
|
||||
Patch,
|
||||
Post,
|
||||
} from '@nestjs/common';
|
||||
|
||||
import { isNil } from '@nestjs/common/utils/shared.utils';
|
||||
|
||||
import { PostEntity } from '../types';
|
||||
|
||||
const posts: PostEntity[] = [
|
||||
let posts: PostEntity[] = [
|
||||
{ title: '第一篇文章标题', body: '第一篇文章内容' },
|
||||
{ title: '第二篇文章标题', body: '第二篇文章内容' },
|
||||
{ title: '第三篇文章标题', body: '第三篇文章内容' },
|
||||
@@ -17,4 +28,45 @@ export class PostController {
|
||||
async index() {
|
||||
return posts;
|
||||
}
|
||||
|
||||
@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;
|
||||
}
|
||||
|
||||
@Post()
|
||||
async store(@Body() data: PostEntity) {
|
||||
const newPost: PostEntity = {
|
||||
id: Math.max(...posts.map(({ id }) => id + 1)),
|
||||
...data,
|
||||
};
|
||||
posts.push(newPost);
|
||||
return newPost;
|
||||
}
|
||||
|
||||
@Patch()
|
||||
async update(@Body() data: PostEntity) {
|
||||
let toUpdate = posts.find((item) => item.id === Number(data.id));
|
||||
if (isNil(toUpdate)) {
|
||||
throw new NotFoundException(`the post with id ${data.id} not exits!`);
|
||||
}
|
||||
|
||||
toUpdate = { ...toUpdate, ...data };
|
||||
posts = posts.map((item) => (item.id === Number(data.id) ? toUpdate : item));
|
||||
return toUpdate;
|
||||
}
|
||||
|
||||
@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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user