-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
324 lines (304 loc) · 12.9 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
const express = require('express');
const app = express();
const cors = require('cors');
const sql = require('mssql');
const jwt = require('jsonwebtoken');
const { config, port, key, status } = require('./config');
const pool = new sql.ConnectionPool(config);
const connection = pool.connect();
const authenticateToken = async (req, res, next) => {
try {
const token = req.headers.authorization;
if (token === undefined) {
res.send({ StatusCode: status.PreconditionFailed, Message: 'Precondition Failed', Data: null });
}
jwt.verify(token, key, async (error, decode) => {
if (error) {
console.log(error);
res.send({ StatusCode: status.Unauthorized, Message: 'Unauthorized', Data: null });
}
else {
await connection;
const data = await pool.query(`select [Token] from [ArticleDB].[dbo].[Token] where [User_Id]=${decode.Id}`);
if ((data.recordset.length === 1) && (data.recordset[0].Token === token)) {
req.params.token = decode;
}
else {
res.send({ StatusCode: status.Forbidden, Message: 'Forbidden', Data: null });
return;
}
next();
}
});
}
catch (error) {
console.log(error);
res.send({ StatusCode: status.SystemError, Message: error, Data: null });
}
};
app.use(cors());
app.use(express.json());
app.get('/articleId', async (req, res) => {
try {
await connection;
let articleId = [];
const data = await pool.query('select [Articles].[Id] from [ArticleDB].[dbo].[Articles]');
data.recordset.forEach((article) => articleId.push(article.Id));
res.send({ StatusCode: status.OK, Message: 'OK', Data: articleId });
}
catch (error) {
console.log(error);
res.send({ StatusCode: status.SystemError, Message: error, Data: null });
}
});
app.get('/personalArticleId', authenticateToken, async (req, res) => {
try {
await connection;
let articleId = [];
const token = req.params.token;
if (token) {
const data = await pool.query(`select [Articles].[Id] from [ArticleDB].[dbo].[Articles] where [Articles].[User_Id]=${token.Id}`);
data.recordset.forEach((article) => articleId.push(article.Id));
res.send({ StatusCode: status.OK, Message: 'OK', Data: articleId });
}
}
catch (error) {
console.log(error);
res.send({ StatusCode: status.SystemError, Message: error, Data: null });
}
});
app.get('/list', async (req, res) => {
try {
await connection;
let list = [];
let articles = [];
if(req.query.list !== undefined){
if((typeof req.query.list) === 'string'){
list.push(Number(req.query.list));
}
else{
req.query.list.forEach((index) => list.push(Number(index)));
}
for (let id of list) {
const data = await pool.query(`select [Articles].[Id], [User].[Name], [Articles].[Title], [Articles].[CreateDatetime] from [ArticleDB].[dbo].[Articles] inner join [ArticleDB].[dbo].[User] on [Articles].[User_Id]=[User].[Id] where [Articles].[Id] = ${id}`);
if (data.recordset.length === 1) {
articles.push(data.recordset[0]);
}
else {
res.send({ StatusCode: status.NotFound, Message: 'Not Found', Data: null });
}
}
res.send({ StatusCode: status.OK, Message: 'OK', Data: articles });
}
else {
res.send({ StatusCode: status.BadRequest, Message: 'Bad Request', Data: null });
}
}
catch (error) {
console.log(error);
res.send({ StatusCode: status.SystemError, Message: error, Data: null });
}
});
app.get('/detail/:id', async (req, res) => {
try {
await connection;
const id = req.params.id;
const data = await pool.query(`select [Articles].[Id], [Articles].[Title], [Articles].[User_Id], [User].[Name], [Articles].[Content], [Articles].[CreateDatetime], [Articles].[UpdateDatetime], [Articles].[Editor] from [ArticleDB].[dbo].[Articles] inner join [ArticleDB].[dbo].[User] on [Articles].[User_Id]=[User].[Id] where [Articles].[Id]=${id}`);
if (data.recordset.length === 1) {
res.send({ StatusCode: status.OK, Message: 'OK', Data: data.recordset[0] });
}
else {
res.send({ StatusCode: status.NotFound, Message: 'Not Found', Data: null });
}
}
catch (error) {
console.log(error);
res.send({ StatusCode: status.SystemError, Message: error, Data: null });
}
});
app.put('/detail/:id', authenticateToken, async (req, res) => {
try {
await connection;
const article = req.body;
const token = req.params.token;
const id = Number(req.params.id);
if (id === article.Id) {
const data = await pool.query(`select [Id] from [ArticleDB].[dbo].[Articles] where [Id]=${id} and [User_Id]=${token.Id}`);
if ((data.recordset.length === 1) || (token.Status === 2)) {
if ((article.Title !== '') && (article.Content !== '')) {
await pool.query(`update [ArticleDB].[dbo].[Articles] set [Title]='${article.Title}', [Content]='${article.Content}', [UpdateDatetime]=GETUTCDATE(), [Editor]='${token.Id}' where [Id]=${article.Id}`);
res.send({ StatusCode: status.OK, Message: 'OK', Data: null });
}
else {
res.send({ StatusCode: status.BadRequest, Message: 'Bad Request', Data: null });
}
}
else {
res.send({ StatusCode: status.Forbidden, Message: 'Forbidden', Data: null });
}
}
else {
res.send({ StatusCode: status.NotFound, Message: 'Not Found', Data: null });
}
}
catch (error) {
console.log(error);
res.send({ StatusCode: status.SystemError, Message: error, Data: null });
}
});
app.delete('/:id', authenticateToken, async (req, res) => {
try {
await connection;
const id = req.params.id;
const token = req.params.token;
const data = await pool.query(`select [Id] from [ArticleDB].[dbo].[Articles] where [Id]=${id} and [User_Id]=${token.Id}`);
if ((data.recordset.length === 1) || (token.Status === 2)) {
const check = await pool.query(`delete [ArticleDB].[dbo].[Articles] where [Id]=${id}`);
if (check.rowsAffected[0] === 1) {
res.send({ StatusCode: status.OK, Message: 'OK', Data: null });
}
else {
res.send({ StatusCode: status.NotFound, Message: 'Not Found', Data: null });
}
}
else {
res.send({ StatusCode: status.Forbidden, Message: 'Forbidden', Data: null });
}
}
catch (error) {
console.log(error);
res.send({ StatusCode: status.SystemError, Message: error, Data: null });
}
});
app.post('/article', authenticateToken, async (req, res) => {
try {
await connection;
const article = req.body;
if ((req.body !== undefined) && (article.Title !== '') && (article.User_Id !== 0) && (article.Content !== '')) {
data = await pool.query(`insert into [ArticleDB].[dbo].[Articles] ([Title], [User_Id], [Content], [Editor]) values ('${article.Title}', '${article.User_Id}', '${article.Content}', '${article.User_Id}')`);
res.send({ StatusCode: status.OK, Message: 'OK', Data: null });
}
else {
res.send({ StatusCode: status.BadRequest, Message: 'Bad Request', Data: article });
}
}
catch (error) {
console.log(error);
res.send({ StatusCode: status.SystemError, Message: error, Data: null });
}
});
app.get('/search', async (req, res) => {
try {
await connection;
const { title, author, fromDate, toDate } = req.query;
let articleId = [];
let option = [];
let name = false;
let string = '';
title !== '' ? option.push(`[Title] like '%${title}%'`) : title;
if (author !== '') {
const data = await pool.query(`select [User].[Id] from [ArticleDB].[dbo].[User] where [User].[Name]='${author}'`);
if (data.recordset.length === 1) {
name = true;
option.push(`[User_Id] = ${data.recordset[0].Id}`);
}
}
if((fromDate !== '') || (toDate !== '')){
fromDate !== '' ? option.push(`[CreateDatetime] >= '${fromDate}'`) : fromDate;
toDate !== '' ? option.push(`[CreateDatetime] <= '${toDate}'`) : toDate;
}
option.forEach((item, index) => {
if (index === 0) {
string = string.concat(' where ',item);
}
else{
string = string.concat(' and ', item);
}
});
if(string !== ''){
const data = await pool.query('select [Articles].[Id] from [ArticleDB].[dbo].[Articles]' + string);
if(data.recordset.length === 0){
res.send({ StatusCode: status.NotFound, Message: 'Not Found', Data: [] });
}
else{
data.recordset.forEach((article) => articleId.push(article.Id));
res.send({ StatusCode: status.OK, Message: 'OK', Data: articleId });
}
}
else if(!name){
res.send({ StatusCode: status.NotFound, Message: 'Not Found', Data: [] });
}
else{
res.send({ StatusCode: status.BadRequest, Message: 'Bad Request', Data: [] });
}
}
catch (error) {
console.log(error);
res.send({ StatusCode: status.SystemError, Message: error, Data: null });
}
});
app.get('/search/:title', async (req, res) => {
try {
await connection;
const title = req.params.title;
const data = await pool.query(`select [Id], [Title] from [ArticleDB].[dbo].[Articles] where [Title] like '%${title}%'`);
res.send({ StatusCode: status.OK, Message: 'OK', Data: data.recordset });
}
catch (error) {
console.log(error);
res.send({ StatusCode: status.SystemError, Message: error, Data: null });
}
});
app.post('/login', async (req, res) => {
try {
await connection;
const username = req.body.UserName;
const password = req.body.Password;
if ((req.body !== undefined) && (username !== '') && (password !== '')) {
const data = await pool.query(`select [Id], [Name], [Status] from [ArticleDB].[dbo].[User] where [Name]='${username}' and [Password]='${password}'`);
if (data.recordset.length === 1) {
const token = jwt.sign(data.recordset[0], key, { expiresIn: '1h' });
await pool.query(`update [ArticleDB].[dbo].[Token] set [Token]='${token}', [UpdateDatetime]=GETUTCDATE() where [User_Id]='${data.recordset[0].Id}'`);
res.send({ StatusCode: status.OK, Message: 'OK', Data: token });
}
else {
res.send({ StatusCode: status.NotFound, Message: 'Not Found', Data: null });
}
}
else {
res.send({ StatusCode: status.BadRequest, Message: 'Bad Request', Data: null });
}
}
catch (error) {
console.log(error);
res.send({ StatusCode: status.SystemError, Message: error, Data: null });
}
});
app.post('/sign', async (req, res) => {
try {
await connection;
const username = req.body.UserName;
const password = req.body.Password;
if ((req.body !== undefined) && (username !== '') && (password !== '')) {
const data = await pool.query(`select [Id] from [ArticleDB].[dbo].[User] where [Name]='${username}'`);
if (data.recordset.length === 1) {
res.send({ StatusCode: status.NotFound, Message: 'Not Found', Data: null });
}
else {
const user = await pool.query(`insert into [ArticleDB].[dbo].[User] ([Name], [Password], [Status]) values('${username}', '${password}', '1') select SCOPE_IDENTITY() as Id`);
if (user.recordset.length === 1) {
await pool.query(`insert into [ArticleDB].[dbo].[Token] ([User_Id], [Token]) values('${user.recordset[0].Id}', '')`);
res.send({ StatusCode: status.OK, Message: 'OK', Data: null });
}
}
}
else {
res.send({ StatusCode: status.BadRequest, Message: 'Bad Request', Data: null });
}
}
catch (error) {
console.log(error);
res.send({ StatusCode: status.SystemError, Message: error, Data: {} });
}
});
app.listen(port, () => console.log(`Article Backend listening on port ${port}`));