Эх сурвалжийг харах

feat(post): 添加按分类和标签获取文章列表的功能

- 在PostController中添加getCates方法获取所有分类
- 添加getPostsOfCate方法获取指定分类下的文章列表
- 添加getPostsOfTag方法获取指定标签关联的文章列表
- 移除TagController中不再使用的getTagById方法
Sakulin 2 сар өмнө
parent
commit
6866379f53

+ 90 - 0
src/post/post.controller.ts

@@ -207,4 +207,94 @@ export class PostController {
         msg: e.message,
       }));
   }
+
+  @Get('cates')
+  async getCates() {
+    const prisma = this.datasource.getPrisma();
+    return await prisma.post
+      .findMany({
+        select: {
+          cate: true,
+        },
+        distinct: ['cate'],
+      })
+      .then((e) => ({
+        code: 200,
+        data: e.map((e) => e.cate),
+      }));
+  }
+
+  @Get('ofCate/:cate')
+  async getPostsOfCate(@Param('cate') cate: string) {
+    const prisma = this.datasource.getPrisma();
+    return {
+      code: 200,
+      data: await prisma.post.findMany({
+        where: { cate },
+        orderBy: {
+          createdAt: 'desc',
+        },
+        select: {
+          id: true,
+          title: true,
+          createdAt: true,
+          updatedAt: true,
+          cate: true,
+          descirption: true,
+          tags: {
+            select: {
+              name: true,
+              id: true,
+              color: true,
+            },
+          },
+        },
+      }),
+    };
+  }
+
+  @Get('ofTag/:tag')
+  async getPostsOfTag(@Param('tag') tag: string) {
+    const prisma = this.datasource.getPrisma();
+    return {
+      code: 200,
+      data: {
+        tag: await prisma.tag.findUnique({
+          where: { name: tag },
+          select: {
+            name: true,
+            id: true,
+            color: true,
+          },
+        }),
+        posts: await prisma.post.findMany({
+          where: {
+            tags: {
+              some: {
+                name: tag,
+              },
+            },
+          },
+          orderBy: {
+            createdAt: 'desc',
+          },
+          select: {
+            id: true,
+            title: true,
+            createdAt: true,
+            updatedAt: true,
+            cate: true,
+            descirption: true,
+            tags: {
+              select: {
+                name: true,
+                id: true,
+                color: true,
+              },
+            },
+          },
+        }),
+      },
+    };
+  }
 }

+ 1 - 33
src/tag/tag.controller.ts

@@ -1,4 +1,4 @@
-import { Controller, Get, Param, Headers } from '@nestjs/common';
+import { Controller, Get, Headers } from '@nestjs/common';
 import { AuthService } from 'src/auth/auth.service';
 import { DatasourceService } from 'src/datasource/datasource.service';
 
@@ -24,38 +24,6 @@ export class TagController {
     };
   }
 
-  @Get('/get/:id')
-  async getTagById(@Param('id') id: string) {
-    const prisma = this.datasource.getPrisma();
-    const target = await prisma.tag.findUnique({
-      where: { id: parseInt(id) },
-      select: {
-        id: true,
-        name: true,
-        color: true,
-        posts: {
-          select: {
-            id: true,
-            title: true,
-            createdAt: true,
-            updatedAt: true,
-          },
-        },
-      },
-    });
-    if (!target) {
-      return {
-        code: 404,
-        msg: 'Tag not found',
-      };
-    } else {
-      return {
-        code: 200,
-        data: target,
-      };
-    }
-  }
-
   @Get('/clear')
   async clearEmptyTags(@Headers('Authorization') token: string) {
     if (!this.authService.checkBearerToken(token)) {