Pārlūkot izejas kodu

完善板块接口
完善发帖接口

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

+ 2 - 2
src/plate/plate.controller.ts

@@ -13,8 +13,8 @@ export class PlateController {
     return await this.postService.getplate()
   }
   @Delete('deleteplate')
-  async deleteplate(@Body() plateid: number) {
-    return await this.postService.deleteplate(plateid)
+  async deleteplate(@Body() body) {
+    return await this.postService.deleteplate(body.plateid)
   }
   @Put('updateplate')
   async updateplate(@Body() { plateid, plate }) {

+ 4 - 4
src/plate/plate.service.ts

@@ -19,16 +19,16 @@ export class PlateService {
       },
     })
   }
-  async deleteplate(plateid: number) {
-    return await this.prisma.plate.deleteMany({
+  async deleteplate(id: number) {
+    return await this.prisma.plate.delete({
       where: {
-        id: plateid,
+        id,
       },
     })
   }
 
   async updateplate(plateid: number, name: string) {
-    return await this.prisma.plate.updateMany({
+    return await this.prisma.plate.update({
       where: {
         id: plateid,
       },

+ 14 - 14
src/post/post.controller.ts

@@ -6,25 +6,25 @@ import { Request } from 'express'
 @Controller('post')
 export class PostController {
   constructor(private readonly postService: PostService) {}
-  @Get('post')
-  async getpost(@Query('id') id: number) {
-    return await this.postService.getpost(id)
-  }
-  @Get('delete')
-  async delete(@Query('postid') postid: number) {
-    return await this.postService.delete(postid)
+  @Get('platelist')
+  async getpostlist(@Query('plateid') plateid: number) {
+    return await this.postService.getpostlist(+plateid)
   }
-  @Get('updated')
-  async updated(@Body() { postid, title, content, plateId }) {
-    return await this.postService.updated(postid, { title, content, plateId })
+  @Get()
+  async getpost(@Query('id') id: number) {
+    return await this.postService.getpost(+id)
   }
-  @Post('post')
+  @Post()
   @UseGuards(AuthGuard('jwt'))
   async post(@Req() req: Request, @Body() { title, content, plateid }) {
     return await this.postService.post(req.user as number, plateid, { title, content })
   }
-  @Get('platelist')
-  async getpostlist(@Query('plateid') plateid: number) {
-    return await this.postService.getpostlist(plateid)
+  @Delete()
+  async delete(@Query('postid') postid: number) {
+    return await this.postService.delete(+postid)
+  }
+  @Put()
+  async updated(@Body() { postid, title, content, plateId }) {
+    return await this.postService.updated(+postid, { title, content, plateId })
   }
 }

+ 34 - 16
src/post/post.service.ts

@@ -15,28 +15,34 @@ export class PostService {
     })
   }
   async updated(postid: number, { title, content, plateId }) {
-    return await this.prisma.post.updateMany({
-      where: {
-        id: postid,
-      },
-      data: {
-        title,
-        content,
-        plateId,
-      },
-    })
+    if (((await this.getpost(postid)) as any).cod !== 400) {
+      return await this.prisma.post.update({
+        where: {
+          id: postid,
+        },
+        data: {
+          title,
+          content,
+          plateId,
+        },
+      })
+    }
+    return { cod: 400, message: '帖子不存在' }
   }
 
   async delete(postid: number) {
-    return await this.prisma.post.deleteMany({
-      where: {
-        id: postid,
-      },
-    })
+    if (((await this.getpost(postid)) as any).cod !== 400) {
+      return await this.prisma.post.delete({
+        where: {
+          id: postid,
+        },
+      })
+    }
+    return { cod: 400, message: '帖子不存在' }
   }
 
   async getpost(postid: number) {
-    return await this.prisma.post.findUnique({
+    const data = await this.prisma.post.findUnique({
       where: {
         id: postid,
       },
@@ -46,6 +52,18 @@ export class PostService {
         comment: true,
       },
     })
+    if (!data) {
+      return { cod: 400, message: '帖子不存在' }
+    }
+    delete data.author.password
+    delete data.author.username
+    delete data.author.email
+    delete data.plate.id
+    delete data.id
+    delete data.authorId
+    delete data.plateId
+
+    return data
   }
 
   async getpostlist(plateid: number) {