Showing
4 changed files
with
36 additions
and
3 deletions
... | @@ -3,13 +3,28 @@ const line = require("@line/bot-sdk"); | ... | @@ -3,13 +3,28 @@ const line = require("@line/bot-sdk"); |
3 | const setFlexMessage = require("./apis/setFlexMessage"); | 3 | const setFlexMessage = require("./apis/setFlexMessage"); |
4 | const fs = require("fs"); | 4 | const fs = require("fs"); |
5 | 5 | ||
6 | +const { sequelize } = require('./models') | ||
7 | + | ||
8 | +// Initialize DB connection | ||
9 | +sequelize.sync({ force: false }) | ||
10 | + .then(() => { | ||
11 | + console.log('database connection complete'); | ||
12 | + }) | ||
13 | + .catch((err) => { | ||
14 | + console.log('database connection failed'); | ||
15 | + }); | ||
16 | + | ||
17 | +// Load .env configuration | ||
6 | require("dotenv").config(); | 18 | require("dotenv").config(); |
7 | const config = { | 19 | const config = { |
8 | channelAccessToken: process.env.channelAccessToken, | 20 | channelAccessToken: process.env.channelAccessToken, |
9 | channelSecret: process.env.channelSecret, | 21 | channelSecret: process.env.channelSecret, |
10 | }; | 22 | }; |
11 | 23 | ||
24 | +// Express app server initialization | ||
12 | const app = express(); | 25 | const app = express(); |
26 | + | ||
27 | +// Create post request handler for chatbot | ||
13 | app.post("/webhook", line.middleware(config), (req, res) => { | 28 | app.post("/webhook", line.middleware(config), (req, res) => { |
14 | Promise.all(req.body.events.map(handleEvent)).then((result) => | 29 | Promise.all(req.body.events.map(handleEvent)).then((result) => |
15 | res.json(result) | 30 | res.json(result) | ... | ... |
... | @@ -34,6 +34,10 @@ Object.keys(db).forEach(modelName => { | ... | @@ -34,6 +34,10 @@ Object.keys(db).forEach(modelName => { |
34 | db.sequelize = sequelize; | 34 | db.sequelize = sequelize; |
35 | db.Sequelize = Sequelize; | 35 | db.Sequelize = Sequelize; |
36 | 36 | ||
37 | -db.user = require("./user")(sequelize, Sequelize) | 37 | +db.user = require("./user")(sequelize, Sequelize); |
38 | +db.keyword = require("./keyword")(sequelize, Sequelize); | ||
39 | + | ||
40 | +db.user.belongsToMany(db.keyword, {through: 'user_keyword'}); | ||
41 | +db.keyword.belongsToMany(db.user, {through: 'user_keyword'}); | ||
38 | 42 | ||
39 | module.exports = db; | 43 | module.exports = db; | ... | ... |
server/models/keyword.js
0 → 100644
1 | +module.exports = (sequelize, DataTypes) => { | ||
2 | + | ||
3 | + return sequelize.define("keyword", { | ||
4 | + id: { | ||
5 | + type: DataTypes.INTEGER, | ||
6 | + autoIncrement: true, | ||
7 | + primaryKey: true | ||
8 | + }, | ||
9 | + keyword: { | ||
10 | + type: DataTypes.STRING, | ||
11 | + allowNull: false | ||
12 | + } | ||
13 | + }) | ||
14 | +} | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
... | @@ -2,12 +2,12 @@ module.exports = (sequelize, DataTypes) => { | ... | @@ -2,12 +2,12 @@ module.exports = (sequelize, DataTypes) => { |
2 | 2 | ||
3 | return sequelize.define("user", { | 3 | return sequelize.define("user", { |
4 | id: { | 4 | id: { |
5 | - type: DataTypes.INT, | 5 | + type: DataTypes.INTEGER, |
6 | autoIncrement: true, | 6 | autoIncrement: true, |
7 | primaryKey: true | 7 | primaryKey: true |
8 | }, | 8 | }, |
9 | userId: { | 9 | userId: { |
10 | - type: DataTypes.INT, | 10 | + type: DataTypes.INTEGER, |
11 | allowNull: false | 11 | allowNull: false |
12 | } | 12 | } |
13 | }) | 13 | }) | ... | ... |
-
Please register or login to post a comment