From 69c2c8e53a01dfe725375db4091552814e7d5bc5 Mon Sep 17 00:00:00 2001 From: JunGu Kang <chr0m3.kr@gmail.com> Date: Thu, 5 Dec 2019 09:29:34 +0900 Subject: [PATCH 1/2] Fix Trainee List, Delete API Make the APIs Respond with Appropriate Status Code --- app.js | 1 + routes/trainee.js | 65 +++++++++++++++++++++++++++++++++++++---------- 2 files changed, 52 insertions(+), 14 deletions(-) diff --git a/app.js b/app.js index c99da3f..b22d95d 100644 --- a/app.js +++ b/app.js @@ -33,6 +33,7 @@ app.use(express.static(path.join(__dirname, 'public'))); app.all('/*', (req, res, next) => { res .header('Access-Control-Allow-Origin', 'http://localhost:8080') + .header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, DELETE') .header('Access-Control-Allow-Headers', 'Content-Type') .header('Access-Control-Allow-Credentials', 'true'); diff --git a/routes/trainee.js b/routes/trainee.js index 30920df..fe86669 100644 --- a/routes/trainee.js +++ b/routes/trainee.js @@ -1,9 +1,11 @@ const express = require('express'); + const db = require('../models/index.js'); + const router = express.Router(); -const traineeDB = db.Trainee; +const { Trainee } = db; /* { @@ -21,7 +23,7 @@ const traineeDB = db.Trainee; /* �⑤뜡�� trainee瑜� �낅젰�⑸땲��. */ router.get('/addrandom', (req, res) => { const seed = Math.round(Math.random() * 10000); - traineeDB.create({ + Trainee.create({ email: `user${seed}@gmail.com`, password: 'passw0rd', name: `�щ엺${seed}`, @@ -41,16 +43,33 @@ router.get('/addrandom', (req, res) => { /* GET Trainee List */ router.get('/', (req, res) => { - traineeDB.findAll().then((result) => { - res.json({ status: 'success', result }); - }).catch((err) => { - res.json({ status: 'error', error: err }); - }); + const queryOption = { + attributes: ['id', 'email', 'name', 'phone', 'birthDate', 'createdAt', 'updatedAt'], + }; + Trainee.findAll(queryOption) + .then((trainees) => { + const resPayload = { + trainees, + }; + res + .status(200) + .json(resPayload); + }) + .catch((e) => { + // Since there is no user input, it is assumed that to be an internal server error + // if the query fails. + const resPayload = { + message: e.message, + }; + res + .status(500) + .json(resPayload); + }); }); /* Post Create Trainee */ router.post('/', (req, res) => { - traineeDB.create(req.body) + Trainee.create(req.body) .then((result) => { res.json({ status: 'success', result }); }).catch((err) => { @@ -60,7 +79,7 @@ router.post('/', (req, res) => { /* GET Read Trainee by ID */ router.get('/:id', (req, res) => { - traineeDB.findAll({ where: { id: req.params.id } }).then((result) => { + Trainee.findAll({ where: { id: req.params.id } }).then((result) => { res.json({ status: 'success', result }); }).catch((err) => { res.json({ status: 'error', error: err }); @@ -69,9 +88,9 @@ router.get('/:id', (req, res) => { /* Put Update Trainee by ID */ router.put('/:id', (req, res) => { - traineeDB.update(req.body, { where: { id: req.params.id }, returning: true }) + Trainee.update(req.body, { where: { id: req.params.id }, returning: true }) .then(() => { - traineeDB.findAll({ where: { id: req.params.id } }).then((result) => { + Trainee.findAll({ where: { id: req.params.id } }).then((result) => { res.json({ status: 'success', result }); }).catch((err) => { res.json({ status: 'error', error: err }); @@ -81,11 +100,29 @@ router.put('/:id', (req, res) => { }); }); + /* Delete Trainee by ID */ router.delete('/:id', (req, res) => { - traineeDB.destroy({ where: { id: req.params.id } }).then(() => { - res.json({ status: 'success' }); - }); + const queryOption = { + where: { + id: req.params.id, + }, + }; + Trainee.destroy(queryOption) + .then(() => { + res + .status(204) + .end(); + }) + .catch((e) => { + const resPayload = { + message: e.message, + }; + res + .status(500) + .json(resPayload); + }); }); + module.exports = router; -- GitLab From e00cbe0605c7f096a7e5411f6d2a60a060d34273 Mon Sep 17 00:00:00 2001 From: nsc <skdiakffn@ajou.ac.kr> Date: Thu, 5 Dec 2019 12:02:23 +0900 Subject: [PATCH 2/2] Fix Trainee APIs: Make the APIs Respond with Appropriate Status Code and Body --- routes/trainee.js | 62 ++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 51 insertions(+), 11 deletions(-) diff --git a/routes/trainee.js b/routes/trainee.js index 30920df..89bec1b 100644 --- a/routes/trainee.js +++ b/routes/trainee.js @@ -61,7 +61,15 @@ router.post('/', (req, res) => { /* GET Read Trainee by ID */ router.get('/:id', (req, res) => { traineeDB.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 Trainee' }); + } else { + res.json({ status: 'success', result }); + } + } catch (exception) { + res.json({ status: 'error', error: exception }); + } }).catch((err) => { res.json({ status: 'error', error: err }); }); @@ -69,22 +77,54 @@ router.get('/:id', (req, res) => { /* Put Update Trainee by ID */ router.put('/:id', (req, res) => { - traineeDB.update(req.body, { where: { id: req.params.id }, returning: true }) - .then(() => { - traineeDB.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 }); + traineeDB.findAll({ where: { id: req.params.id } }).then((result) => { + try { + if (result.length === 0) { + return new Promise(((resolve, reject) => { + reject(Error(404)); + })); + } + return traineeDB.update(req.body, { where: { id: req.params.id }, returning: true }); + } catch (exception) { + return new Promise(((resolve, reject) => { + reject(exception); + })); + } + }).then(() => traineeDB.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 Trainee' }); + } else { + res.json({ status: 'error', error: err.message }); + } }); }); /* Delete Trainee by ID */ router.delete('/:id', (req, res) => { - traineeDB.destroy({ where: { id: req.params.id } }).then(() => { + traineeDB.findAll({ where: { id: req.params.id } }).then((result) => { + try { + if (result.length === 0) { + return new Promise(((resolve, reject) => { + reject(Error(404)); + })); + } + return traineeDB.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 Trainee' }); + } else { + res.json({ status: 'error', error: err.message }); + } }); }); -- GitLab