From 234b2984a6dacd43d2e167846e1bd4f2c22fcdf6 Mon Sep 17 00:00:00 2001 From: Jeremy Yin Date: Thu, 27 Jun 2019 23:08:36 +0800 Subject: [PATCH] =?UTF-8?q?post=20entity=E7=9A=84=E5=A2=9E=E5=88=A0?= =?UTF-8?q?=E6=94=B9=E6=9F=A5=EF=BC=8C=E8=B7=AF=E7=94=B1=E6=89=BE=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1=EF=BC=8C=E6=9C=8D=E5=8A=A1=E6=89=BE=E5=BA=93=EF=BC=8C?= =?UTF-8?q?=E5=BA=93=E7=BB=91=E5=AE=9A=E5=AE=9E=E4=BD=93=E5=B9=B6=E6=8F=90?= =?UTF-8?q?=E4=BE=9B=E6=96=B9=E6=B3=95=EF=BC=9B=E5=A2=9E=E5=8A=A0dto?= =?UTF-8?q?=E6=8F=90=E4=BE=9B=E6=8F=90=E4=BA=A4=E6=95=B0=E6=8D=AE=E5=AD=97?= =?UTF-8?q?=E6=AE=B5=E6=8F=90=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/modules/post/post.controller.ts | 25 +++++++++++++++++++++++-- src/modules/post/post.dto.ts | 4 ++++ src/modules/post/post.entity.ts | 2 +- src/modules/post/post.service.ts | 23 ++++++++++++++++++++++- 4 files changed, 50 insertions(+), 4 deletions(-) create mode 100644 src/modules/post/post.dto.ts diff --git a/src/modules/post/post.controller.ts b/src/modules/post/post.controller.ts index 51bebea..846493a 100644 --- a/src/modules/post/post.controller.ts +++ b/src/modules/post/post.controller.ts @@ -1,5 +1,6 @@ -import { Controller, Post, Body } from '@nestjs/common'; +import { Controller, Post, Body, Get, Param, Put, Delete } from '@nestjs/common'; import { PostService } from './post.service'; +import { PostDto } from './post.dto'; @Controller('posts') export class PostController { @@ -8,7 +9,27 @@ export class PostController { ) { } @Post() - async store(@Body() data) { + async store(@Body() data: PostDto) { return await this.postService.store(data); } + + @Get() + async index() { + return await this.postService.index(); + } + + @Get(':id') + async show(@Param() id: string) { + return await this.postService.show(id); + } + + @Put(':id') + async update(@Param() id: string, @Body() data: Partial) { + return await this.postService.update(id, data); + } + + @Delete(':id') + async destroy(@Param() id: string) { + return await this.postService.destroy(id); + } } diff --git a/src/modules/post/post.dto.ts b/src/modules/post/post.dto.ts new file mode 100644 index 0000000..87eb738 --- /dev/null +++ b/src/modules/post/post.dto.ts @@ -0,0 +1,4 @@ +export class PostDto { + readonly title: string; + readonly body: string; +} \ No newline at end of file diff --git a/src/modules/post/post.entity.ts b/src/modules/post/post.entity.ts index 8ca7bca..381ee46 100644 --- a/src/modules/post/post.entity.ts +++ b/src/modules/post/post.entity.ts @@ -8,7 +8,7 @@ export class Post { @Column() title: string; - @Column('longtext') + @Column('longtext', {nullable: true}) body: string; @CreateDateColumn() diff --git a/src/modules/post/post.service.ts b/src/modules/post/post.service.ts index 8a4d974..610787b 100644 --- a/src/modules/post/post.service.ts +++ b/src/modules/post/post.service.ts @@ -2,6 +2,7 @@ import { Injectable } from '@nestjs/common'; import { Repository } from 'typeorm'; import { Post } from './post.entity'; import { InjectRepository } from '@nestjs/typeorm'; +import { PostDto } from './post.dto'; @Injectable() export class PostService { @@ -10,8 +11,28 @@ export class PostService { private readonly postRepository: Repository, ) { } - async store(data) { + async store(data: PostDto) { const entity = await this.postRepository.create(data); return await this.postRepository.save(entity); } + + async index() { + const entities = await this.postRepository.find(); + return entities; + } + + async show(id: string) { + const entity = await this.postRepository.findOne(id); + return entity; + } + + async update(id: string, data: Partial) { + const result = await this.postRepository.update(id, data); + return result; + } + + async destroy(id: string) { + const result = await this.postRepository.delete(id); + return result; + } }