const { getDb } = require('../dbPool'); // ✅ 전체 스펙 목록 (프론트 드롭다운용 label 포함) exports.getAll = async (req, res) => { try { const db = await getDb(); const [rows] = await db.query(` SELECT spec_id, material, diameter_in, schedule FROM PipeSpecs ORDER BY material, diameter_in `); const result = rows.map(row => ({ spec_id: row.spec_id, label: `${row.material} / ${row.diameter_in} / ${row.schedule}` })); res.json(result); } catch (err) { console.error('[getAll 오류]', err); res.status(500).json({ error: '파이프 스펙 전체 조회 실패' }); } }; // ✅ 등록 exports.create = async (req, res) => { try { const { material, diameter_in, schedule } = req.body; if (!material || !diameter_in || !schedule) { return res.status(400).json({ error: '모든 항목이 필요합니다.' }); } const db = await getDb(); // 중복 체크 const [existing] = await db.query( `SELECT * FROM PipeSpecs WHERE material = ? AND diameter_in = ? AND schedule = ?`, [material.trim(), diameter_in.trim(), schedule.trim()] ); if (existing.length > 0) { return res.status(409).json({ error: '이미 등록된 스펙입니다.' }); } await db.query( `INSERT INTO PipeSpecs (material, diameter_in, schedule) VALUES (?, ?, ?)`, [material.trim(), diameter_in.trim(), schedule.trim()] ); res.json({ success: true }); } catch (err) { console.error('[create 오류]', err); res.status(500).json({ error: '파이프 스펙 등록 실패' }); } }; // ✅ 삭제 exports.remove = async (req, res) => { const { spec_id } = req.params; try { const db = await getDb(); const [result] = await db.query( `DELETE FROM PipeSpecs WHERE spec_id = ?`, [spec_id] ); if (result.affectedRows === 0) { return res.status(404).json({ error: '해당 스펙이 존재하지 않습니다.' }); } res.json({ success: true }); } catch (err) { console.error('[remove 오류]', err); res.status(500).json({ error: '파이프 스펙 삭제 실패' }); } }; // ✅ 재질 목록 exports.getMaterials = async (req, res) => { try { const db = await getDb(); const [rows] = await db.query( `SELECT DISTINCT material FROM PipeSpecs ORDER BY material` ); res.json(rows.map(row => row.material)); } catch (err) { res.status(500).json({ error: '재질 목록 조회 실패' }); } }; // ✅ 직경 목록 (material 기준) exports.getDiameters = async (req, res) => { const { material } = req.query; if (!material) { return res.status(400).json({ error: 'material 파라미터가 필요합니다.' }); } try { const db = await getDb(); const [rows] = await db.query( `SELECT DISTINCT diameter_in FROM PipeSpecs WHERE material = ? ORDER BY diameter_in`, [material] ); res.json(rows.map(row => row.diameter_in)); } catch (err) { res.status(500).json({ error: '직경 목록 조회 실패' }); } }; // ✅ 스케줄 목록 (material + 직경 기준) exports.getSchedules = async (req, res) => { const { material, diameter_in } = req.query; if (!material || !diameter_in) { return res.status(400).json({ error: 'material과 diameter_in이 필요합니다.' }); } try { const db = await getDb(); const [rows] = await db.query( `SELECT DISTINCT schedule FROM PipeSpecs WHERE material = ? AND diameter_in = ? ORDER BY schedule`, [material, diameter_in] ); res.json(rows.map(row => row.schedule)); } catch (err) { res.status(500).json({ error: '스케줄 목록 조회 실패' }); } };