From 43045d47832b0dfd954e23807b165ad0e8fadee4 Mon Sep 17 00:00:00 2001
From: Kim Siwon <shmd01@ajou.ac.kr>
Date: Thu, 5 Dec 2019 17:17:11 +0900
Subject: [PATCH] Revise Program APIs

---
 routes/program.js | 241 ++++++++++++++++++++++++++++++++--------------
 1 file changed, 171 insertions(+), 70 deletions(-)

diff --git a/routes/program.js b/routes/program.js
index 46faef0..7211632 100644
--- a/routes/program.js
+++ b/routes/program.js
@@ -1,9 +1,11 @@
 const express = require('express');
 const db = require('../models/index.js');
 
+const Strings = require('../resources/strings.json');
+
 const router = express.Router();
 
-const programDB = db.Program;
+const { Program } = db;
 
 /*
   {
@@ -15,7 +17,7 @@ const programDB = db.Program;
 /* �⑤뜡�� Program瑜� �낅젰�⑸땲��. */
 router.get('/addrandom', (req, res) => {
   const seed = Math.round(Math.random() * 10000);
-  programDB.create({
+  Program.create({
     title: `Program Title ${seed}`,
     detail: `Program Detail ${seed}`,
     durationTime: seed,
@@ -29,91 +31,190 @@ router.get('/addrandom', (req, res) => {
 
 /* GET Program List */
 router.get('/', (req, res) => {
-  programDB.findAll().then((result) => {
-    res.json({ status: 'success', result });
-  }).catch((err) => {
-    res.json({ status: 'error', error: err });
-  });
+  const queryOption = {
+    attributes: ['id', 'title', 'detail', 'duration_time', 'createdAt', 'updatedAt'],
+  };
+  Program.findAll(queryOption)
+    .then((result) => {
+      const resPayload = {
+        result,
+      };
+      res
+        .status(200)
+        .json(resPayload);
+    }).catch((err) => {
+      const resPayload = {
+        message: err.message,
+      };
+      res
+        .status(500)
+        .json(resPayload);
+    });
 });
 
 /* Post Create Program */
-router.post('/', (req, res) => {
-  programDB.create(req.body)
-    .then((result) => {
-      res.json({ status: 'success', result });
-    }).catch((err) => {
-      res.json({ status: 'error', error: err });
+// eslint-disable-next-line consistent-return
+router.post('/', (req, res, next) => {
+  if (!('title' in req.body) || !('durationTime' in req.body)) {
+    const resPayload = {
+      message: Strings.ERR.INVALID_REQUEST,
+    };
+    res
+      .status(400)
+      .json(resPayload);
+
+    return next();
+  }
+
+  const queryValue = {
+    title: req.body.title,
+    detail: req.body.detail,
+    durationTime: req.body.durationTime,
+  };
+
+  Program.create(queryValue)
+    .then((program) => {
+      res
+        .status(201)
+        .json(program);
+
+      return next();
+    })
+    .catch((e) => {
+      const resPayload = {
+        message: e.message,
+      };
+      res
+        .status(500)
+        .json(resPayload);
+
+      return next();
     });
 });
 
 /* GET Read Program by ID */
-router.get('/:id', (req, res) => {
-  programDB.findAll({ where: { id: req.params.id } }).then((result) => {
-    try {
-      if (result.length === 0) {
-        res.status(404).json({ status: 'error', error: 'There is no Program' });
-      } else {
-        res.json({ status: 'success', result });
+// eslint-disable-next-line consistent-return
+router.get('/:id', (req, res, next) => {
+  if (!('id' in req.params)) {
+    const resPayload = {
+      message: Strings.ERR.INVALID_REQUEST,
+    };
+    res
+      .status(400)
+      .json(resPayload);
+
+    return next();
+  }
+
+  const queryOption = {
+    attributes: ['id', 'title', 'detail', 'duration_time', 'createdAt', 'updatedAt'],
+  };
+  Program.findByPk(req.params.id, queryOption)
+    .then((program) => {
+      if (!program) {
+        res
+          .status(404)
+          .end();
+
+        return next();
       }
-    } catch (exception) {
-      res.json({ status: 'error', error: exception });
-    }
-  }).catch((err) => {
-    res.json({ status: 'error', error: err });
-  });
+      const resPayload = {
+        program,
+      };
+      res
+        .status(200)
+        .json(resPayload);
+
+      return next();
+    }).catch((e) => {
+      const resPayload = {
+        message: e.message,
+      };
+      res
+        .status(500)
+        .json(resPayload);
+
+      return next();
+    });
 });
 
 /* Put Update Program by ID */
-router.put('/:id', (req, res) => {
-  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 });
+router.put('/:id', (req, res, next) => {
+  const queryOption = {
+    attributes: ['id', 'title', 'detail', 'duration_time', 'createdAt', 'updatedAt'],
+  };
+  Program.findByPk(req.params.id, queryOption)
+    .then((program) => {
+      if (!program) {
+        res
+          .status(404)
+          .end();
+
+        return next();
       }
+
+      // eslint-disable-next-line no-param-reassign
+      if ('title' in req.body) program.title = req.body.title;
+      // eslint-disable-next-line no-param-reassign
+      if ('detail' in req.body) program.detail = req.body.detail;
+      // eslint-disable-next-line no-param-reassign
+      if ('durationTime' in req.body) program.durationTime = req.durationTime.phone;
+
+
+      return program.save();
+    })
+    .then((program) => {
+      const resPayload = {
+        program,
+      };
+      res
+        .status(200)
+        .json(resPayload);
+
+      return next();
+    })
+    .catch((e) => {
+      const resPayload = {
+        message: e.message,
+      };
+      res
+        .status(500)
+        .json(resPayload);
+
+      return next();
     });
 });
 
 /* Delete Program by ID */
-router.delete('/:id', (req, res) => {
-  programDB.findAll({ where: { id: req.params.id } }).then((result) => {
-    try {
-      if (result.length === 0) {
-        return new Promise(((resolve, reject) => {
-          reject(Error(404));
-        }));
+router.delete('/:id', (req, res, next) => {
+  Program.findByPk(req.params.id)
+    .then((program) => {
+      if (!program) {
+        res
+          .status(404)
+          .end();
+        return next();
       }
-      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 });
-    }
-  });
+
+      return program.destroy();
+    })
+    .then(() => {
+      res
+        .status(204)
+        .end();
+
+      return next();
+    })
+    .catch((e) => {
+      const resPayload = {
+        message: e.message,
+      };
+      res
+        .status(500)
+        .json(resPayload);
+
+      return next();
+    });
 });
 
 module.exports = router;
-- 
GitLab