123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- 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, mcp_tools_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, mcp_tools_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,
- mcp_tools_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 = ?, mcp_tools_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 = ?
- `;
- // 将 undefined 值转换为 null,避免数据库绑定参数错误
- const params = [
- tools_name ?? null,
- tools_function_name ?? null,
- mcp_tools_name ?? null,
- tools_full_name ?? null,
- tools_desc ?? null,
- tools_version ?? null,
- access_task_id ?? null,
- status ?? null,
- call_type ?? null,
- api_provider ?? null,
- api_url_path ?? null,
- operate_path_data ?? null,
- params_definition ?? null,
- response_desc ?? null,
- id,
- ];
- await executeQuery(sql, params);
- 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;
|