Pārlūkot izejas kodu

置顶帖子后端

枫叶秋林 2 gadi atpakaļ
vecāks
revīzija
97b327b546

+ 2 - 2
package.json

@@ -27,7 +27,7 @@
     "@nestjs/jwt": "^9.0.0",
     "@nestjs/passport": "^9.0.0",
     "@nestjs/platform-express": "^9.0.0",
-    "@prisma/client": "4.7.0",
+    "@prisma/client": "latest",
     "argon2": "^0.30.2",
     "class-transformer": "^0.5.1",
     "class-validator": "^0.13.2",
@@ -61,7 +61,7 @@
     "eslint-plugin-prettier": "^4.0.0",
     "jest": "28.1.2",
     "prettier": "^2.3.2",
-    "prisma": "^4.7.0",
+    "prisma": "latest",
     "source-map-support": "^0.5.20",
     "supertest": "^6.1.3",
     "ts-jest": "28.0.5",

+ 2 - 0
prisma/migrations/20230109133424_upset_post_add_is_top/migration.sql

@@ -0,0 +1,2 @@
+-- AlterTable
+ALTER TABLE `post` ADD COLUMN `isTop` BOOLEAN NOT NULL DEFAULT false;

+ 8 - 0
prisma/migrations/20230110024530_/migration.sql

@@ -0,0 +1,8 @@
+/*
+  Warnings:
+
+  - You are about to alter the column `isTop` on the `post` table. The data in that column could be lost. The data in that column will be cast from `TinyInt` to `UnsignedInt`.
+
+*/
+-- AlterTable
+ALTER TABLE `post` MODIFY `isTop` INTEGER UNSIGNED NOT NULL DEFAULT 0;

+ 1 - 0
prisma/schema.prisma

@@ -28,6 +28,7 @@ model post{
   comment   comment[]
   plateId   Int       @db.UnsignedInt @default(0)
   plate     plate     @relation(fields: [plateId], references: [id])
+  isTop     Int       @default(0) @db.UnsignedInt
 }
 model comment{
   id        Int       @id @default(autoincrement()) @db.UnsignedInt

+ 3 - 2
src/auth/auth.service.ts

@@ -88,7 +88,7 @@ export class AuthService {
         jurisdiction: true,
       },
     })
-    switch (data.jurisdiction) {
+    switch (data?.jurisdiction) {
       case 0:
         return { cod: 200, msg: '普通用户' }
       case 1:
@@ -97,7 +97,8 @@ export class AuthService {
         return { cod: 200, msg: '超级管理员' }
       case 3:
         return { cod: 200, msg: '封禁' }
+      default:
+        return { cod: 200, msg: '普通用户' }
     }
-    return data
   }
 }

+ 1 - 2
src/cod/cod.service.ts

@@ -37,14 +37,13 @@ export class CodService {
     }
     const cods1: code1[] = JSON.parse(cod1)
     let success = 0
-    console.log(cods, cods1)
+
     for (let index = 0; index < cods.length; index++) {
       const element = cods[index]
       if (Math.abs(cods1[index]?.x - element.x) < 30 && Math.abs(cods1[index]?.y - element.y) < 30) {
         success++
       }
     }
-    console.log(success)
 
     return success === 4
   }

+ 9 - 6
src/post/post.controller.ts

@@ -10,11 +10,8 @@ import { CodService } from '@/cod/cod.service'
 export class PostController {
   constructor(private readonly postService: PostService, private readonly codService: CodService) {}
   @Get('platelist')
-  async getpostlist(@Query() { plateid, page, limit }) {
-    if (page === undefined || limit === undefined) {
-      return await this.postService.getpostlist(+plateid)
-    }
-    return await this.postService.getpostlist(+plateid, +page, +limit)
+  async getpostlist(@Query() { plateid, page = 1, limit = 10, isTop = false }) {
+    return await this.postService.getpostlist(+plateid, +page, +limit, Boolean(isTop))
   }
   @Get()
   async getpost(@Query('id') id: number) {
@@ -52,7 +49,7 @@ export class PostController {
   //根据用户id获取帖子
   @Get('getpostbyuserid')
   @UseGuards(AuthGuard('jwt'))
-  async getpostbyuserid(@Req() req: Request, @Query() { link, page }) {
+  async getpostbyuserid(@Req() req: Request, @Query() { link = 10, page = 1 }) {
     return await this.postService.getPostByUserId(req.user as number, page, link)
   }
   // 增加浏览量
@@ -60,4 +57,10 @@ export class PostController {
   async addview(@Req() req, @Query('id') id: number) {
     return await this.postService.addView(req.ip, +id)
   }
+  // 置顶帖子
+  @Put('top')
+  @UseGuards(AuthGuard('jwt'))
+  async top(@Req() req, @Query('id') id: number) {
+    return await this.postService.setTop(+id, req.user)
+  }
 }

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

@@ -1,7 +1,7 @@
 import { AuthService } from '@/auth/auth.service'
 import { PrismaService } from '@/prisma/prisma.service'
 import { RedisService } from '@/redis/redis.service'
-import { Injectable } from '@nestjs/common'
+import { BadGatewayException, BadRequestException, HttpException, Injectable } from '@nestjs/common'
 
 @Injectable()
 export class PostService {
@@ -88,11 +88,14 @@ export class PostService {
     return { comment, ...data }
   }
 
-  async getpostlist(plateid: number, page: number = 1, limit: number = 10) {
+  async getpostlist(plateid: number, page: number = 1, limit: number = 10, isTop = false) {
     if (plateid === 0) {
       const data = await this.prisma.post.findMany({
         skip: (page - 1) * limit,
         take: limit,
+        where: {
+          isTop: Number(isTop),
+        },
         select: {
           id: true,
           title: true,
@@ -110,6 +113,7 @@ export class PostService {
         take: limit,
         where: {
           plateId: plateid,
+          isTop: Number(isTop),
         },
         select: {
           id: true,
@@ -130,7 +134,7 @@ export class PostService {
   private async res(total: number, data: any, limit: number = 10) {
     const totalPage = Math.ceil(total / limit)
     data?.map(async (item) => {
-      item.content = item.content.slice(0, 20).concat('...')
+      item.content = item.content.slice(0, 40).concat('...')
       const user = await this.prisma.auth.findUnique({
         where: {
           auth_id: item.authorId,
@@ -158,7 +162,6 @@ export class PostService {
 
     return { totalPage, data }
   }
-
   async getNewPost(num = 5) {
     const data = await this.prisma.post.findMany({
       take: num,
@@ -254,4 +257,53 @@ export class PostService {
     })
     return { cod: 200, message: '增加浏览量成功', data: data.views + 1 }
   }
+  // 置顶帖子
+  async setTop(postid: number, userid: number) {
+    const data = await this.prisma.post.findUnique({
+      where: {
+        id: postid,
+      },
+      select: {
+        isTop: true,
+      },
+    })
+    if (!data) {
+      throw new HttpException('帖子不存在', 404)
+    }
+
+    const admin = await this.auth.checkPermissions(userid)
+    if (admin.msg === '管理员' || '超级管理员') {
+      if (data.isTop) {
+        const res = await this.prisma.post.update({
+          where: {
+            id: postid,
+          },
+          data: {
+            isTop: 0,
+          },
+          select: {
+            isTop: true,
+          },
+        })
+
+        return { cod: 200, message: '取消置顶成功', data: Boolean(res.isTop) }
+      } else {
+        const res = await this.prisma.post.update({
+          where: {
+            id: postid,
+          },
+          data: {
+            isTop: 1,
+          },
+          select: {
+            isTop: true,
+          },
+        })
+
+        return { cod: 200, message: '置顶成功', data: Boolean(res.isTop) }
+      }
+    } else {
+      throw new HttpException('权限不足', 403)
+    }
+  }
 }

+ 5 - 1
src/prisma/prisma.service.ts

@@ -2,4 +2,8 @@ import { Injectable } from '@nestjs/common'
 import { PrismaClient } from '@prisma/client'
 // import { PrismaClient } from '@prisma/client';
 @Injectable()
-export class PrismaService extends PrismaClient {}
+export class PrismaService extends PrismaClient {
+  constructor() {
+    super({})
+  }
+}