Browse Source

评论修改

wuyi 2 years ago
parent
commit
fb972be88a

+ 5 - 0
src/comment/comment.controller.ts

@@ -27,6 +27,11 @@ export class CommentController {
         return await this.commentService.findAll(page)
     }
 
+    @Post('/childComments')
+    public async childList(@Body() page: PageRequest<Comment>) {
+        return await this.commentService.findChildList(page)
+    }
+
     @Put()
     public async create(@Body() commentDto: CommentDto) {
         return await this.commentService.create(commentDto)

+ 47 - 2
src/comment/comment.service.ts

@@ -8,7 +8,6 @@ import { PageRequest } from '../common/dto/page-request'
 import { Comment } from './entities/comment.entity'
 import { CommentDto } from './dto/comment.dto'
 import { LikesService } from 'src/likes/likes.service'
-import { string } from 'yup'
 
 @Injectable()
 export class CommentService {
@@ -61,7 +60,14 @@ export class CommentService {
         const childList = await this.commentRepository.createQueryBuilder()
             .where({
                 rootCommentId: In(ids),
-            }).getMany();
+            })
+            .andWhere(`(
+                SELECT COUNT(*)
+                FROM comment AS c2
+                WHERE c2.rootCommentId = comment.rootCommentId AND c2.createdAt <= comment.createdAt
+            ) <= 2`)
+            .orderBy("comment.rootCommentId, comment.createdAt", "ASC")
+            .getMany();
 
         childList.forEach(comment => {
             if (Boolean(comment.rootCommentId)) {
@@ -87,9 +93,48 @@ export class CommentService {
     }
 
     public async create(commentDto: CommentDto) {
+        if (commentDto.rootCommentId) {
+            const list: CommentDto[] = []
+            const root = await this.commentRepository.findOneBy({ id: commentDto.toCommentId })
+            commentDto.replyUserName = root.userName
+            root.replyNum += 1
+            list.push(root)
+            if (commentDto.rootCommentId != commentDto.toCommentId) {
+                const root = await this.commentRepository.findOneBy({ id: commentDto.rootCommentId })
+                root.replyNum += 1
+                list.push(root)
+            }
+            await this.commentRepository.save(list)
+        }
         return await this.commentRepository.save(commentDto);
     }
 
+    public async findChildList(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}`);
+                    }
+                }
+            }
+
+            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');
+                    }
+                }
+            }
+        }
+
+        return await paginate<Comment>(query, { ...req.page })
+    }
+
     public async delete(id: number): Promise<void> {
         const comment = await this.commentRepository.findOneBy({ id: +id })
         if (comment) {

+ 1 - 1
src/comment/dto/comment.dto.ts

@@ -14,10 +14,10 @@ export class CommentDto {
     @IsNumber()
     momentsId: number
 
+    replyUserName?: string
 
     rootCommentId?: number
 
-
     toCommentId?: number
 
 }

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

@@ -28,6 +28,12 @@ export class Comment {
     @Column({ default: 0 })
     commentLikeCount: number
 
+    @Column()
+    replyUserName: string
+
+    @Column({ default: 0 })
+    replyNum: number
+
     @Column({ default: false })
     isTop: boolean