add content
This commit is contained in:
parent
2e03eca42d
commit
65b52d8b6e
10
.eslintrc.js
10
.eslintrc.js
@ -11,13 +11,7 @@ module.exports = {
|
|||||||
node: true,
|
node: true,
|
||||||
jest: true,
|
jest: true,
|
||||||
},
|
},
|
||||||
plugins: [
|
plugins: ['@typescript-eslint', 'jest', 'prettier', 'import', 'unused-imports'],
|
||||||
'@typescript-eslint',
|
|
||||||
'jest',
|
|
||||||
'prettier',
|
|
||||||
'import',
|
|
||||||
'unused-imports',
|
|
||||||
],
|
|
||||||
extends: [
|
extends: [
|
||||||
// airbnb规范
|
// airbnb规范
|
||||||
// https://github.com/airbnb/javascript/tree/master/packages/eslint-config-airbnb
|
// https://github.com/airbnb/javascript/tree/master/packages/eslint-config-airbnb
|
||||||
@ -102,7 +96,7 @@ module.exports = {
|
|||||||
// https://github.com/sweepline/eslint-plugin-unused-imports
|
// https://github.com/sweepline/eslint-plugin-unused-imports
|
||||||
'unused-imports/no-unused-imports': 1,
|
'unused-imports/no-unused-imports': 1,
|
||||||
'unused-imports/no-unused-vars': [
|
'unused-imports/no-unused-vars': [
|
||||||
'error',
|
'warn',
|
||||||
{
|
{
|
||||||
vars: 'all',
|
vars: 'all',
|
||||||
args: 'none',
|
args: 'none',
|
||||||
|
@ -1,8 +1,9 @@
|
|||||||
import { NestFactory } from '@nestjs/core';
|
import { NestFactory } from '@nestjs/core';
|
||||||
|
|
||||||
import { AppModule } from './app.module';
|
|
||||||
import { FastifyAdapter, NestFastifyApplication } from '@nestjs/platform-fastify';
|
import { FastifyAdapter, NestFastifyApplication } from '@nestjs/platform-fastify';
|
||||||
|
|
||||||
|
import { AppModule } from './app.module';
|
||||||
|
|
||||||
async function bootstrap() {
|
async function bootstrap() {
|
||||||
const app = await NestFactory.create<NestFastifyApplication>(AppModule, new FastifyAdapter(), {
|
const app = await NestFactory.create<NestFastifyApplication>(AppModule, new FastifyAdapter(), {
|
||||||
cors: true,
|
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';
|
import { PostEntity } from '../types';
|
||||||
|
|
||||||
const posts: PostEntity[] = [
|
let posts: PostEntity[] = [
|
||||||
{ title: '第一篇文章标题', body: '第一篇文章内容' },
|
{ title: '第一篇文章标题', body: '第一篇文章内容' },
|
||||||
{ title: '第二篇文章标题', body: '第二篇文章内容' },
|
{ title: '第二篇文章标题', body: '第二篇文章内容' },
|
||||||
{ title: '第三篇文章标题', body: '第三篇文章内容' },
|
{ title: '第三篇文章标题', body: '第三篇文章内容' },
|
||||||
@ -17,4 +28,45 @@ export class PostController {
|
|||||||
async index() {
|
async index() {
|
||||||
return posts;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user