From 607d3d86be4fc41c5f1f15d30a290ed7d29c7cea Mon Sep 17 00:00:00 2001 From: Jeremy Yin Date: Thu, 27 Jun 2019 22:22:50 +0800 Subject: [PATCH] =?UTF-8?q?=E7=8E=B0=E5=9C=A8=E5=8F=AF=E4=BB=A5=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E5=AE=9E=E4=BD=93=E6=95=B0=E6=8D=AE=E8=AE=B0=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/modules/post/post.controller.ts | 14 ++++++++++++-- src/modules/post/post.module.ts | 7 ++++++- src/modules/post/post.service.ts | 15 ++++++++++++++- 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/src/modules/post/post.controller.ts b/src/modules/post/post.controller.ts index 5ef33dd..51bebea 100644 --- a/src/modules/post/post.controller.ts +++ b/src/modules/post/post.controller.ts @@ -1,4 +1,14 @@ -import { Controller } from '@nestjs/common'; +import { Controller, Post, Body } from '@nestjs/common'; +import { PostService } from './post.service'; @Controller('posts') -export class PostController {} +export class PostController { + constructor( + private readonly postService: PostService, + ) { } + + @Post() + async store(@Body() data) { + return await this.postService.store(data); + } +} diff --git a/src/modules/post/post.module.ts b/src/modules/post/post.module.ts index af735ef..a43b068 100644 --- a/src/modules/post/post.module.ts +++ b/src/modules/post/post.module.ts @@ -1,9 +1,14 @@ import { Module } from '@nestjs/common'; import { PostController } from './post.controller'; import { PostService } from './post.service'; +import { TypeOrmModule } from '@nestjs/typeorm'; +import { Post } from './post.entity'; @Module({ + imports: [ + TypeOrmModule.forFeature([Post]), + ], controllers: [PostController], - providers: [PostService] + providers: [PostService], }) export class PostModule {} diff --git a/src/modules/post/post.service.ts b/src/modules/post/post.service.ts index 1e36a92..8a4d974 100644 --- a/src/modules/post/post.service.ts +++ b/src/modules/post/post.service.ts @@ -1,4 +1,17 @@ import { Injectable } from '@nestjs/common'; +import { Repository } from 'typeorm'; +import { Post } from './post.entity'; +import { InjectRepository } from '@nestjs/typeorm'; @Injectable() -export class PostService {} +export class PostService { + constructor( + @InjectRepository(Post) + private readonly postRepository: Repository, + ) { } + + async store(data) { + const entity = await this.postRepository.create(data); + return await this.postRepository.save(entity); + } +}