Bläddra i källkod

完善分页
新增查询别人信息接口(未完善

枫叶秋林 2 år sedan
förälder
incheckning
65f0a98948

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

@@ -7,8 +7,11 @@ import { Request } from 'express'
 export class PostController {
   constructor(private readonly postService: PostService) {}
   @Get('platelist')
-  async getpostlist(@Query('plateid') plateid: number) {
-    return await this.postService.getpostlist(+plateid)
+  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)
   }
   @Get()
   async getpost(@Query('id') id: number) {

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

@@ -4,7 +4,7 @@ import { Injectable } from '@nestjs/common'
 @Injectable()
 export class PostService {
   constructor(private readonly prisma: PrismaService) {}
-  async post(authId: number, plateid: number, { title, content }) {
+  async post(authId: number, plateid: number = 0, { title, content }) {
     return await this.prisma.post.create({
       data: {
         title,
@@ -66,18 +66,31 @@ export class PostService {
     return data
   }
 
-  async getpostlist(plateid: number) {
+  async getpostlist(plateid: number, page: number = 1, limit: number = 10) {
     const data = await this.prisma.post.findMany({
+      skip: (page - 1) * limit,
+      take: limit,
       where: {
         plateId: plateid,
       },
       select: {
         id: true,
         title: true,
+        content: true,
         authorId: true,
+        updatedAt: true,
       },
     })
+    // 分页结果
+    const total = await this.prisma.post.count({
+      where: {
+        plateId: plateid,
+      },
+    })
+    const totalPage = Math.ceil(total / limit)
+
     data.map(async (item) => {
+      item.content = item.content.slice(0, 20).concat('...')
       const user = await this.prisma.auth.findUnique({
         where: {
           auth_id: item.authorId,
@@ -86,13 +99,22 @@ export class PostService {
           user: true,
         },
       })
+      let comments = await this.prisma.comment.count({
+        where: {
+          postId: item.id,
+        },
+      })
+      if (comments) {
+        comments = 0
+      }
       delete user.password
       delete user.user.authId
       return {
+        comments,
         ...item,
         ...user,
       }
     })
-    return data
+    return { totalPage, data }
   }
 }

+ 11 - 3
src/userinfo/userinfo.controller.ts

@@ -1,4 +1,4 @@
-import { Body, Controller, Get, Post, Req, UseGuards } from '@nestjs/common'
+import { Body, Controller, Get, Post, Put, Query, Req, UseGuards } from '@nestjs/common'
 import { AuthGuard } from '@nestjs/passport'
 import { UserinfoService } from './userinfo.service'
 import { Request } from 'express'
@@ -6,12 +6,20 @@ import userDateDto from './dto/userinfo.dto'
 @Controller('userinfo')
 export class UserinfoController {
   constructor(private readonly userinfoService: UserinfoService) {}
-  @Get('getinfo')
+  @Get()
   @UseGuards(AuthGuard('jwt'))
   async getinfo(@Req() req: Request) {
     return await this.userinfoService.getinfo(req.user as number)
   }
-  @Post('updateinfo')
+  @Get('getuser')
+  async getuser(@Query('id') id: number) {
+    if (id) {
+      return await this.userinfoService.getinfo(+id)
+    } else {
+      return { cod: 400, message: '用户不存在' }
+    }
+  }
+  @Put()
   @UseGuards(AuthGuard('jwt'))
   updateinfo(@Req() req: Request, @Body() data: userDateDto) {
     return this.userinfoService.updateinfo(req.user as number, data)

+ 3 - 0
src/userinfo/userinfo.service.ts

@@ -15,6 +15,9 @@ export class UserinfoService {
         user: true,
       },
     })
+    if (userinfo === null) {
+      return { cod: 400, msg: '用户不存在' }
+    }
     delete userinfo.auth_id
     delete userinfo.user.authId
     delete userinfo.password