From 9d01673d80bf36358274829f5d834bf3a591f28e Mon Sep 17 00:00:00 2001
From: nsc <skdiakffn@ajou.ac.kr>
Date: Sat, 23 Nov 2019 01:37:02 +0900
Subject: [PATCH] Add Booking CRUD API

---
 routes/booking.js | 93 +++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 93 insertions(+)
 create mode 100644 routes/booking.js

diff --git a/routes/booking.js b/routes/booking.js
new file mode 100644
index 0000000..8b6635a
--- /dev/null
+++ b/routes/booking.js
@@ -0,0 +1,93 @@
+const express = require('express');
+const db = require('../models/index.js');
+
+const router = express.Router();
+
+const bookingDB = db.Booking;
+
+/*
+  {
+    "memo" : "�덉빟 Text"
+  }
+*/
+/* �⑤뜡�� Booking瑜� �낅젰�⑸땲��. */
+router.get('/addrandom', (req, res) => {
+  const seed = Math.round(Math.random() * 10000);
+  bookingDB.create({
+    memo: `${seed} �덉빟 Text`,
+  }).then((result) => {
+    res.json({ status: 'success', result });
+  }).catch((err) => {
+    res.json({ status: 'error', error: err });
+  });
+});
+
+
+/* GET Booking List */
+router.get('/', (req, res) => {
+  bookingDB.findAll().then((result) => {
+    res.json({ status: 'success', result });
+  }).catch((err) => {
+    res.json({ status: 'error', error: err });
+  });
+});
+
+/* Post Create Booking */
+router.post('/', (req, res) => {
+  bookingDB.create(req.body)
+    .then((result) => {
+      res.json({ status: 'success', result });
+    }).catch((err) => {
+      res.json({ status: 'error', error: err });
+    });
+});
+
+/* GET Read Booking by ID */
+router.get('/:id', (req, res) => {
+  bookingDB.findAll({ where: { id: req.params.id } }).then((result) => {
+    res.json({ status: 'success', result });
+  }).catch((err) => {
+    res.json({ status: 'error', error: err });
+  });
+});
+
+/* GET Read Booking by programID */
+router.get('/findByProgramId/:id', (req, res) => {
+  bookingDB.findAll({ where: { programId: req.params.id } }).then((result) => {
+    res.json({ status: 'success', result });
+  }).catch((err) => {
+    res.json({ status: 'error', error: err });
+  });
+});
+
+/* GET Read Booking by trainerID */
+router.get('/findByTrainerId/:id', (req, res) => {
+  bookingDB.findAll({ where: { trainerId: req.params.id } }).then((result) => {
+    res.json({ status: 'success', result });
+  }).catch((err) => {
+    res.json({ status: 'error', error: err });
+  });
+});
+
+/* Put Update Booking by ID */
+router.put('/:id', (req, res) => {
+  bookingDB.update(req.body, { where: { id: req.params.id }, returning: true })
+    .then(() => {
+      bookingDB.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 });
+    });
+});
+
+/* Delete Booking by ID */
+router.delete('/:id', (req, res) => {
+  bookingDB.destroy({ where: { id: req.params.id } }).then(() => {
+    res.json({ status: 'success' });
+  });
+});
+
+module.exports = router;
-- 
GitLab