post entity的增删改查,路由找服务,服务找库,库绑定实体并提供方法;增加dto提供提交数据字段提示
This commit is contained in:
parent
607d3d86be
commit
234b2984a6
|
@ -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 { PostService } from './post.service';
|
||||||
|
import { PostDto } from './post.dto';
|
||||||
|
|
||||||
@Controller('posts')
|
@Controller('posts')
|
||||||
export class PostController {
|
export class PostController {
|
||||||
|
@ -8,7 +9,27 @@ export class PostController {
|
||||||
) { }
|
) { }
|
||||||
|
|
||||||
@Post()
|
@Post()
|
||||||
async store(@Body() data) {
|
async store(@Body() data: PostDto) {
|
||||||
return await this.postService.store(data);
|
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<PostDto>) {
|
||||||
|
return await this.postService.update(id, data);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Delete(':id')
|
||||||
|
async destroy(@Param() id: string) {
|
||||||
|
return await this.postService.destroy(id);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,4 @@
|
||||||
|
export class PostDto {
|
||||||
|
readonly title: string;
|
||||||
|
readonly body: string;
|
||||||
|
}
|
|
@ -8,7 +8,7 @@ export class Post {
|
||||||
@Column()
|
@Column()
|
||||||
title: string;
|
title: string;
|
||||||
|
|
||||||
@Column('longtext')
|
@Column('longtext', {nullable: true})
|
||||||
body: string;
|
body: string;
|
||||||
|
|
||||||
@CreateDateColumn()
|
@CreateDateColumn()
|
||||||
|
|
|
@ -2,6 +2,7 @@ import { Injectable } from '@nestjs/common';
|
||||||
import { Repository } from 'typeorm';
|
import { Repository } from 'typeorm';
|
||||||
import { Post } from './post.entity';
|
import { Post } from './post.entity';
|
||||||
import { InjectRepository } from '@nestjs/typeorm';
|
import { InjectRepository } from '@nestjs/typeorm';
|
||||||
|
import { PostDto } from './post.dto';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class PostService {
|
export class PostService {
|
||||||
|
@ -10,8 +11,28 @@ export class PostService {
|
||||||
private readonly postRepository: Repository<Post>,
|
private readonly postRepository: Repository<Post>,
|
||||||
) { }
|
) { }
|
||||||
|
|
||||||
async store(data) {
|
async store(data: PostDto) {
|
||||||
const entity = await this.postRepository.create(data);
|
const entity = await this.postRepository.create(data);
|
||||||
return await this.postRepository.save(entity);
|
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<PostDto>) {
|
||||||
|
const result = await this.postRepository.update(id, data);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
async destroy(id: string) {
|
||||||
|
const result = await this.postRepository.delete(id);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue