routes_file_category.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import { Hono } from "hono";
  2. import debug from "debug";
  3. import type {
  4. FileCategory,
  5. } from "../client/share/types.ts";
  6. import {
  7. DeleteStatus,
  8. } from "../client/share/types.ts";
  9. import type { Variables, WithAuth } from "./middlewares.ts";
  10. const log = {
  11. api: debug("api:sys"),
  12. };
  13. // 创建文件分类路由
  14. export function createFileCategoryRoutes(withAuth: WithAuth) {
  15. const fileCategoryRoutes = new Hono<{ Variables: Variables }>();
  16. // 获取文件分类列表
  17. fileCategoryRoutes.get("/", withAuth, async (c) => {
  18. try {
  19. const apiClient = c.get('apiClient');
  20. const page = Number(c.req.query("page")) || 1;
  21. const pageSize = Number(c.req.query("pageSize")) || 10;
  22. const offset = (page - 1) * pageSize;
  23. const search = c.req.query("search") || "";
  24. let query = apiClient.database.table("file_category").orderBy("id", "desc");
  25. if (search) {
  26. query = query.where("name", "like", `%${search}%`);
  27. }
  28. const total = await query.clone().count();
  29. const categories = await query
  30. .select("id", "name", "code", "description")
  31. .limit(pageSize)
  32. .offset(offset);
  33. return c.json({
  34. data: categories,
  35. total: Number(total),
  36. page,
  37. pageSize,
  38. });
  39. } catch (error) {
  40. log.api("获取文件分类列表失败:", error);
  41. return c.json({ error: "获取文件分类列表失败" }, 500);
  42. }
  43. });
  44. // 创建文件分类
  45. fileCategoryRoutes.post("/", withAuth, async (c) => {
  46. try {
  47. const apiClient = c.get('apiClient');
  48. const data = (await c.req.json()) as Partial<FileCategory>;
  49. // 验证必填字段
  50. if (!data.name) {
  51. return c.json({ error: "分类名称不能为空" }, 400);
  52. }
  53. // 插入文件分类
  54. const [id] = await apiClient.database.table("file_category").insert({
  55. ...data,
  56. created_at: apiClient.database.fn.now(),
  57. updated_at: apiClient.database.fn.now(),
  58. });
  59. return c.json({
  60. message: "文件分类创建成功",
  61. data: {
  62. id,
  63. ...data,
  64. },
  65. });
  66. } catch (error) {
  67. log.api("创建文件分类失败:", error);
  68. return c.json({ error: "创建文件分类失败" }, 500);
  69. }
  70. });
  71. // 更新文件分类
  72. fileCategoryRoutes.put("/:id", withAuth, async (c) => {
  73. try {
  74. const apiClient = c.get('apiClient');
  75. const id = Number(c.req.param("id"));
  76. if (!id || isNaN(id)) {
  77. return c.json({ error: "无效的分类ID" }, 400);
  78. }
  79. const data = (await c.req.json()) as Partial<FileCategory>;
  80. // 更新文件分类
  81. await apiClient.database
  82. .table("file_category")
  83. .where("id", id)
  84. .update({
  85. ...data,
  86. updated_at: apiClient.database.fn.now(),
  87. });
  88. return c.json({
  89. message: "文件分类更新成功",
  90. data: {
  91. id,
  92. ...data,
  93. },
  94. });
  95. } catch (error) {
  96. log.api("更新文件分类失败:", error);
  97. return c.json({ error: "更新文件分类失败" }, 500);
  98. }
  99. });
  100. // 删除文件分类
  101. fileCategoryRoutes.delete("/:id", withAuth, async (c) => {
  102. try {
  103. const apiClient = c.get('apiClient');
  104. const id = Number(c.req.param("id"));
  105. if (!id || isNaN(id)) {
  106. return c.json({ error: "无效的分类ID" }, 400);
  107. }
  108. await apiClient.database.table("file_category").where("id", id).update({
  109. is_deleted: DeleteStatus.DELETED,
  110. updated_at: apiClient.database.fn.now(),
  111. });
  112. return c.json({
  113. message: "文件分类删除成功",
  114. });
  115. } catch (error) {
  116. log.api("删除文件分类失败:", error);
  117. return c.json({ error: "删除文件分类失败" }, 500);
  118. }
  119. });
  120. return fileCategoryRoutes;
  121. }