|
|
@@ -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;
|