toolsLibrary.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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, tools_function_name, tools_full_name, tools_desc,
  58. tools_version, access_task_id, status, call_type, api_provider,
  59. api_url_path, operate_path_data, params_definition, response_desc
  60. } = req.body;
  61. const sql = `
  62. UPDATE tools_library
  63. SET tools_name = ?, tools_function_name = ?, tools_full_name = ?,
  64. tools_desc = ?, tools_version = ?, access_task_id = ?,
  65. status = ?, call_type = ?, api_provider = ?, api_url_path = ?,
  66. operate_path_data = ?, params_definition = ?, response_desc = ?,
  67. update_time = NOW()
  68. WHERE tools_id = ?
  69. `;
  70. await executeQuery(sql, [
  71. tools_name, tools_function_name, tools_full_name, tools_desc,
  72. tools_version, access_task_id, status, call_type, api_provider,
  73. api_url_path, operate_path_data, params_definition, response_desc, id
  74. ]);
  75. res.json({ message: 'Tool updated successfully' });
  76. } catch (error) {
  77. console.error('Error updating tool:', error);
  78. res.status(500).json({ error: 'Internal server error' });
  79. }
  80. });
  81. router.post('/:id/publish', async (req, res) => {
  82. try {
  83. const { id } = req.params;
  84. const sql = `
  85. UPDATE tools_library
  86. SET status = 'normal', update_time = NOW()
  87. WHERE tools_id = ?
  88. `;
  89. await executeQuery(sql, [id]);
  90. res.json({ message: 'Tool published successfully' });
  91. } catch (error) {
  92. console.error('Error publishing tool:', error);
  93. res.status(500).json({ error: 'Internal server error' });
  94. }
  95. });
  96. module.exports = router;