윤준석

ADD: get Keywords by user id function

......@@ -27,3 +27,25 @@ database.addKeyword = async function(keyword, userId) {
}
})
}
database.getKeywordsByUserId = async function(userId) {
const keywords = await Keyword.findAll({
attributes: ['keyword'],
where: {
'$user.userId$': userId
},
include: [{
attributes: [],
model: User,
as: 'user'
}],
raw: true
})
let result = []
for (let i = 0; i < keywords.length; i++) {
result.push(keywords[i].keyword)
}
return result
}
......
......@@ -10,7 +10,8 @@ const database = require("./apis/database");
sequelize.sync({ force: false })
.then(() => {
console.log('database connection complete');
database.addKeyword("rtx3080", "phobyjun")
database.addKeyword("rtx3060", "junseok")
database.getKeywordsByUserId("junseok")
})
.catch((err) => {
console.log('database connection failed');
......
......@@ -38,7 +38,7 @@ db.user = require("./user")(sequelize, Sequelize);
db.keyword = require("./keyword")(sequelize, Sequelize);
db.userKeyword = sequelize.define('user_keywords');
db.user.belongsToMany(db.keyword, {through: db.userKeyword});
db.keyword.belongsToMany(db.user, {through: db.userKeyword});
db.user.belongsToMany(db.keyword, {through: db.userKeyword, as: 'keyword'});
db.keyword.belongsToMany(db.user, {through: db.userKeyword, as: 'user'});
module.exports = db;
......