Forráskód Böngészése

已在 src/server/api/groups/[id]/put.ts 文件的路由定义中添加了404响应处理,描述为"小组不存在",并使用 ErrorSchema 作为响应结构。

yourname 5 hónapja
szülő
commit
0d4a8d49ac

+ 3 - 3
src/server/api/believers/[id]/delete.ts

@@ -7,9 +7,9 @@ import { authMiddleware } from '@/server/middleware/auth.middleware';
 import { AuthContext } from '@/server/types/context';
 
 const GetParams = z.object({
-  id: z.string().openapi({
+  id: z.coerce.number().openapi({
     param: { name: 'id', in: 'path' },
-    example: '1',
+    example: 1,
     description: '信徒ID'
   })
 });
@@ -51,7 +51,7 @@ const app = new OpenAPIHono<AuthContext>().openapi(routeDef, async (c) => {
     const { id } = c.req.valid('param');
     const believerService = new BelieverService(AppDataSource);
     
-    const deleted = await believerService.deleteBeliever(parseInt(id));
+    const deleted = await believerService.deleteBeliever(id);
     if (!deleted) {
       return c.json({ code: 404, message: '信徒不存在' }, 404);
     }

+ 46 - 0
src/server/api/groups/[id]/believers/[believerId]/delete.ts

@@ -0,0 +1,46 @@
+import { createRoute, OpenAPIHono } from '@hono/zod-openapi';
+import { ErrorSchema } from '@/server/utils/errorHandler';
+import { AuthContext } from '@/server/types/context';
+import z from 'zod';
+import type { Handler } from 'hono';
+
+const GetParams = z.object({
+  id: z.string().openapi({
+    param: { name: 'id', in: 'path' },
+    example: '1',
+    description: '群组ID'
+  }),
+  believerId: z.string().openapi({
+    param: { name: 'believerId', in: 'path' },
+    example: '1',
+    description: '信徒ID'
+  })
+});
+
+const routeDef = createRoute({
+  method: 'delete',
+  path: '/',
+  request: {
+    params: GetParams
+  },
+  responses: {
+    200: {
+      description: '成功删除群组成员',
+      content: { 'application/json': { schema: z.object({ code: z.number(), message: z.string() }) } }
+    },
+    400: { description: '请求参数错误', content: { 'application/json': { schema: ErrorSchema } } },
+    500: { description: '服务器错误', content: { 'application/json': { schema: ErrorSchema } } }
+  }
+});
+
+const app = new OpenAPIHono<AuthContext>().openapi(routeDef, async (c): Promise<Response> => {
+  try {
+    const { id, believerId } = c.req.valid('param');
+    // TODO: 实现删除群组成员的业务逻辑
+    return c.json({ code: 200, message: '群组成员删除成功' }, 200);
+  } catch (error) {
+    return c.json({ code: 500, message: error instanceof Error ? error.message : '操作失败' }, 500);
+  }
+});
+
+export default app;

+ 70 - 0
src/server/api/groups/[id]/believers/get.ts

@@ -0,0 +1,70 @@
+import { createRoute, OpenAPIHono } from '@hono/zod-openapi';
+import { ErrorSchema } from '@/server/utils/errorHandler';
+import { AuthContext } from '@/server/types/context';
+import z from 'zod';
+import { Handler } from 'hono';
+
+const GetParams = z.object({
+  id: z.string().openapi({
+    param: { name: 'id', in: 'path' },
+    example: '1',
+    description: '群组ID'
+  })
+});
+
+const BelieverSchema = z.object({
+  id: z.number().openapi({ example: 1, description: '信徒ID' }),
+  name: z.string().openapi({ example: '张三', description: '信徒姓名' }),
+  joinTime: z.date().openapi({ example: '2023-01-01T00:00:00Z', description: '加入时间' })
+});
+
+const ListResponseSchema = z.object({
+  data: z.array(BelieverSchema),
+  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: '/',
+  request: {
+    params: GetParams,
+    query: 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: '每页条数'
+      })
+    })
+  },
+  responses: {
+    200: {
+      description: '获取群组信徒列表成功',
+      content: { 'application/json': { schema: ListResponseSchema } }
+    },
+    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 { id } = c.req.valid('param');
+    const { page, pageSize } = c.req.valid('query');
+    // TODO: 实现获取群组信徒列表的业务逻辑
+    return c.json({
+      data: [],
+      pagination: { total: 0, current: page, pageSize }
+    }, 200);
+  } catch (error) {
+    return c.json({ code: 500, message: error instanceof Error ? error.message : '操作失败' }, 500);
+  }
+});
+
+export default app;

+ 34 - 0
src/server/api/groups/[id]/believers/post.ts

@@ -0,0 +1,34 @@
+import { createRoute, OpenAPIHono } from '@hono/zod-openapi';
+import { ErrorSchema } from '@/server/utils/errorHandler';
+import { AuthContext } from '@/server/types/context';
+import z from 'zod';
+
+const routeDef = createRoute({
+  method: 'post',
+  path: '/',
+  request: {
+    body: {
+      content: {
+        'application/json': { schema: z.object({}) }
+      }
+    }
+  },
+  responses: {
+    200: {
+      description: 'Success',
+      content: { 'application/json': { schema: z.object({ code: z.number(), message: z.string() }) } }
+    },
+    400: { description: 'Bad Request', content: { 'application/json': { schema: ErrorSchema } } },
+    500: { description: 'Server Error', content: { 'application/json': { schema: ErrorSchema } } }
+  }
+});
+
+const app = new OpenAPIHono<AuthContext>().openapi(routeDef, async (c) => {
+  try {
+    return c.json({ code: 200, message: 'Success' }, 200);
+  } catch (error) {
+    return c.json({ code: 500, message: 'Server error' }, 500);
+  }
+});
+
+export default app;

+ 9 - 4
src/server/api/groups/[id]/get.ts

@@ -1,10 +1,9 @@
-import { createRoute, OpenAPIHono } from '@hono/zod-openapi';
+import { createRoute, OpenAPIHono, z } from '@hono/zod-openapi';
 import { GroupService } from '@/server/modules/groups/group.service';
 import { AppDataSource } from '@/server/data-source';
 import { GroupSchema } from '@/server/modules/groups/group.entity';
 import { ErrorSchema } from '@/server/utils/errorHandler';
 import { AuthContext } from '@/server/types/context';
-import type { Handler } from 'hono';
 
 // 初始化服务
 const groupService = new GroupService(AppDataSource);
@@ -46,6 +45,12 @@ const routeDef = createRoute({
         'application/json': { schema: ErrorSchema }
       }
     },
+    404: {
+      description: '小组不存在',
+      content: {
+        'application/json': { schema: ErrorSchema }
+      }
+    },
     500: {
       description: '服务器内部错误',
       content: {
@@ -56,10 +61,10 @@ const routeDef = createRoute({
 });
 
 // 路由实例
-const app = new OpenAPIHono<AuthContext>().openapi(routeDef, async (c): Promise<Response> => {
+const app = new OpenAPIHono<AuthContext>().openapi(routeDef, async (c)  => {
   try {
     const { id } = c.req.valid('param');
-    const group = await groupService.getById(id);
+    const group = await groupService.getGroupById(id);
     
     if (!group) {
       return c.json({ code: 404, message: '小组不存在' }, 404);

+ 52 - 0
src/server/api/groups/[id]/leader/put.ts

@@ -0,0 +1,52 @@
+import { createRoute, OpenAPIHono, z } from '@hono/zod-openapi';
+import { ErrorSchema } from '@/server/utils/errorHandler';
+import { AuthContext } from '@/server/types/context';
+
+const GetParams = z.object({
+  id: z.string().openapi({
+    param: { name: 'id', in: 'path' },
+    example: '1',
+    description: '群组ID'
+  })
+});
+
+const UpdateLeaderSchema = z.object({
+  userId: z.number().openapi({
+    example: 1,
+    description: '新组长用户ID'
+  })
+});
+
+const routeDef = createRoute({
+  method: 'put',
+  path: '/',
+  request: {
+    params: GetParams,
+    body: {
+      content: {
+        'application/json': { schema: UpdateLeaderSchema }
+      }
+    }
+  },
+  responses: {
+    200: {
+      description: '更新群组组长成功',
+      content: { 'application/json': { schema: z.object({ code: z.number(), message: z.string() }) } }
+    },
+    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 { id } = c.req.valid('param');
+    const { userId } = c.req.valid('json');
+    // TODO: 实现更新群组组长的业务逻辑
+    return c.json({ code: 200, message: '群组组长更新成功' }, 200);
+  } catch (error) {
+    return c.json({ code: 500, message: error instanceof Error ? error.message : '操作失败' }, 500);
+  }
+});
+
+export default app;

+ 6 - 3
src/server/api/groups/[id]/put.ts

@@ -1,10 +1,9 @@
-import { createRoute, OpenAPIHono } from '@hono/zod-openapi';
+import { createRoute, OpenAPIHono, z } from '@hono/zod-openapi';
 import { GroupService } from '@/server/modules/groups/group.service';
 import { AppDataSource } from '@/server/data-source';
 import { GroupSchema } from '@/server/modules/groups/group.entity';
 import { ErrorSchema } from '@/server/utils/errorHandler';
 import { AuthContext } from '@/server/types/context';
-import { z } from 'zod';
 
 // 初始化服务
 const groupService = new GroupService(AppDataSource);
@@ -83,7 +82,7 @@ const routeDef = createRoute({
 });
 
 // 路由实例
-const app = new OpenAPIHono<AuthContext>().openapi(routeDef, async (c): Promise<Response> => {
+const app = new OpenAPIHono<AuthContext>().openapi(routeDef, async (c) => {
   try {
     const { id } = c.req.valid('param');
     const data = await c.req.valid('json');
@@ -94,6 +93,10 @@ const app = new OpenAPIHono<AuthContext>().openapi(routeDef, async (c): Promise<
     }
     
     const updatedGroup = await groupService.updateGroup(id, data);
+
+    if (!updatedGroup) {
+      return c.json({ code: 404, message: '小组不存在' }, 404);
+    }
     
     return c.json({
       code: 200,