Przeglądaj źródła

Merge branch 'comment' of xiongzhu/chat-api into master

wuyi 2 lat temu
rodzic
commit
b5a3d6aac3
2 zmienionych plików z 26 dodań i 2 usunięć
  1. 18 1
      src/comment/comment.controller.ts
  2. 8 1
      src/comment/comment.service.ts

+ 18 - 1
src/comment/comment.controller.ts

@@ -2,7 +2,11 @@ import {
     Controller,
     Put,
     Body,
-    Post
+    Post,
+    Delete,
+    Param,
+    HttpStatus,
+    BadRequestException
 } from '@nestjs/common'
 import { ApiBearerAuth, ApiTags } from '@nestjs/swagger'
 import { CommentService } from './comment.service'
@@ -27,4 +31,17 @@ export class CommentController {
         return await this.commentService.create(commentDto)
     }
 
+    @Delete('/:id')
+    public async deleteComment(@Param('id') id: number) {
+        try {
+            await this.commentService.delete(id)
+            return {
+                message: 'Comment Deleted successfully!',
+                status: HttpStatus.OK
+            }
+        } catch (err) {
+            throw new BadRequestException(err, 'Error: Comment not deleted!')
+        }
+    }
+
 }

+ 8 - 1
src/comment/comment.service.ts

@@ -57,7 +57,6 @@ export class CommentService {
         })
 
         items.forEach(comment => {
-            console.log(rootMap)
             if (Boolean(comment.rootCommentId)) {
                 const root = rootMap.get(comment.rootCommentId)
                 if (root.hasOwnProperty('childList')) {
@@ -84,4 +83,12 @@ export class CommentService {
         return await this.commentRepository.save(commentDto);
     }
 
+    public async delete(id: number): Promise<void> {
+        const comment = await this.commentRepository.findOneBy({ id: +id })
+        if (comment) {
+            comment.isDel = true
+            await this.commentRepository.save(comment)
+        }
+    }
+
 }