-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboard.js
More file actions
315 lines (275 loc) · 8.51 KB
/
board.js
File metadata and controls
315 lines (275 loc) · 8.51 KB
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
'use strict';
const board = require('../services/board');
const { Op } = require('sequelize');
const { success, fail } = require('../functions/responseStatus');
const { createSearchQuery } = require('../functions/query');
/**
* page, limit 값을 받아, 해당하는 페이지의 게시글을 조회한다.
* 검색어가 있을 경우, 검색어에 해당하는 게시글을 조회한다. (없을 경우, 모든 게시글을 조회)
* 검색어: 제목, 내용, 작성자로 검색할 수 있다.
*
* limit 값이 5, 10, 20이 아닐 경우, 5로 설정하고, 에러 메시지를 반환한다.
* page 값이 1000 이상일 경우, 1로 설정하고, 에러 메시지를 반환한다.
*
* DB에서 조회한 글의 수보다 page * limit 값이 클 경우, page 값을 마지막 페이지로 설정한다.
*
* @returns {Object} 게시글 정보
*/
const boardGet = async (req, res) => {
let { page, limit, searchType, searchText } = req.query;
page = parseInt(page);
limit = parseInt(limit);
let where_content = null,
where_user = null;
const rendering = (res, posts, message, currentPage = 1, maxPage = 1, limit = 5) => {
return res.render('post/index', {
posts: posts,
currentPage: currentPage,
maxPage: maxPage,
limit: limit,
searchType: searchType,
searchText: searchText,
error: message,
});
};
page = !isNaN(page) ? page : 1;
if (page >= 1000) {
return rendering(res, [], 'Page can only be a number less than 1000.');
}
limit = !isNaN(limit) ? limit : 10;
if ([5, 10, 20].indexOf(limit) === -1) {
return rendering(res, [], 'Limit can only be 5, 10, 20.');
}
try {
let searchQuery = await createSearchQuery(req.query);
let key = searchQuery.length > 0 ? Object.keys(searchQuery[0]) : undefined;
if (searchQuery.length === 2) {
where_content = {
[Op.or]: [
{ title: { [Op.like]: `%${searchQuery[0].title}%` } },
{ content: { [Op.like]: `%${searchQuery[1].body}%` } },
],
};
} else if (searchQuery.length === 1) {
switch (key[0]) {
case 'title':
where_content = { title: { [Op.like]: `%${searchQuery[0].title}%` } };
break;
case 'body':
where_content = { content: { [Op.like]: `%${searchQuery[0].body}%` } };
break;
case 'user_name':
where_user = { user_name: searchQuery[0].user_name };
break;
}
}
const post_count = await board.countPost();
if (page * limit > post_count) page = post_count / limit; // 마지막 페이지
if(!Number.isInteger(page)) page = parseInt(page) + 1;
await board.getBoard(where_user, where_content, limit, page).then((data) => {
return rendering(res, data.rows, null, page, Math.ceil(data.count / Math.max(1, limit)), limit);
});
} catch (err) {
return fail(res, 500, err.message);
}
};
/**
* post_id에 해당하는 게시글을 조회하고, 조회수를 1 증가시킨다.
*
* @returns {Object} 게시글 정보
*/
const boardGetByPostId = async (req, res) => {
const { id: post_id } = req.params;
try {
let data = await board.searchByPostId(post_id);
let comments = await board.searchCommentByPostId(post_id, 5, 1);
res.render('post/read', { post: data, count: comments.count, comments: comments.rows, more: comments.more });
} catch (err) {
if (err.message === 'No data.') {
return fail(res, 404, err.message);
} else {
return fail(res, 500, err.message);
}
}
};
/**
* 유저로부터, 게시글의 제목과 내용을 받아 글을 생성한다.
*/
const boardPost = (req, res) => {
const { title, content } = req.body;
const user_id = req.decoded.id;
try {
board.postBoard(title, content, user_id).then(() => {
return success(res, 201, 'Post created success.');
});
} catch (err) {
return fail(res, 500, err.message);
}
};
/**
* 유저로부터, 게시글의 제목과 내용을 받아 글을 수정한다.
*/
const boardUpdateByPostId = async (req, res) => {
const { title, content } = req.body;
const { id: post_id } = req.params;
const user_id = req.decoded.id;
try {
let is_authorized = await board.authCheckPost(user_id, post_id);
if (is_authorized) {
await board.updatePost(title, content, post_id);
return success(res, 200, 'Post updated success.');
} else {
return fail(res, 403, 'You are not authorized to update this post.');
}
} catch (err) {
return fail(res, 500, err.message);
}
};
/**
* 해당하는 id의 게시글을 삭제한다.
*/
const boardDeleteByPostId = async (req, res) => {
const { id: post_id } = req.params;
const user_id = req.decoded.id;
try {
let is_authorized = await board.authCheckPost(user_id, post_id);
if (is_authorized) {
await board.deletePost(post_id);
return success(res, 200, 'Post deleted success.');
} else {
return fail(res, 403, 'You are not authorized to delete this post.');
}
} catch (err) {
return fail(res, 500, err.message);
}
};
/**
* 게시글에 대한 추천을 한다. (추천 O -> 추천 X) (추천 X -> 추천 O)
*/
const boardRecommand = async (req, res) => {
let user_id = req.decoded.id;
let post_id = req.params.id;
try {
const result = await board.recommandBoard(user_id, post_id);
return success(res, 200, result.message, result.data);
} catch (err) {
return fail(res, 500, err.message);
}
};
/**
* 유저로부터, 댓글 내용을 받아 생성한다.
*/
const boardCommentPost = async (req, res) => {
const { comment } = req.body;
const user_id = req.decoded.id;
let post_id = req.params.id;
try {
await board.commentPost(comment, user_id, post_id);
return success(res, 201, 'Comment created success.');
} catch (err) {
return fail(res, 500, err.message);
}
};
/**
* 댓글 id와 유저 id를 받아서 댓글을 삭제한다.
*/
const boardCommentDelete = async (req, res) => {
const comment_id = req.params.comment_id;
const user_id = req.decoded.id;
try {
await board.commentDelete(comment_id, user_id);
return success(res, 200, 'Comment deleted success.');
} catch (err) {
if(err.message === 'unauthorized') {
return fail(res, 401, err.message);
} else {
return fail(res, 500, err.message);
}
}
};
/**
* 댓글 더보기
*/
const boardCommentMore = async (req, res) => {
const { id: post_id, comment_page: comment_page } = req.params;
try {
const comments = await board.searchCommentByPostId(post_id, 5, comment_page);
return success(res, 200, 'Bringing up comments success.', comments);
} catch (err) {
if (err.message === 'Page can only be a number less than 1000.') {
return fail(res, 400, err.message);
} else {
return fail(res, 500, err.message);
}
}
};
/**
* 게시글 작성자인지 확인한다. (작성자일 경우, 200, 작성자가 아닐 경우, 401)
*/
const postAuthCheck = async (req, res) => {
let user_id = req.decoded.id;
let post_id = req.params.id;
try {
let is_authorized = await board.authCheckPost(user_id, post_id);
if (is_authorized) {
return success(res, 200, 'authorized');
} else {
return success(res, 401, 'unauthorized');
}
} catch (err) {
return fail(res, 500, err.message);
}
};
/**
* 해당 유저의 게시글 추천 여부를 확인한다. (추천 O, 200, 추천 X, 200)
*/
const boardRecommandCheck = (req, res) => {
let user_id = req.decoded.id;
let post_id = req.params.id;
try {
board.recommandCheckBoard(user_id, post_id).then((data) => {
if (data !== null) {
// 추천 O
return success(res, 200, 'created');
} else {
// 추천 X
return success(res, 200, 'deleted');
}
});
} catch (err) {
return fail(res, 500, err.message);
}
};
/**
* 게시글 작성 페이지를 렌더링한다.
*/
const postView = (req, res) => {
res.render('post/create');
};
/**
* 게시글 수정 페이지를 렌더링하면서, 해당 게시글의 정보를 함께 전달한다.
*/
const updateViewByPostId = async (req, res) => {
const { id: post_id } = req.params;
try {
let data = await board.searchByPostId(post_id);
res.render('post/update', { post: data });
} catch (err) {
return fail(res, 500, err.message);
}
};
module.exports = {
boardGet,
boardGetByPostId,
boardPost,
boardUpdateByPostId,
boardDeleteByPostId,
boardRecommand,
boardCommentPost,
boardCommentDelete,
boardCommentMore,
postAuthCheck,
boardRecommandCheck,
postView,
updateViewByPostId,
};