const express = require("express"); const router = express.Router(); const { executeQuery } = require("../config/database"); router.get("/", async (req, res) => { try { const { page = 1, pageSize = 10 } = req.query; const offset = (page - 1) * pageSize; const sql = ` SELECT tools_id, tools_name, tools_function_name, tools_full_name, tools_desc, tools_version, access_task_id, status, call_type, api_provider, api_url_path, create_time, update_time FROM tools_library ORDER BY create_time DESC LIMIT ? OFFSET ? `; const countSql = `SELECT COUNT(*) as total FROM tools_library`; const [data, countResult] = await Promise.all([ executeQuery(sql, [parseInt(pageSize), offset]), executeQuery(countSql), ]); res.json({ data, total: countResult[0].total, page: parseInt(page), pageSize: parseInt(pageSize), }); } catch (error) { console.error("Error fetching tools library:", error); res.status(500).json({ error: "Internal server error" }); } }); router.get("/:id", async (req, res) => { try { const { id } = req.params; const sql = ` SELECT tools_id, tools_name, tools_function_name, tools_full_name, tools_desc, tools_version, access_task_id, status, call_type, api_provider, api_url_path, operate_path_data, params_definition, response_desc, create_time, update_time FROM tools_library WHERE tools_id = ? `; const data = await executeQuery(sql, [id]); if (data.length === 0) { return res.status(404).json({ error: "Tool not found" }); } res.json(data[0]); } catch (error) { console.error("Error fetching tool detail:", error); res.status(500).json({ error: "Internal server error" }); } }); router.put("/:id", async (req, res) => { try { const { id } = req.params; const { tools_name, tools_function_name, tools_full_name, tools_desc, tools_version, access_task_id, status, call_type, api_provider, api_url_path, operate_path_data, params_definition, response_desc, } = req.body; const sql = ` UPDATE tools_library SET tools_name = ?, tools_function_name = ?, tools_full_name = ?, tools_desc = ?, tools_version = ?, access_task_id = ?, status = ?, call_type = ?, api_provider = ?, api_url_path = ?, operate_path_data = ?, params_definition = ?, response_desc = ?, update_time = NOW() WHERE tools_id = ? `; await executeQuery(sql, [ tools_name, tools_function_name, tools_full_name, tools_desc, tools_version, access_task_id, status, call_type, api_provider, api_url_path, operate_path_data, params_definition, response_desc, id, ]); res.json({ message: "Tool updated successfully" }); } catch (error) { console.error("Error updating tool:", error); res.status(500).json({ error: "Internal server error" }); } }); router.post("/:id/publish", async (req, res) => { try { const { id } = req.params; const sql = ` UPDATE tools_library SET status = 'normal', update_time = NOW() WHERE tools_id = ? `; await executeQuery(sql, [id]); res.json({ message: "Tool published successfully" }); } catch (error) { console.error("Error publishing tool:", error); res.status(500).json({ error: "Internal server error" }); } }); module.exports = router;