|
|
@@ -1,71 +0,0 @@
|
|
|
-import { createRoute, OpenAPIHono } from '@hono/zod-openapi';
|
|
|
-import { UserService } from '@/server/modules/users/user.service';
|
|
|
-import { z } from '@hono/zod-openapi';
|
|
|
-import { authMiddleware } from '@/server/middleware/auth.middleware';
|
|
|
-import { ErrorSchema } from '@/server/utils/errorHandler';
|
|
|
-import { AppDataSource } from '@/server/data-source';
|
|
|
-import { AuthContext } from '@/server/types/context';
|
|
|
-import { UserSchema, UpdateUserDto } from '@/server/modules/users/user.schema';
|
|
|
-
|
|
|
-const userService = new UserService(AppDataSource);
|
|
|
-
|
|
|
-const UpdateParams = z.object({
|
|
|
- id: z.coerce.number().openapi({
|
|
|
- param: { name: 'id', in: 'path' },
|
|
|
- example: 1,
|
|
|
- description: '用户ID'
|
|
|
- })
|
|
|
-});
|
|
|
-
|
|
|
-const routeDef = createRoute({
|
|
|
- method: 'put',
|
|
|
- path: '/{id}',
|
|
|
- middleware: [authMiddleware],
|
|
|
- request: {
|
|
|
- params: UpdateParams,
|
|
|
- body: {
|
|
|
- content: {
|
|
|
- 'application/json': {
|
|
|
- schema: UpdateUserDto
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- },
|
|
|
- responses: {
|
|
|
- 200: {
|
|
|
- description: '用户更新成功',
|
|
|
- content: { 'application/json': { schema: UserSchema } }
|
|
|
- },
|
|
|
- 400: {
|
|
|
- description: '无效输入',
|
|
|
- content: { 'application/json': { schema: ErrorSchema } }
|
|
|
- },
|
|
|
- 404: {
|
|
|
- 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 data = c.req.valid('json');
|
|
|
- const user = await userService.updateUser(id, data);
|
|
|
- if (!user) {
|
|
|
- return c.json({ code: 404, message: '用户不存在' }, 404);
|
|
|
- }
|
|
|
- return c.json(user, 200);
|
|
|
- } catch (error) {
|
|
|
- return c.json({
|
|
|
- code: 500,
|
|
|
- message: error instanceof Error ? error.message : '更新用户失败'
|
|
|
- }, 500);
|
|
|
- }
|
|
|
-});
|
|
|
-
|
|
|
-export default app;
|