Browse Source

后端积分增加

枫叶秋林 2 years ago
parent
commit
973014830d

+ 4 - 0
src/auth/auth.controller.ts

@@ -43,4 +43,8 @@ export class AuthController {
   async Permissions(@Req() req: Request) {
     return await this.auto.checkPermissions(req.user as number)
   }
+  @Get('concat')
+  async concat(@Req() req: Request) {
+    return await this.auto.count()
+  }
 }

+ 24 - 0
src/auth/auth.service.ts

@@ -101,4 +101,28 @@ export class AuthService {
         return { cod: 200, msg: '普通用户' }
     }
   }
+  //统计用户数
+  async count() {
+    //最新用户
+    const newuser = await this.prisma.userinfo.findFirst({
+      orderBy: {
+        createdAt: 'desc',
+      },
+      select: {
+        user: {
+          select: {
+            username: true,
+          },
+        },
+      },
+    })
+    return {
+      cod: 200,
+      msg: '查询成功',
+      data: {
+        newuser: newuser.user.username,
+        count: await this.prisma.auth.count(),
+      },
+    }
+  }
 }

+ 2 - 1
src/comment/comment.module.ts

@@ -1,10 +1,11 @@
 import { Module } from '@nestjs/common'
 import { CommentService } from './comment.service'
 import { CommentController } from './comment.controller'
+import { UserinfoService } from '@/userinfo/userinfo.service'
 
 @Module({
   imports: [],
   controllers: [CommentController],
-  providers: [CommentService],
+  providers: [CommentService, UserinfoService],
 })
 export class CommentModule {}

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

@@ -1,11 +1,16 @@
 import { AuthService } from '@/auth/auth.service'
 import { PrismaService } from '@/prisma/prisma.service'
+import { UserinfoService } from '@/userinfo/userinfo.service'
 import { Injectable } from '@nestjs/common'
 import e from 'express'
 
 @Injectable()
 export class CommentService {
-  constructor(private readonly prisma: PrismaService, private readonly auth: AuthService) {}
+  constructor(
+    private readonly prisma: PrismaService,
+    private readonly auth: AuthService,
+    private readonly userinfo: UserinfoService,
+  ) {}
   // 发表评论
   async createComment(content: string, articleId: number, userId: number) {
     const data = await this.prisma.comment.create({
@@ -15,6 +20,11 @@ export class CommentService {
         postId: articleId,
       },
     })
+    this.userinfo.updateinfo(userId, {
+      exp: 1,
+      mapleCoin: 1,
+      level: 0,
+    })
     return { cod: 200, msg: '发表评论成功', data }
   }
   // 删除评论

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

@@ -49,8 +49,12 @@ export class PostController {
   //根据用户id获取帖子
   @Get('getpostbyuserid')
   @UseGuards(AuthGuard('jwt'))
-  async getpostbyuserid(@Req() req: Request, @Query() { link = 10, page = 1 }) {
-    return await this.postService.getPostByUserId(req.user as number, page, link)
+  async getpostbyuserid(@Req() req: Request, @Query() { userid = -1, link = 10, page = 1 }) {
+    if (userid == -1) {
+      return await this.postService.getPostByUserId(req.user as number, page, link)
+    } else {
+      return await this.postService.getPostByUserId(+userid, page, link)
+    }
   }
   // 增加浏览量
   @Get('addview')

+ 6 - 5
src/post/post.module.ts

@@ -1,9 +1,10 @@
-import { Module } from '@nestjs/common';
-import { PostService } from './post.service';
-import { PostController } from './post.controller';
+import { Module } from '@nestjs/common'
+import { PostService } from './post.service'
+import { PostController } from './post.controller'
+import { UserinfoService } from '@/userinfo/userinfo.service'
 
 @Module({
-  providers: [PostService],
-  controllers: [PostController]
+  providers: [PostService, UserinfoService],
+  controllers: [PostController],
 })
 export class PostModule {}

+ 8 - 3
src/post/post.service.ts

@@ -1,6 +1,7 @@
 import { AuthService } from '@/auth/auth.service'
 import { PrismaService } from '@/prisma/prisma.service'
 import { RedisService } from '@/redis/redis.service'
+import { UserinfoService } from '@/userinfo/userinfo.service'
 import { BadGatewayException, BadRequestException, HttpException, Injectable } from '@nestjs/common'
 
 @Injectable()
@@ -9,6 +10,7 @@ export class PostService {
     private readonly prisma: PrismaService,
     private readonly redis: RedisService,
     private readonly auth: AuthService,
+    private readonly userinfo: UserinfoService,
   ) {}
   async post(authId: number, plateid: number = 0, { title, content }) {
     const res = await this.prisma.post.create({
@@ -20,6 +22,11 @@ export class PostService {
       },
       select: { id: true },
     })
+    this.userinfo.updateinfo(authId, {
+      exp: 1,
+      mapleCoin: 1,
+      level: 0,
+    })
     return { code: 200, message: '发帖成功', data: res }
   }
   async updated(userId: number, postid: number, { title, content, plateId }) {
@@ -93,15 +100,13 @@ export class PostService {
       const data = await this.prisma.post.findMany({
         skip: (page - 1) * limit,
         take: limit,
-        where: {
-          isTop: Number(isTop),
-        },
         select: {
           id: true,
           title: true,
           content: true,
           authorId: true,
           updatedAt: true,
+          isTop: true,
           views: true,
         },
       })

+ 3 - 3
src/userinfo/dto/userinfo.dto.ts

@@ -1,5 +1,5 @@
 export default class userDateDto {
-  avatar: string
-  exp: number
-  level: number
+  mapleCoin: number = 0
+  exp: number = 0
+  level: number = 0
 }

+ 5 - 4
src/userinfo/userinfo.module.ts

@@ -1,9 +1,10 @@
-import { Module } from '@nestjs/common';
-import { UserinfoService } from './userinfo.service';
-import { UserinfoController } from './userinfo.controller';
+import { Module } from '@nestjs/common'
+import { UserinfoService } from './userinfo.service'
+import { UserinfoController } from './userinfo.controller'
 
 @Module({
   providers: [UserinfoService],
-  controllers: [UserinfoController]
+  controllers: [UserinfoController],
+  exports: [UserinfoService],
 })
 export class UserinfoModule {}

+ 19 - 11
src/userinfo/userinfo.service.ts

@@ -32,17 +32,25 @@ export class UserinfoService {
         user: true,
       },
     })
-    const user = await this.prisma.userinfo.update({
-      where: {
-        authId: userinfo.user.authId,
-      },
-      data: {
-        avatar: data.avatar,
-        exp: data.exp,
-        level: data.level,
-      },
-    })
-    return { cod: 200, msg: '修改成功', data: user }
+    if (userinfo) {
+      const user = await this.prisma.userinfo.findFirst({
+        where: {
+          authId: userinfo.user.authId,
+        },
+      })
+      if (user) {
+        return await this.prisma.userinfo.update({
+          where: {
+            authId: userinfo.user.authId,
+          },
+          data: {
+            mapleCoin: user.mapleCoin++,
+            exp: user.exp++,
+            level: user.level++,
+          },
+        })
+      }
+    }
   }
   async updateuserinfo(id: number, dto: userInfoDto) {
     const data = await this.prisma.userinfo.update({