add: 支持添加评论

This commit is contained in:
SunnyQjm committed 2019-10-29 20:44:41 +08:00
1 parent e62bd09bb4
commit 2caed93d46
7 files changed
+55 -8

No files matched your search

+1
View File
@@ -17,6 +17,7 @@ import { AdminModule } from './admin/admin.module';
password: 'LXDW4RjtyYdGKh64',
database: 'sunny-blog',
entities: [__dirname + '/**/*.entity{.ts,.js}'],
synchronize: true,
},
), UserModule, AuthModule, PostModule, AdminModule],
controllers: [AppController],
+1 -2
View File
@@ -1,6 +1,5 @@
import { Column, Entity, ManyToOne, OneToOne, PrimaryGeneratedColumn } from 'typeorm';
import { Post } from './post.entity';
import { User } from './user.entity';
/**
* 评论表
@@ -8,7 +7,7 @@ import { User } from './user.entity';
@Entity()
export class Comment {
@PrimaryGeneratedColumn()
id: number;
id?: number;
@Column()
name: string;
+1 -1
View File
@@ -32,7 +32,7 @@ export class Post {
comments?: Comment[]; // 评论
@ManyToOne(type => User, user => user.posts)
owner: User;
owner?: User;
@CreateDateColumn()
createdAt?: string;
+1 -1
View File
@@ -33,7 +33,7 @@ export class ResponseUtil {
}
}
static fail(message, code = StatusCode.COMMENT_ERROR) {
static fail(message = '请求失败', code = StatusCode.COMMENT_ERROR) {
return {
statusCode: code,
error: '',
+15
View File
@@ -0,0 +1,15 @@
import { IsNumber, IsString } from 'class-validator';
export class AddCommentDto {
@IsString()
name: string;
@IsString()
email: string;
@IsString()
content: string;
@IsNumber()
postId: number;
}
+17 -1
View File
@@ -1,6 +1,7 @@
import { Controller, Get, Param, Query } from '@nestjs/common';
import { Body, Controller, Get, Param, Post, Query } from '@nestjs/common';
import { PostService } from './post.service';
import { ResponseUtil } from '../model/response-util';
import { AddCommentDto } from './dto/add-comment.dto';
@Controller('post')
export class PostController {
@@ -27,4 +28,19 @@ export class PostController {
const post = await this.postService.getPostById(id);
return ResponseUtil.success(post);
}
@Post('addComment')
async addComment(@Body() addCommentDto: AddCommentDto) {
const result = await this.postService.saveComment(
addCommentDto.postId,
addCommentDto.name,
addCommentDto.email,
addCommentDto.content,
);
if (result) {
return ResponseUtil.success();
} else {
return ResponseUtil.fail();
}
}
}
+19 -3
View File
@@ -80,6 +80,7 @@ export class PostService {
.skip((page - 1) * size)
.limit(size)
.leftJoinAndSelect('post.tags', 'tag')
.leftJoinAndSelect('post.comments', 'comments')
.getMany();
}
@@ -94,6 +95,7 @@ export class PostService {
.skip((page - 1) * size)
.limit(size)
.leftJoinAndSelect('post.tags', 'tag')
.leftJoinAndSelect('post.comments', 'comments')
.where('tag.id = :id', { id: tagId })
.getMany();
}
@@ -106,7 +108,7 @@ export class PostService {
return await this.postRepository.findOne({
id: postId,
}, {
relations: ['tags'],
relations: ['tags', 'comments'],
});
}
@@ -114,6 +116,20 @@ export class PostService {
/////// 评论相关
////////////////////////////////////////////////////////////////////
// async saveComment(postId: number, name: string, email: string, content: string) {
// }
async saveComment(postId: number, name: string, email: string, content: string) {
const post: Post = await this.postRepository.findOne({
id: postId,
});
if (!post) {
return null;
}
const comment: Comment = {
post,
name,
email,
content,
};
return await this.commentRepository.save(comment);
}
}