diff --git a/routes/program.js b/routes/program.js index c6a41a44ff38754ad820a684094f514eedb41a1f..46faef0b5a3e6f1e38a8ee12a566582c1c1edf3e 100644 --- a/routes/program.js +++ b/routes/program.js @@ -9,7 +9,7 @@ const programDB = db.Program; { "title" : "Program Title" "detail" : "Program Detail" - "duration_time" : 100000 + "durationTime" : 100000 } */ /* �⑤뜡�� Program瑜� �낅젰�⑸땲��. */ @@ -49,7 +49,15 @@ router.post('/', (req, res) => { /* GET Read Program by ID */ router.get('/:id', (req, res) => { programDB.findAll({ where: { id: req.params.id } }).then((result) => { - res.json({ status: 'success', result }); + try { + if (result.length === 0) { + res.status(404).json({ status: 'error', error: 'There is no Program' }); + } else { + res.json({ status: 'success', result }); + } + } catch (exception) { + res.json({ status: 'error', error: exception }); + } }).catch((err) => { res.json({ status: 'error', error: err }); }); @@ -57,22 +65,54 @@ router.get('/:id', (req, res) => { /* Put Update Program by ID */ router.put('/:id', (req, res) => { - programDB.update(req.body, { where: { id: req.params.id }, returning: true }) - .then(() => { - programDB.findAll({ where: { id: req.params.id } }).then((result) => { - res.json({ status: 'success', result }); - }).catch((err) => { - res.json({ status: 'error', error: err }); - }); - }).catch((err) => { - res.json({ status: 'error', error: err }); + programDB.findAll({ where: { id: req.params.id } }).then((result) => { + try { + if (result.length === 0) { + return new Promise(((resolve, reject) => { + reject(Error(404)); + })); + } + return programDB.update(req.body, { where: { id: req.params.id }, returning: true }); + } catch (exception) { + return new Promise(((resolve, reject) => { + reject(exception); + })); + } + }).then(() => programDB.findAll({ where: { id: req.params.id } })).then((output) => { + res.json({ status: 'success', result: output }); + }) + .catch((err) => { + if (err.message === '404') { + res.status(404).json({ status: 'error', error: 'There is no Program' }); + } else { + res.json({ status: 'error', error: err.message }); + } }); }); /* Delete Program by ID */ router.delete('/:id', (req, res) => { - programDB.destroy({ where: { id: req.params.id } }).then(() => { + programDB.findAll({ where: { id: req.params.id } }).then((result) => { + try { + if (result.length === 0) { + return new Promise(((resolve, reject) => { + reject(Error(404)); + })); + } + return programDB.destroy({ where: { id: req.params.id } }); + } catch (exception) { + return new Promise(((resolve, reject) => { + reject(exception); + })); + } + }).then(() => { res.json({ status: 'success' }); + }).catch((err) => { + if (err.message === '404') { + res.status(404).json({ status: 'error', error: 'There is no Program' }); + } else { + res.json({ status: 'error', error: err.message }); + } }); });