Files
TK-FB-Project/api.hyungi.net/controllers/cuttingPlanController.js

85 lines
2.6 KiB
JavaScript

// controllers/cuttingPlanController.js
const cuttingPlanModel = require('../models/cuttingPlanModel');
// 1. 생성
exports.createCuttingPlan = async (req, res) => {
try {
const planData = req.body;
const lastID = await new Promise((resolve, reject) => {
cuttingPlanModel.create(planData, (err, id) => {
if (err) return reject(err);
resolve(id);
});
});
res.json({ success: true, cutting_plan_id: lastID });
} catch (err) {
res.status(500).json({ error: err.message || String(err) });
}
};
// 2. 전체 조회
exports.getAllCuttingPlans = async (req, res) => {
try {
const rows = await new Promise((resolve, reject) => {
cuttingPlanModel.getAll((err, data) => {
if (err) return reject(err);
resolve(data);
});
});
res.json(rows);
} catch (err) {
res.status(500).json({ error: err.message || String(err) });
}
};
// 3. 단일 조회
exports.getCuttingPlanById = async (req, res) => {
try {
const cutting_plan_id = parseInt(req.params.cutting_plan_id, 10);
const row = await new Promise((resolve, reject) => {
cuttingPlanModel.getById(cutting_plan_id, (err, data) => {
if (err) return reject(err);
resolve(data);
});
});
if (!row) return res.status(404).json({ error: 'CuttingPlan not found' });
res.json(row);
} catch (err) {
res.status(500).json({ error: err.message || String(err) });
}
};
// 4. 수정
exports.updateCuttingPlan = async (req, res) => {
try {
const cutting_plan_id = parseInt(req.params.cutting_plan_id, 10);
const planData = { ...req.body, cutting_plan_id };
const changes = await new Promise((resolve, reject) => {
cuttingPlanModel.update(planData, (err, count) => {
if (err) return reject(err);
resolve(count);
});
});
if (changes === 0) return res.status(404).json({ error: 'No changes or not found' });
res.json({ success: true, changes });
} catch (err) {
res.status(500).json({ error: err.message || String(err) });
}
};
// 5. 삭제
exports.removeCuttingPlan = async (req, res) => {
try {
const cutting_plan_id = parseInt(req.params.cutting_plan_id, 10);
const changes = await new Promise((resolve, reject) => {
cuttingPlanModel.remove(cutting_plan_id, (err, count) => {
if (err) return reject(err);
resolve(count);
});
});
if (changes === 0) return res.status(404).json({ error: 'CuttingPlan not found' });
res.json({ success: true, changes });
} catch (err) {
res.status(500).json({ error: err.message || String(err) });
}
};