XNSim/XNSimHtml/routes/configurations.js

100 lines
2.9 KiB
JavaScript
Raw Normal View History

const express = require('express');
const router = express.Router();
const {
getConfigurations,
getConfigurationById,
createConfiguration,
updateConfiguration,
deleteConfiguration
} = require('../utils/configuration-utils');
2025-05-16 15:30:54 +08:00
const { validatePaths } = require('../utils/path-utils');
// 获取所有配置列表
router.get('/configurations', (req, res) => {
try {
const planeName = req.query.plane;
const configs = getConfigurations(planeName);
res.json(configs);
} catch (error) {
console.error('获取配置列表失败:', error);
res.status(500).json({ error: '获取配置列表失败', details: error.message });
}
});
// 根据ID获取配置
router.get('/configurations/:id', (req, res) => {
try {
const config = getConfigurationById(req.params.id);
if (!config) {
return res.status(404).json({ error: '配置不存在' });
}
res.json(config);
} catch (error) {
console.error('获取配置详情失败:', error);
res.status(500).json({ error: '获取配置详情失败', details: error.message });
}
});
// 创建新配置
router.post('/configurations', (req, res) => {
try {
const result = createConfiguration(req.body);
res.status(201).json(result);
} catch (error) {
console.error('创建配置失败:', error);
res.status(500).json({ error: '创建配置失败', details: error.message });
}
});
// 更新配置
2025-05-16 15:30:54 +08:00
router.put('/configurations/:id', async (req, res) => {
try {
const configData = { ...req.body, ConfID: req.params.id };
2025-05-16 15:30:54 +08:00
// 验证路径
const pathValidation = await validatePaths({
workPath: configData.WorkPath,
modelsPath: configData.ModelsPath,
servicesPath: configData.ServicesPath
});
// 检查是否有无效路径
const invalidPaths = Object.entries(pathValidation)
.filter(([_, result]) => !result.valid)
.map(([key, result]) => ({
path: key,
error: result.error
}));
if (invalidPaths.length > 0) {
return res.status(400).json({
error: '路径验证失败',
details: invalidPaths
});
}
// 使用规范化后的路径更新配置
configData.WorkPath = pathValidation.workPath.normalizedPath;
configData.ModelsPath = pathValidation.modelsPath.normalizedPath;
configData.ServicesPath = pathValidation.servicesPath.normalizedPath;
const result = updateConfiguration(configData);
res.json(result);
} catch (error) {
console.error('更新配置失败:', error);
res.status(500).json({ error: '更新配置失败', details: error.message });
}
});
// 删除配置
router.delete('/configurations/:id', (req, res) => {
try {
const result = deleteConfiguration(req.params.id);
res.json(result);
} catch (error) {
console.error('删除配置失败:', error);
res.status(500).json({ error: '删除配置失败', details: error.message });
}
});
module.exports = router;