|
|
@@ -0,0 +1,90 @@
|
|
|
+import { z } from '@hono/zod-openapi';
|
|
|
+import { createRoute, OpenAPIHono } from '@hono/zod-openapi';
|
|
|
+import { ExcelTemplateService } from '@/server/modules/excel/excel-template.service';
|
|
|
+import { AppDataSource } from '@/server/data-source';
|
|
|
+import { ExcelTemplateSchema } from '@/server/modules/excel/excel-template.entity';
|
|
|
+import { ErrorSchema } from '@/server/utils/errorHandler';
|
|
|
+import { AuthContext } from '@/server/types/context';
|
|
|
+import { authMiddleware } from '@/server/middleware/auth.middleware';
|
|
|
+
|
|
|
+const excelTemplateService = new ExcelTemplateService(AppDataSource);
|
|
|
+
|
|
|
+const GetParams = z.object({
|
|
|
+ id: z.string().openapi({
|
|
|
+ param: { name: 'id', in: 'path' },
|
|
|
+ example: '1',
|
|
|
+ description: '模板ID'
|
|
|
+ })
|
|
|
+});
|
|
|
+
|
|
|
+const routeDef = createRoute({
|
|
|
+ method: 'put',
|
|
|
+ path: '/{id}',
|
|
|
+ middleware: [authMiddleware],
|
|
|
+ request: {
|
|
|
+ params: GetParams,
|
|
|
+ body: {
|
|
|
+ content: {
|
|
|
+ 'application/json': {
|
|
|
+ schema: ExcelTemplateSchema.omit({
|
|
|
+ id: true,
|
|
|
+ createdBy: true,
|
|
|
+ createdAt: true,
|
|
|
+ updatedAt: true,
|
|
|
+ isDeleted: true
|
|
|
+ })
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ },
|
|
|
+ responses: {
|
|
|
+ 200: {
|
|
|
+ description: '成功更新Excel模板',
|
|
|
+ content: {
|
|
|
+ 'application/json': {
|
|
|
+ schema: ExcelTemplateSchema
|
|
|
+ }
|
|
|
+ }
|
|
|
+ },
|
|
|
+ 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 templateData = c.req.valid('json');
|
|
|
+ const updatedTemplate = await excelTemplateService.update(Number(id), templateData);
|
|
|
+
|
|
|
+ if (!updatedTemplate) {
|
|
|
+ return c.json({
|
|
|
+ code: 404,
|
|
|
+ message: '模板不存在'
|
|
|
+ }, 404);
|
|
|
+ }
|
|
|
+
|
|
|
+ return c.json(updatedTemplate, 200);
|
|
|
+ } catch (error) {
|
|
|
+ return c.json({
|
|
|
+ code: 500,
|
|
|
+ message: error instanceof Error ? error.message : '更新模板失败'
|
|
|
+ }, 500);
|
|
|
+ }
|
|
|
+});
|
|
|
+
|
|
|
+export default app;
|