95 lines
3.7 KiB
JavaScript
95 lines
3.7 KiB
JavaScript
const express = require('express');
|
|
const router = express.Router();
|
|
const { pool } = require('../db');
|
|
|
|
// GET /api/custom-metrics
|
|
router.get('/', async (req, res) => {
|
|
const { babyId } = req.query;
|
|
try {
|
|
const [rows] = await pool.query(`
|
|
SELECT * FROM \`custom_metric\`
|
|
WHERE \`is_active\` = 1 AND (\`baby_id\` = 'global' OR \`baby_id\` = ?)
|
|
ORDER BY \`id\` ASC
|
|
`, [babyId || '']);
|
|
res.json({ success: true, data: rows });
|
|
} catch (err) {
|
|
res.status(500).json({ success: false, error: err.message });
|
|
}
|
|
});
|
|
|
|
// POST /api/custom-metrics - Create new metric
|
|
router.post('/', async (req, res) => {
|
|
const { babyId, name, icon, unit, targetDaily, defaultValue, type } = req.body;
|
|
if (!name) {
|
|
return res.status(400).json({ success: false, error: '名称必填' });
|
|
}
|
|
|
|
const metricId = 'metric-' + Date.now();
|
|
const target = parseInt(targetDaily) || 1;
|
|
const unitStr = unit || '次';
|
|
const iconStr = icon || '✨';
|
|
const defValStr = String(defaultValue !== undefined && defaultValue !== null && defaultValue !== '' ? defaultValue : '1');
|
|
const typeStr = type || 'count';
|
|
const bId = babyId || 'global';
|
|
|
|
try {
|
|
await pool.query(`
|
|
INSERT INTO \`custom_metric\` (\`metric_id\`, \`baby_id\`, \`name\`, \`icon\`, \`type\`, \`unit\`, \`default_value\`, \`target_daily\`)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
|
`, [metricId, bId, name, iconStr, typeStr, unitStr, defValStr, target]);
|
|
|
|
const [created] = await pool.query('SELECT * FROM `custom_metric` WHERE `metric_id` = ?', [metricId]);
|
|
res.json({ success: true, data: created[0] });
|
|
} catch (err) {
|
|
console.error('Error creating custom metric:', err);
|
|
res.status(500).json({ success: false, error: err.message });
|
|
}
|
|
});
|
|
|
|
// PUT /api/custom-metrics/:metricId - Update metric configuration including default value
|
|
router.put('/:metricId', async (req, res) => {
|
|
const { metricId } = req.params;
|
|
const { name, icon, unit, targetDaily, defaultValue, type, isActive } = req.body;
|
|
|
|
try {
|
|
const [existing] = await pool.query('SELECT * FROM `custom_metric` WHERE `metric_id` = ?', [metricId]);
|
|
if (existing.length === 0) {
|
|
return res.status(404).json({ success: false, error: '未找到该自定义指标' });
|
|
}
|
|
|
|
const cur = existing[0];
|
|
const newName = name !== undefined ? name : cur.name;
|
|
const newIcon = icon !== undefined ? icon : cur.icon;
|
|
const newUnit = unit !== undefined ? unit : cur.unit;
|
|
const newDefVal = defaultValue !== undefined ? String(defaultValue) : cur.default_value;
|
|
const newTarget = targetDaily !== undefined ? parseInt(targetDaily) : cur.target_daily;
|
|
const newType = type !== undefined ? type : cur.type;
|
|
const newActive = isActive !== undefined ? (isActive ? 1 : 0) : cur.is_active;
|
|
|
|
await pool.query(`
|
|
UPDATE \`custom_metric\`
|
|
SET \`name\` = ?, \`icon\` = ?, \`unit\` = ?, \`default_value\` = ?, \`target_daily\` = ?, \`type\` = ?, \`is_active\` = ?
|
|
WHERE \`metric_id\` = ?
|
|
`, [newName, newIcon, newUnit, newDefVal, newTarget, newType, newActive, metricId]);
|
|
|
|
const [updated] = await pool.query('SELECT * FROM `custom_metric` WHERE `metric_id` = ?', [metricId]);
|
|
res.json({ success: true, data: updated[0] });
|
|
} catch (err) {
|
|
console.error('Error updating custom metric:', err);
|
|
res.status(500).json({ success: false, error: err.message });
|
|
}
|
|
});
|
|
|
|
// DELETE /api/custom-metrics/:metricId
|
|
router.delete('/:metricId', async (req, res) => {
|
|
const { metricId } = req.params;
|
|
try {
|
|
await pool.query('UPDATE `custom_metric` SET `is_active` = 0 WHERE `metric_id` = ?', [metricId]);
|
|
res.json({ success: true });
|
|
} catch (err) {
|
|
res.status(500).json({ success: false, error: err.message });
|
|
}
|
|
});
|
|
|
|
module.exports = router;
|