Bläddra i källkod

写评论及其展示

wuyi 2 år sedan
förälder
incheckning
963b07ce71

+ 9 - 10
src/comment/comment.controller.ts

@@ -1,20 +1,14 @@
 import {
     Controller,
     Put,
-    Get,
     Body,
-    Param,
-    HttpStatus,
-    NotFoundException,
-    Delete,
-    BadRequestException,
-    Req,
     Post
 } from '@nestjs/common'
 import { ApiBearerAuth, ApiTags } from '@nestjs/swagger'
 import { CommentService } from './comment.service'
 import { Comment } from './entities/comment.entity'
 import { PageRequest } from 'src/common/dto/page-request'
+import { CommentDto } from './dto/comment.dto'
 
 @ApiTags('comment')
 @Controller('/comment')
@@ -23,9 +17,14 @@ export class CommentController {
 
     constructor(private readonly commentService: CommentService) { }
 
-    @Post('/:id')
-    public async list(@Body() page: PageRequest<Comment>, @Param('id') id: number) {
-        return await this.commentService.findAll(page,id)
+    @Post()
+    public async list(@Body() page: PageRequest<Comment>) {
+        return await this.commentService.findAll(page)
+    }
+
+    @Put()
+    public async create(@Body() commentDto: CommentDto) {
+        return await this.commentService.create(commentDto)
     }
 
 }

+ 60 - 9
src/comment/comment.service.ts

@@ -1,11 +1,14 @@
 import {
     Injectable
 } from '@nestjs/common'
-import { In, Repository, UpdateResult } from 'typeorm'
+import { FindManyOptions, FindOptionsOrder, In, Repository, UpdateResult } from 'typeorm'
 import { InjectRepository } from '@nestjs/typeorm'
 import { paginate, Pagination } from 'nestjs-typeorm-paginate'
 import { PageRequest } from '../common/dto/page-request'
 import { Comment } from './entities/comment.entity'
+import { number } from 'yup'
+import { CommentDto } from './dto/comment.dto'
+import { el, ro } from 'date-fns/locale'
 
 @Injectable()
 export class CommentService {
@@ -16,17 +19,65 @@ export class CommentService {
     ) { }
 
 
-    async findAll(req: PageRequest<Comment>, momentsId: number): Promise<Pagination<Comment>> {
-        const query = await this.commentRepository.createQueryBuilder('comment')
-            .where('(comment.momentsId = :momentsId and comment.rootCommentId is null) or (comment.rootCommentId in (select id from Comment where momentsId = :momentsId and rootCommentId is null))', { momentsId })
+    async findAll(req: PageRequest<Comment>): Promise<Pagination<Comment>> {
+
+        const query = await this.commentRepository.createQueryBuilder()
+            .where('comment.isDel = false')
+
+        if (req.search) {
+            const searchOptions = req.search as FindManyOptions<Comment>;
+            if ('where' in req.search) {
+                if ('where' in searchOptions && searchOptions.where) {
+                    for (const [key, value] of Object.entries(searchOptions.where)) {
+                        query.andWhere(`comment.${key} = ${value}`);
+                    }
+                }
+            }
+
+            query.orderBy('comment.isTop', 'DESC')
+
+            if ('order' in req.search) {
+                if ('order' in searchOptions && searchOptions.order) {
+                    for (const [key, value] of Object.entries(searchOptions.order)) {
+                        query.addOrderBy(`comment.${key}`, value as 'ASC' | 'DESC');
+                    }
+                }
+            }
+        }
 
-        // if (req.search) {
-        //     query.where(req.search)
-        // }
-    
         const options = { ...req.page }
+        const page = await paginate<Comment>(query, options)
+
+        const { items, meta } = page;
+        const rootMap = new Map<number, Comment>()
+
+        items.forEach(comment => {
+            if (!Boolean(comment.rootCommentId)) {
+                rootMap.set(comment.id, comment)
+            } else {
+                const root = rootMap.get(comment.rootCommentId)
+                if (Boolean(root.childList)) {
+                    root.childList.push(comment)
+                } else {
+                    const childList: Comment[] = []
+                    childList.push(comment)
+                    root.childList = childList
+                }
+                rootMap.set(root.id, root)
+            }
+        })
+        const newItems = Array.from(rootMap.values());
+
+        const result: Pagination<Comment> = {
+            items: newItems,
+            meta
+        };
+
+        return result
+    }
 
-        return await paginate<Comment>(query, options)
+    public async create(commentDto: CommentDto) {
+        return await this.commentRepository.save(commentDto);
     }
 
 }

+ 23 - 0
src/comment/dto/comment.dto.ts

@@ -0,0 +1,23 @@
+import { IsNumber, IsString } from "class-validator";
+
+export class CommentDto {
+
+    @IsString()
+    content: string
+
+    @IsNumber()
+    userId: number
+
+    @IsString()
+    userName: string
+
+    @IsNumber()
+    momentsId: number
+
+
+    rootCommentId?: number
+
+
+    toCommentId?: number
+
+}

+ 7 - 0
src/comment/entities/comment.entity.ts

@@ -1,3 +1,4 @@
+import { Exclude } from "class-transformer";
 import { Column, CreateDateColumn, Entity, ManyToOne, OneToMany, PrimaryGeneratedColumn } from "typeorm";
 
 @Entity()
@@ -12,6 +13,9 @@ export class Comment {
     @Column()
     userId: number
 
+    @Column()
+    userName: string
+
     @Column()
     momentsId: number
 
@@ -33,4 +37,7 @@ export class Comment {
     @CreateDateColumn()
     createdAt: Date
 
+    @Exclude()
+    childList: Comment[]
+
 }