autoAccessTasks.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 access_task_id, search_task_id, tools_name, tools_function_name,
  10. access_type, tools_function_desc, api_doc, api_class_name,
  11. operate_path_data, origin_content_link, status, fail_reason,
  12. create_time, update_time
  13. FROM tools_auto_access_task
  14. ORDER BY create_time DESC
  15. LIMIT ? OFFSET ?
  16. `;
  17. const countSql = `SELECT COUNT(*) as total FROM tools_auto_access_task`;
  18. const [data, countResult] = await Promise.all([
  19. executeQuery(sql, [parseInt(pageSize), offset]),
  20. executeQuery(countSql)
  21. ]);
  22. res.json({
  23. data,
  24. total: countResult[0].total,
  25. page: parseInt(page),
  26. pageSize: parseInt(pageSize)
  27. });
  28. } catch (error) {
  29. console.error('Error fetching auto access tasks:', error);
  30. res.status(500).json({ error: 'Internal server error' });
  31. }
  32. });
  33. router.get('/:id', async (req, res) => {
  34. try {
  35. const { id } = req.params;
  36. const sql = `
  37. SELECT access_task_id, search_task_id, tools_name, tools_function_name,
  38. access_type, tools_function_desc, api_doc, api_class_name,
  39. operate_path_data, origin_content_link, status, fail_reason,
  40. create_time, update_time
  41. FROM tools_auto_access_task
  42. WHERE access_task_id = ?
  43. `;
  44. const data = await executeQuery(sql, [id]);
  45. if (data.length === 0) {
  46. return res.status(404).json({ error: 'Task not found' });
  47. }
  48. res.json(data[0]);
  49. } catch (error) {
  50. console.error('Error fetching auto access task detail:', error);
  51. res.status(500).json({ error: 'Internal server error' });
  52. }
  53. });
  54. router.put('/:id', async (req, res) => {
  55. try {
  56. const { id } = req.params;
  57. const {
  58. search_task_id, tools_name, tools_function_name, access_type,
  59. tools_function_desc, api_doc, api_class_name, operate_path_data,
  60. origin_content_link, status, fail_reason
  61. } = req.body;
  62. const sql = `
  63. UPDATE tools_auto_access_task
  64. SET search_task_id = ?, tools_name = ?, tools_function_name = ?,
  65. access_type = ?, tools_function_desc = ?, api_doc = ?,
  66. api_class_name = ?, operate_path_data = ?, origin_content_link = ?,
  67. status = ?, fail_reason = ?, update_time = NOW()
  68. WHERE access_task_id = ?
  69. `;
  70. await executeQuery(sql, [
  71. search_task_id, tools_name, tools_function_name, access_type,
  72. tools_function_desc, api_doc, api_class_name, operate_path_data,
  73. origin_content_link, status, fail_reason, id
  74. ]);
  75. res.json({ message: 'Task updated successfully' });
  76. } catch (error) {
  77. console.error('Error updating auto access task:', error);
  78. res.status(500).json({ error: 'Internal server error' });
  79. }
  80. });
  81. module.exports = router;