瀏覽代碼

✨ feat(posts): add post list api endpoint

- create get.ts file for post list api
- implement pagination with page and pageSize parameters
- add user filtering by userId query parameter
- support following feed when no userId provided
- add OpenAPI schema definitions for request and response
- integrate auth middleware for authentication
- implement error handling for api requests
- connect with PostService to fetch post data
yourname 4 月之前
父節點
當前提交
829f6fd811
共有 1 個文件被更改,包括 91 次插入0 次删除
  1. 91 0
      src/server/api/posts/get.ts

+ 91 - 0
src/server/api/posts/get.ts

@@ -0,0 +1,91 @@
+import { createRoute, OpenAPIHono } from '@hono/zod-openapi';
+import { z } from '@hono/zod-openapi';
+import { AppDataSource } from '@/server/data-source';
+import { PostService } from '@/server/modules/posts/post.service';
+import { PostSchema } from '@/server/modules/posts/post.entity';
+import { AuthContext } from '@/server/types/context';
+import { authMiddleware } from '@/server/middleware/auth.middleware';
+import { ErrorSchema } from '@/server/utils/errorHandler';
+
+// 查询参数Schema
+const QuerySchema = z.object({
+  page: z.coerce.number().int().positive().default(1).openapi({
+    example: 1,
+    description: '页码'
+  }),
+  pageSize: z.coerce.number().int().positive().default(10).openapi({
+    example: 10,
+    description: '每页条数'
+  }),
+  userId: z.coerce.number().int().positive().optional().openapi({
+    example: 1,
+    description: '用户ID,用于筛选特定用户的帖子'
+  })
+});
+
+// 响应Schema
+const PostListResponse = z.object({
+  data: z.array(PostSchema),
+  pagination: z.object({
+    total: z.number().openapi({ example: 100, description: '总记录数' }),
+    current: z.number().openapi({ example: 1, description: '当前页码' }),
+    pageSize: z.number().openapi({ example: 10, description: '每页数量' })
+  })
+});
+
+// 路由定义
+const routeDef = createRoute({
+  method: 'get',
+  path: '/',
+  middleware: [authMiddleware],
+  request: {
+    query: QuerySchema
+  },
+  responses: {
+    200: {
+      description: '成功获取帖子列表',
+      content: { 'application/json': { schema: PostListResponse } }
+    },
+    400: {
+      description: '请求参数错误',
+      content: { 'application/json': { schema: ErrorSchema } }
+    },
+    500: {
+      description: '服务器错误',
+      content: { 'application/json': { schema: ErrorSchema } }
+    }
+  }
+});
+
+// 路由实现
+const app = new OpenAPIHono<AuthContext>().openapi(routeDef, async (c) => {
+  try {
+    const { page, pageSize, userId } = c.req.valid('query');
+    const postService = new PostService(AppDataSource);
+    
+    let posts, total;
+    
+    if (userId) {
+      // 获取特定用户的帖子
+      [posts, total] = await postService.getUserPosts(userId, page, pageSize);
+    } else {
+      // 获取当前用户关注的人的帖子流
+      const user = c.get('user');
+      [posts, total] = await postService.getFollowingFeed(user.id, page, pageSize);
+    }
+    
+    return c.json({
+      data: posts,
+      pagination: {
+        total,
+        current: page,
+        pageSize
+      }
+    }, 200);
+  } catch (error) {
+    const { code = 500, message = '获取帖子列表失败' } = error as Error & { code?: number };
+    return c.json({ code, message }, code as unknown as 400 | 500);
+  }
+});
+
+export default app;