윤준석

ADD: get Keywords by user id function

...@@ -27,3 +27,25 @@ database.addKeyword = async function(keyword, userId) { ...@@ -27,3 +27,25 @@ database.addKeyword = async function(keyword, userId) {
27 } 27 }
28 }) 28 })
29 } 29 }
30 +
31 +database.getKeywordsByUserId = async function(userId) {
32 + const keywords = await Keyword.findAll({
33 + attributes: ['keyword'],
34 + where: {
35 + '$user.userId$': userId
36 + },
37 + include: [{
38 + attributes: [],
39 + model: User,
40 + as: 'user'
41 + }],
42 + raw: true
43 + })
44 +
45 + let result = []
46 + for (let i = 0; i < keywords.length; i++) {
47 + result.push(keywords[i].keyword)
48 + }
49 +
50 + return result
51 +}
......
...@@ -10,7 +10,8 @@ const database = require("./apis/database"); ...@@ -10,7 +10,8 @@ const database = require("./apis/database");
10 sequelize.sync({ force: false }) 10 sequelize.sync({ force: false })
11 .then(() => { 11 .then(() => {
12 console.log('database connection complete'); 12 console.log('database connection complete');
13 - database.addKeyword("rtx3080", "phobyjun") 13 + database.addKeyword("rtx3060", "junseok")
14 + database.getKeywordsByUserId("junseok")
14 }) 15 })
15 .catch((err) => { 16 .catch((err) => {
16 console.log('database connection failed'); 17 console.log('database connection failed');
......
...@@ -38,7 +38,7 @@ db.user = require("./user")(sequelize, Sequelize); ...@@ -38,7 +38,7 @@ 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 db.userKeyword = sequelize.define('user_keywords');
40 40
41 -db.user.belongsToMany(db.keyword, {through: db.userKeyword}); 41 +db.user.belongsToMany(db.keyword, {through: db.userKeyword, as: 'keyword'});
42 -db.keyword.belongsToMany(db.user, {through: db.userKeyword}); 42 +db.keyword.belongsToMany(db.user, {through: db.userKeyword, as: 'user'});
43 43
44 module.exports = db; 44 module.exports = db;
......