toolsLibrary.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. const express = require("express");
  2. const router = express.Router();
  3. const { executeQuery } = require("../config/database");
  4. router.get("/", async (req, res) => {
  5. try {
  6. const { page = 1, pageSize = 10 } = req.query;
  7. const offset = (page - 1) * pageSize;
  8. const sql = `
  9. SELECT tools_id, tools_name, tools_function_name, tools_full_name,
  10. tools_desc, tools_version, access_task_id, status, call_type,
  11. api_provider, api_url_path, create_time, update_time
  12. FROM tools_library
  13. ORDER BY create_time DESC
  14. LIMIT ? OFFSET ?
  15. `;
  16. const countSql = `SELECT COUNT(*) as total FROM tools_library`;
  17. const [data, countResult] = await Promise.all([
  18. executeQuery(sql, [parseInt(pageSize), offset]),
  19. executeQuery(countSql),
  20. ]);
  21. res.json({
  22. data,
  23. total: countResult[0].total,
  24. page: parseInt(page),
  25. pageSize: parseInt(pageSize),
  26. });
  27. } catch (error) {
  28. console.error("Error fetching tools library:", error);
  29. res.status(500).json({ error: "Internal server error" });
  30. }
  31. });
  32. router.get("/:id", async (req, res) => {
  33. try {
  34. const { id } = req.params;
  35. const sql = `
  36. SELECT tools_id, tools_name, tools_function_name, tools_full_name,
  37. tools_desc, tools_version, access_task_id, status, call_type,
  38. api_provider, api_url_path, operate_path_data, params_definition,
  39. response_desc, create_time, update_time
  40. FROM tools_library
  41. WHERE tools_id = ?
  42. `;
  43. const data = await executeQuery(sql, [id]);
  44. if (data.length === 0) {
  45. return res.status(404).json({ error: "Tool not found" });
  46. }
  47. res.json(data[0]);
  48. } catch (error) {
  49. console.error("Error fetching tool detail:", error);
  50. res.status(500).json({ error: "Internal server error" });
  51. }
  52. });
  53. router.put("/:id", async (req, res) => {
  54. try {
  55. const { id } = req.params;
  56. const {
  57. tools_name,
  58. tools_function_name,
  59. tools_full_name,
  60. tools_desc,
  61. tools_version,
  62. access_task_id,
  63. status,
  64. call_type,
  65. api_provider,
  66. api_url_path,
  67. operate_path_data,
  68. params_definition,
  69. response_desc,
  70. } = req.body;
  71. const sql = `
  72. UPDATE tools_library
  73. SET tools_name = ?, tools_function_name = ?, tools_full_name = ?,
  74. tools_desc = ?, tools_version = ?, access_task_id = ?,
  75. status = ?, call_type = ?, api_provider = ?, api_url_path = ?,
  76. operate_path_data = ?, params_definition = ?, response_desc = ?,
  77. update_time = NOW()
  78. WHERE tools_id = ?
  79. `;
  80. await executeQuery(sql, [
  81. tools_name,
  82. tools_function_name,
  83. tools_full_name,
  84. tools_desc,
  85. tools_version,
  86. access_task_id,
  87. status,
  88. call_type,
  89. api_provider,
  90. api_url_path,
  91. operate_path_data,
  92. params_definition,
  93. response_desc,
  94. id,
  95. ]);
  96. res.json({ message: "Tool updated successfully" });
  97. } catch (error) {
  98. console.error("Error updating tool:", error);
  99. res.status(500).json({ error: "Internal server error" });
  100. }
  101. });
  102. router.post("/:id/publish", async (req, res) => {
  103. try {
  104. const { id } = req.params;
  105. const sql = `
  106. UPDATE tools_library
  107. SET status = 'normal', update_time = NOW()
  108. WHERE tools_id = ?
  109. `;
  110. await executeQuery(sql, [id]);
  111. res.json({ message: "Tool published successfully" });
  112. } catch (error) {
  113. console.error("Error publishing tool:", error);
  114. res.status(500).json({ error: "Internal server error" });
  115. }
  116. });
  117. module.exports = router;