delete-system-config.mt.ts 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import { createRoute, OpenAPIHono } from '@hono/zod-openapi';
  2. import { z } from '@hono/zod-openapi';
  3. import { authMiddleware } from '../../../../auth-module-mt/src/middleware/index.mt';
  4. import { AppDataSource, ErrorSchema } from '@d8d/shared-utils';
  5. import { AuthContext } from '@d8d/shared-types';
  6. import { SystemConfigServiceMt } from '../../services/system-config.service.mt';
  7. const deleteSystemConfigRoute = createRoute({
  8. method: 'delete',
  9. path: '/{id}',
  10. middleware: [authMiddleware],
  11. request: {
  12. params: z.object({
  13. id: z.coerce.number<number>().openapi({
  14. description: '系统配置ID',
  15. example: 1
  16. })
  17. })
  18. },
  19. responses: {
  20. 204: {
  21. description: '系统配置删除成功'
  22. },
  23. 400: {
  24. description: '请求参数错误',
  25. content: { 'application/json': { schema: ErrorSchema } }
  26. },
  27. 404: {
  28. description: '系统配置不存在',
  29. content: { 'application/json': { schema: ErrorSchema } }
  30. },
  31. 500: {
  32. description: '服务器内部错误',
  33. content: { 'application/json': { schema: ErrorSchema } }
  34. }
  35. }
  36. });
  37. const deleteSystemConfigRoutes = new OpenAPIHono<AuthContext>()
  38. // 删除系统配置路由
  39. .openapi(deleteSystemConfigRoute, async (c) => {
  40. const { id } = c.req.valid('param');
  41. try {
  42. const systemConfigService = new SystemConfigServiceMt(AppDataSource);
  43. // 使用delete方法,它会自动处理缓存刷新
  44. const result = await systemConfigService.delete(id);
  45. if (!result) {
  46. return c.json({ code: 404, message: '系统配置不存在' }, 404);
  47. }
  48. return c.body(null, 204);
  49. } catch (error) {
  50. console.error('删除系统配置失败:', error);
  51. return c.json(
  52. { code: 500, message: error instanceof Error ? error.message : '删除系统配置失败' },
  53. 500
  54. );
  55. }
  56. });
  57. export default deleteSystemConfigRoutes;