Parcourir la source

完成评论后台

枫叶秋林 il y a 2 ans
Parent
commit
b64f4c22c3

+ 2 - 0
prisma/migrations/20230108005603_usset_post_content_varchar_1000/migration.sql

@@ -0,0 +1,2 @@
+-- AlterTable
+ALTER TABLE `post` MODIFY `content` VARCHAR(10000) NOT NULL;

+ 1 - 2
prisma/schema.prisma

@@ -18,10 +18,9 @@ model auth{
 model post{
   id        Int       @id @default(autoincrement()) @db.UnsignedInt
   title     String
-  content   String
+  content   String    @db.VarChar(10000)
   createdAt DateTime  @default(now())
   updatedAt DateTime  @updatedAt
-  // 浏览量
   views     Int       @default(0) @db.UnsignedInt
   authorId  Int       @db.UnsignedInt
   author    auth      @relation(fields: [authorId], references: [auth_id])

+ 2 - 0
src/app.module.ts

@@ -8,6 +8,7 @@ import { UserinfoModule } from './userinfo/userinfo.module';
 import { PostModule } from './post/post.module';
 import { PlateModule } from './plate/plate.module';
 import { UploadModule } from './upload/upload.module';
+import { CommentModule } from './comment/comment.module';
 @Module({
   imports: [
     AuthModule,
@@ -21,6 +22,7 @@ import { UploadModule } from './upload/upload.module';
     PostModule,
     PlateModule,
     UploadModule,
+    CommentModule,
   ],
   controllers: [],
   providers: [],

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

@@ -0,0 +1,49 @@
+import { Controller, Get, Post, Body, Param, Delete, UseGuards, Req, Query, Put, Headers } from '@nestjs/common'
+import { CommentService } from './comment.service'
+import { AuthGuard } from '@nestjs/passport'
+import { CodService } from '@/cod/cod.service'
+
+@Controller('comment')
+export class CommentController {
+  constructor(private readonly commentService: CommentService, private readonly codService: CodService) {}
+
+  @Get()
+  async getComment(@Query('postId') postId: number, @Query('page') page: number, @Query('limit') limit: number) {
+    if (page && limit && postId) {
+      return this.commentService.getComment(+postId, +page, +limit)
+    }
+    return { status: 400, message: '参数错误' }
+  }
+  @Post()
+  @UseGuards(AuthGuard('jwt'))
+  async createComment(@Req() req, @Body() body: any, @Headers('cod') cod: any) {
+    const rescod = await this.codService.verifyCod(req.ip, cod)
+    if (!rescod.status) {
+      return rescod
+    }
+    const { content, postid } = body
+    if (!content || !postid) {
+      return { status: 400, message: '参数错误' }
+    }
+    return this.commentService.createComment(content, +postid, req.user as number)
+  }
+
+  @Delete()
+  @UseGuards(AuthGuard('jwt'))
+  async deleteComment(@Req() req, @Body() body: any) {
+    const { Commentid } = body
+    if (!Commentid) {
+      return { status: 400, message: '参数错误' }
+    }
+    return this.commentService.deleteComment(req.user as number, +Commentid)
+  }
+  @Put()
+  @UseGuards(AuthGuard('jwt'))
+  async updateComment(@Req() req, @Body() body: any) {
+    const { Commentid, content } = body
+    if (!Commentid || !content) {
+      return { status: 400, message: '参数错误' }
+    }
+    return this.commentService.updateComment(req.user as number, +Commentid, content)
+  }
+}

+ 9 - 0
src/comment/comment.module.ts

@@ -0,0 +1,9 @@
+import { Module } from '@nestjs/common';
+import { CommentService } from './comment.service';
+import { CommentController } from './comment.controller';
+
+@Module({
+  controllers: [CommentController],
+  providers: [CommentService]
+})
+export class CommentModule {}

+ 88 - 0
src/comment/comment.service.ts

@@ -0,0 +1,88 @@
+import { PrismaService } from '@/prisma/prisma.service'
+import { Injectable } from '@nestjs/common'
+
+@Injectable()
+export class CommentService {
+  constructor(private readonly prisma: PrismaService) {}
+  // 发表评论
+  async createComment(content: string, articleId: number, userId: number) {
+    const data = await this.prisma.comment.create({
+      data: {
+        authorId: userId,
+        content: content,
+        postId: articleId,
+      },
+    })
+    return { cod: 200, msg: '发表评论成功', data }
+  }
+  // 删除评论
+  async deleteComment(userId: number, Commentid: number) {
+    const data = await this.prisma.comment.findFirst({
+      where: {
+        id: Commentid,
+      },
+    })
+    if (!data) {
+      return { cod: 400, msg: '评论不存在' }
+    }
+    if (data.authorId !== userId) {
+      return { cod: 400, msg: '无权删除评论' }
+    }
+    await this.prisma.comment.delete({
+      where: {
+        id: Commentid,
+      },
+    })
+    return { cod: 200, msg: '删除评论成功', data }
+  }
+  async getComment(postId: number, page: number = 1, limit: number = 10) {
+    const data = await this.prisma.comment.findMany({
+      skip: (page - 1) * limit,
+      take: limit,
+      // 时间最新排序
+      orderBy: {
+        createdAt: 'desc',
+      },
+      where: {
+        postId: postId,
+      },
+    })
+    data.map((item) => {
+      delete item.postId
+      delete item.createdAt
+      return item
+    })
+    //总页数
+    const total = await this.prisma.comment.count({
+      where: {
+        postId: postId,
+      },
+    })
+    //总条数
+    const totalPage = Math.ceil(total / limit)
+    return { cod: 200, msg: '获取评论成功', data, total, totalPage }
+  }
+  //修改评论
+  async updateComment(userId: number, Commentid: number, content: string) {
+    const data = await this.prisma.comment.findFirst({
+      where: {
+        id: Commentid,
+      },
+    })
+    if (!data) {
+      return { cod: 400, msg: '评论不存在' }
+    }
+    if (data.authorId !== userId) {
+      return { cod: 400, msg: '无权修改评论' }
+    }
+    await this.prisma.comment.update({
+      where: {
+        id: Commentid,
+      },
+      data: {
+        content: content,
+      },
+    })
+    return { cod: 200, msg: '修改评论成功', data }
+  }
+}

+ 6 - 4
src/post/post.service.ts

@@ -63,19 +63,21 @@ export class PostService {
       },
       include: {
         author: true,
-        comment: true,
       },
     })
     if (!data) {
       return { cod: 400, message: '帖子不存在' }
     }
-
+    const comment = await this.prisma.comment.count({
+      where: {
+        postId: postid,
+      },
+    })
     delete data.author.password
     delete data.author.username
     delete data.author.email
     delete data.authorId
-
-    return data
+    return { comment, ...data }
   }
 
   async getpostlist(plateid: number, page: number = 1, limit: number = 10) {