유명현

Merge branch 'main' into feature/line-bot-v2

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 +}
30 +
31 +database.deleteKeyword = async function(userId, keyword) {
32 +
33 + const u = await User.findOrCreate({
34 + where: {
35 + userId: userId
36 + }
37 + })
38 +
39 + const k = await Keyword.findOrCreate({
40 + where: {
41 + keyword: keyword
42 + }
43 + })
44 +
45 + await UserKeyword.destroy({
46 + where: {
47 + userId: u[0].id,
48 + keywordId: k[0].id
49 + }
50 + })
51 +}
52 +
53 +database.getKeywordsByUserId = async function(userId) {
54 +
55 + const keywords = await Keyword.findAll({
56 + attributes: ['keyword'],
57 + where: {
58 + '$user.userId$': userId
59 + },
60 + include: [{
61 + attributes: [],
62 + model: User,
63 + as: 'user'
64 + }],
65 + raw: true
66 + })
67 +
68 + let result = []
69 + for (let i = 0; i < keywords.length; i++) {
70 + result.push(keywords[i].keyword)
71 + }
72 +
73 + return result
74 +}
75 +
76 +database.getUsersByKeyword = async function(keyword) {
77 +
78 + const users = await User.findAll({
79 + attributes: ['userId'],
80 + where: {
81 + '$keyword.keyword$': keyword
82 + },
83 + include: [{
84 + attributes: [],
85 + model: Keyword,
86 + as: 'keyword'
87 + }],
88 + raw: true
89 + })
90 +
91 + let result = []
92 + for (let i = 0; i < users.length; i++) {
93 + result.push(users[i].userId)
94 + }
95 +
96 + return result
97 +}
98 +
99 +database.getAllUsers = async function() {
100 +
101 + const users = await User.findAll({
102 + raw: true
103 + })
104 +
105 + let result = []
106 + for (let i = 0; i < users.length; i++) {
107 + result.push(users[i].userId)
108 + }
109 +
110 + return result
111 +}
112 +
113 +database.getAllKeywords = async function() {
114 +
115 + const keywords = await Keyword.findAll({
116 + raw: true
117 + })
118 +
119 + let result = []
120 + for (let i = 0; i< keywords.length; i++) {
121 + result.push(keywords[i].keyword)
122 + }
123 +
124 + console.log(result)
125 + return result
126 +}
...\ No newline at end of file ...\ No newline at end of file
...@@ -3,8 +3,36 @@ const line = require("@line/bot-sdk"); ...@@ -3,8 +3,36 @@ const line = require("@line/bot-sdk");
3 const fs = require("fs"); 3 const fs = require("fs");
4 const { handleEvent } = require("./chatbot/index"); 4 const { handleEvent } = require("./chatbot/index");
5 const { config } = require("./chatbot/index"); 5 const { config } = require("./chatbot/index");
6 +const { sequelize } = require("./models");
7 +const database = require("./apis/database");
6 8
9 +// Initialize DB connection
10 +sequelize
11 + .sync({ force: false })
12 + .then(() => {
13 + console.log("database connection complete");
14 + database.addKeyword("rtx3060", "junseok");
15 + database.getKeywordsByUserId("junseok");
16 + database.deleteKeyword("phobyjun", "rtx3080");
17 + database.getAllUsers();
18 + database.getUsersByKeyword("rtx3060");
19 + database.getAllKeywords();
20 + })
21 + .catch((err) => {
22 + console.log("database connection failed");
23 + });
24 +
25 +// Load .env configuration
26 +require("dotenv").config();
27 +const config = {
28 + channelAccessToken: process.env.channelAccessToken,
29 + channelSecret: process.env.channelSecret,
30 +};
31 +
32 +// Express app server initialization
7 const app = express(); 33 const app = express();
34 +
35 +// Create post request handler for chatbot
8 app.post("/webhook", line.middleware(config), (req, res) => { 36 app.post("/webhook", line.middleware(config), (req, res) => {
9 Promise.all(req.body.events.map(handleEvent)).then((result) => { 37 Promise.all(req.body.events.map(handleEvent)).then((result) => {
10 res.json(result); 38 res.json(result);
......
1 +{
2 + "development": {
3 + "username": "root",
4 + "password": "mamuri",
5 + "database": "mamuri_db",
6 + "host": "127.0.0.1",
7 + "dialect": "mysql"
8 + },
9 + "test": {
10 + "username": "root",
11 + "password": null,
12 + "database": "database_test",
13 + "host": "127.0.0.1",
14 + "dialect": "mysql"
15 + },
16 + "production": {
17 + "username": "root",
18 + "password": null,
19 + "database": "database_production",
20 + "host": "127.0.0.1",
21 + "dialect": "mysql"
22 + }
23 +}
1 +'use strict';
2 +
3 +const fs = require('fs');
4 +const path = require('path');
5 +const Sequelize = require('sequelize');
6 +const basename = path.basename(__filename);
7 +const env = process.env.NODE_ENV || 'development';
8 +const config = require(__dirname + '/../config/config.json')[env];
9 +const db = {};
10 +
11 +let sequelize;
12 +if (config.use_env_variable) {
13 + sequelize = new Sequelize(process.env[config.use_env_variable], config);
14 +} else {
15 + sequelize = new Sequelize(config.database, config.username, config.password, config);
16 +}
17 +
18 +fs
19 + .readdirSync(__dirname)
20 + .filter(file => {
21 + return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js');
22 + })
23 + .forEach(file => {
24 + const model = require(path.join(__dirname, file))(sequelize, Sequelize.DataTypes);
25 + db[model.name] = model;
26 + });
27 +
28 +Object.keys(db).forEach(modelName => {
29 + if (db[modelName].associate) {
30 + db[modelName].associate(db);
31 + }
32 +});
33 +
34 +db.sequelize = sequelize;
35 +db.Sequelize = Sequelize;
36 +
37 +db.user = require("./user")(sequelize, Sequelize);
38 +db.keyword = require("./keyword")(sequelize, Sequelize);
39 +db.userKeyword = sequelize.define('user_keywords');
40 +
41 +db.user.belongsToMany(db.keyword, {through: db.userKeyword, as: 'keyword'});
42 +db.keyword.belongsToMany(db.user, {through: db.userKeyword, as: 'user'});
43 +
44 +module.exports = db;
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
1 +module.exports = (sequelize, DataTypes) => {
2 +
3 + return sequelize.define("user", {
4 + id: {
5 + type: DataTypes.INTEGER,
6 + autoIncrement: true,
7 + primaryKey: true
8 + },
9 + userId: {
10 + type: DataTypes.STRING,
11 + allowNull: false
12 + }
13 + })
14 +}
...\ No newline at end of file ...\ No newline at end of file
This diff is collapsed. Click to expand it.
...@@ -19,6 +19,8 @@ ...@@ -19,6 +19,8 @@
19 "axios": "^0.27.2", 19 "axios": "^0.27.2",
20 "dotenv": "^16.0.1", 20 "dotenv": "^16.0.1",
21 "express": "^4.18.1", 21 "express": "^4.18.1",
22 - "nodemon": "^2.0.16" 22 + "mysql2": "^2.3.3",
23 + "nodemon": "^2.0.16",
24 + "sequelize": "^6.20.0"
23 } 25 }
24 } 26 }
......