-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
201 lines (181 loc) · 6.5 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
require('dotenv').config()
const mysql = require('mysql2')
// Create the connection to the database
const connection = mysql.createConnection(process.env.DATABASE_URL)
const express = require('express')
const app = express()
app.use(express.json())
const port = 3000;
const cors = require('cors');
app.use(cors());
// api health check
app.get('/health', (req, res) => {
res.status(200).send('OK');
});
// 사용자 추가
app.post('/users/add', (req, res) => {
const { name, generation } = req.body;
// 사용자 추가
connection.query('INSERT INTO UserInfo (name, generation) VALUES (?, ?)', [name, generation], (err, results) => {
if (err) {
return res.status(500).json({ error: err.message });
}
// 새로 생성된 사용자의 ID 가져오기
connection.query('SELECT LAST_INSERT_ID() as id', (err, results) => {
if (err) {
return res.status(500).json({ error: err.message });
}
const id = results[0].id;
res.json({ message: 'User added successfully', id: id });
});
});
});
// 사용자 목록과 메시지 개수 조회
app.get('/users', (req, res) => {
connection.query('SELECT * FROM UserInfo', (err, results) => {
if (err) {
return res.status(500).json({ error: err.message });
}
res.json(results);
});
});
//id로 사용자 조회
app.get('/users/id', (req, res) => {
const { name, generation } = req.query;
connection.query(
'SELECT * FROM UserInfo WHERE name = ? AND generation = ?',
[name, generation],
(err, results) => {
if (err) {
return res.status(500).json({ error: err.message });
}
if (results.length > 0) {
res.json(results[0]);
} else {
res.status(404).json({ message: 'User not found' });
}
}
);
});
// 메시지 추가
app.post('/messages/add', (req, res) => {
const { user_id, message, password } = req.body;
connection.query('INSERT INTO UserMessages (user_id, message, password) VALUES (?, ?, ?)',
[user_id, message, password], (err) => {
if (err) {
return res.status(500).json({ error: err.message });
}
res.json({ message: 'Message added successfully' });
});
});
// 메시지 삭제
app.delete('/messages/delete', (req, res) => {
const { user_id, message, password } = req.body;
connection.query(
'SELECT password FROM UserMessages WHERE user_id = ? AND message = ?',
[user_id, message],
(err, results) => {
if (err) {
return res.status(500).json({ error: err.message });
}
if (results.length > 0) {
const dbPassword = results[0].password;
if (dbPassword === password) {
// 비밀번호가 일치하면 메시지 삭제
connection.query(
'DELETE FROM UserMessages WHERE user_id = ? AND message = ?',
[user_id, message],
(deleteErr) => {
if (deleteErr) {
return res.status(500).json({ error: deleteErr.message });
}
res.json({ message: 'Message deleted successfully' });
}
);
} else {
res.status(401).json({ error: 'Incorrect password' });
}
} else {
res.status(404).json({ message: 'Message not found' });
}
}
);
});
//메시지 수정
app.put('/messages/adjust', (req, res) => {
const { user_id, oldMessage, newMessage, password } = req.body;
connection.query(
'SELECT password FROM UserMessages WHERE user_id = ? AND message = ?',
[user_id, oldMessage],
(err, results) => {
if (err) {
return res.status(500).json({ error: err.message });
}
if (results.length > 0) {
const dbPassword = results[0].password;
if (dbPassword === password) {
// 비밀번호가 일치하면 메시지 수정
connection.query(
'UPDATE UserMessages SET message = ? WHERE user_id = ? AND message = ?',
[newMessage, user_id, oldMessage],
(updateErr) => {
if (updateErr) {
return res.status(500).json({ error: updateErr.message });
}
res.json({ message: 'Message updated successfully' });
}
);
} else {
res.status(401).json({ error: 'Incorrect password' });
}
} else {
res.status(404).json({ message: 'Message not found' });
}
}
);
});
app.get('/messages', (req, res) => {
const query = `
SELECT
UserMessages.user_id,
UserInfo.name,
UserInfo.generation AS number,
UserMessages.message,
UserMessages.password
FROM
UserMessages
INNER JOIN
UserInfo ON UserMessages.user_id = UserInfo.id
`;
connection.query(query, (err, results) => {
if (err) {
return res.status(500).json({ error: err.message });
}
res.json(results);
});
});
// 활성화
app.put('/messages/activate', (req, res) => {
const { user_id, message } = req.body;
connection.query('UPDATE UserMessages SET active = 1 WHERE user_id = ? AND message = ?',
[user_id, message], (err) => {
if (err) {
return res.status(500).json({ error: err.message });
}
res.json({ message: 'Message activated successfully' });
});
});
// 비활성화
app.put('/messages/deactivate', (req, res) => {
const { user_id, message } = req.body;
connection.query('UPDATE UserMessages SET active = 0 WHERE user_id = ? AND message = ?',
[user_id, message], (err) => {
if (err) {
return res.status(500).json({ error: err.message });
}
res.json({ message: 'Message deactivated successfully' });
});
});
app.listen(port, () => {
console.log(`Server running on http://127.0.0.1:${port}`);
});