From bf5c7b260046d0c0855b09bf4cbc97a28e7fdd37 Mon Sep 17 00:00:00 2001
From: JunGu Kang <chr0m3.kr@gmail.com>
Date: Sat, 16 Nov 2019 19:24:13 +0900
Subject: [PATCH] Add Schedule Model

See #4 for details.
---
 models/index.js    |  7 ++++---
 models/schedule.js | 46 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 50 insertions(+), 3 deletions(-)
 create mode 100644 models/schedule.js

diff --git a/models/index.js b/models/index.js
index 653c03a..6e778f9 100644
--- a/models/index.js
+++ b/models/index.js
@@ -26,11 +26,12 @@ fs
 db.sequelize = sequelize;
 db.Sequelize = Sequelize;
 
-db.Trainer = require('./trainer')(sequelize, Sequelize);
-db.Trainee = require('./trainee')(sequelize, Sequelize);
+db.Inventory = require('./inventory')(sequelize, Sequelize);
 db.Program = require('./program')(sequelize, Sequelize);
+db.Schedule = require('./schedule')(sequelize, Sequelize);
+db.Trainee = require('./trainee')(sequelize, Sequelize);
+db.Trainer = require('./trainer')(sequelize, Sequelize);
 db.Voucher = require('./voucher')(sequelize, Sequelize);
-db.Inventory = require('./inventory')(sequelize, Sequelize);
 
 Object.keys(db).forEach((modelName) => {
   if (db[modelName].associate) {
diff --git a/models/schedule.js b/models/schedule.js
new file mode 100644
index 0000000..59732d1
--- /dev/null
+++ b/models/schedule.js
@@ -0,0 +1,46 @@
+module.exports = (sequelize, DataTypes) => {
+  const schedule = sequelize.define('schedule', {
+    startAt: {
+      type: DataTypes.DATE,
+      allowNull: false,
+      validate: {
+        isDate: true,
+      },
+    },
+    openAt: {
+      type: DataTypes.DATE,
+      allowNull: true,
+      validate: {
+        isDate: true,
+      },
+    },
+    closeAt: {
+      type: DataTypes.DATE,
+      allowNull: true,
+      validate: {
+        isDate: true,
+      },
+    },
+  }, {
+    underscored: true,
+    timestamps: true,
+    classMethods: {},
+  });
+
+  schedule.associate = (models) => {
+    schedule.belongsTo(models.Program, {
+      foreignKey: {
+        allowNull: false,
+      },
+      onDelete: 'RESTRICT',
+    });
+    schedule.belongsTo(models.Trainer, {
+      foreignKey: {
+        allowNull: false,
+      },
+      onDelete: 'RESTRICT',
+    });
+  };
+
+  return schedule;
+};
-- 
GitLab