윤준석

ADD: addKeyword function for add keyword to database

if user is not exist in database, create user
if keyword is not exist in database, create keyword
1 +let database = module.exports = {};
2 +
3 +const db = require("../models")
4 +const User = db.user
5 +const Keyword = db.keyword
6 +const UserKeyword = db.userKeyword
7 +
8 +
9 +database.addKeyword = async function(keyword, userId) {
10 +
11 + const u = await User.findOrCreate({
12 + where: {
13 + userId: userId
14 + }
15 + })
16 +
17 + const k = await Keyword.findOrCreate({
18 + where: {
19 + keyword: keyword
20 + }
21 + })
22 +
23 + await UserKeyword.findOrCreate({
24 + where: {
25 + userId: u[0].id,
26 + keywordId: k[0].id
27 + }
28 + })
29 +}
...@@ -4,11 +4,13 @@ const setFlexMessage = require("./apis/setFlexMessage"); ...@@ -4,11 +4,13 @@ const setFlexMessage = require("./apis/setFlexMessage");
4 const fs = require("fs"); 4 const fs = require("fs");
5 5
6 const { sequelize } = require('./models') 6 const { sequelize } = require('./models')
7 +const database = require("./apis/database");
7 8
8 // Initialize DB connection 9 // Initialize DB connection
9 sequelize.sync({ force: false }) 10 sequelize.sync({ force: false })
10 .then(() => { 11 .then(() => {
11 console.log('database connection complete'); 12 console.log('database connection complete');
13 + database.addKeyword("rtx3080", "phobyjun")
12 }) 14 })
13 .catch((err) => { 15 .catch((err) => {
14 console.log('database connection failed'); 16 console.log('database connection failed');
......
...@@ -36,8 +36,9 @@ db.Sequelize = Sequelize; ...@@ -36,8 +36,9 @@ 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); 38 db.keyword = require("./keyword")(sequelize, Sequelize);
39 +db.userKeyword = sequelize.define('user_keywords');
39 40
40 -db.user.belongsToMany(db.keyword, {through: 'user_keyword'}); 41 +db.user.belongsToMany(db.keyword, {through: db.userKeyword});
41 -db.keyword.belongsToMany(db.user, {through: 'user_keyword'}); 42 +db.keyword.belongsToMany(db.user, {through: db.userKeyword});
42 43
43 module.exports = db; 44 module.exports = db;
......
...@@ -7,7 +7,7 @@ module.exports = (sequelize, DataTypes) => { ...@@ -7,7 +7,7 @@ module.exports = (sequelize, DataTypes) => {
7 primaryKey: true 7 primaryKey: true
8 }, 8 },
9 userId: { 9 userId: {
10 - type: DataTypes.INTEGER, 10 + type: DataTypes.STRING,
11 allowNull: false 11 allowNull: false
12 } 12 }
13 }) 13 })
......