forked from Cheese-Guardians-Opensource/cheese
-
Notifications
You must be signed in to change notification settings - Fork 2
/
communityController.js
327 lines (299 loc) · 10.8 KB
/
communityController.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
324
325
326
327
const communityService = require('../services/communityService');
const jwt = require('jsonwebtoken');
//const secret = require('../config/secret');
const baseResponse = require("../config/baseResponseStatus");
const path = require('path');
const querystring = require('querystring');
require('dotenv').config();
//게시글 세부 조회 + 댓글 조회
exports.getCommunity = async function (req, res) {
const token = req.cookies.x_auth;
if(token) {
const decodedToken = jwt.verify(token, process.env.jwtsecret);
const user_id = decodedToken.user_id; // user_id를 추출
// validation
if(!user_id) {
return res.send(errResponse(baseResponse.USER_USERIDX_EMPTY));
}
if (user_id <= 0) {
return res.send(errResponse(baseResponse.USER_USERIDX_LENGTH));
}
const board_id = req.params.board_id;
const title = req.params.title;
const communityResult = await communityService.retrieveCommunity(board_id, title);
const commentResult = await communityService.retrieveComment(board_id, title);
const myPostResult = await communityService.retriveMyPost(user_id);
//자신의 게시물 개수 제한 7개
const limitedPosts = myPostResult.slice(0, 7);
const otherPostResult = await communityService.retrieveOtherPost(user_id, board_id, title);
//다른 사람의 게시물 개수 제한 13개
const limitedOtherPosts = otherPostResult.slice(0, 13);
console.log(communityResult);
// Combine communityResult and commentResult as needed before rendering the view
const combinedData = {
communityResult: communityResult,
commentResult: commentResult,
myPostResult: limitedPosts,
otherPostResult: limitedOtherPosts,
};
await communityService.updateViewsCount(board_id);
console.log("combindedData",combinedData);
//console.log(communityResult.title);
return res.render('community/commun_view.ejs', combinedData);
}
else {
return res.redirect('/');
}
}
exports.getWrite = async function (req, res) {
const token = req.cookies.x_auth;
if(token) {
const decodedToken = jwt.verify(token, process.env.jwtsecret);// 토큰 검증, 복호화
const user_id = decodedToken.user_id; // user_id를 추출
const board_id = req.params.board_id;
const title = req.params.title;
// validation
if(!user_id) {
return res.send(errResponse(baseResponse.USER_USERIDX_EMPTY));
}
if (user_id <= 0) {
return res.send(errResponse(baseResponse.USER_USERIDX_LENGTH));
}
//나의 게시물
const myPostResult = await communityService.retriveMyPost(user_id);
const limitedPosts = myPostResult.slice(0, 7);
//다른 사람 게시물
const otherPostResult = await communityService.retrieveOtherPost(user_id, board_id, title);
const limitedOtherPosts = otherPostResult.slice(0, 13);
const combinedData = {
myPostResult: limitedPosts,
otherPostResult: limitedOtherPosts
};
//console.log("combindedData",combinedData);
//console.log(communityResult.title);
// 화면 크기에 따라 적절한 템플릿 파일 렌더링
const userAgent = req.headers['user-agent'];
if (userAgent.includes('Mobile')) {
// 모바일 화면일 경우 mobile.ejs 렌더링
return res.render('community/mobile_commun_write.ejs', combinedData);
} else {
// 데스크탑 화면일 경우 desktop.ejs 렌더링
return res.render('community/commun_write.ejs', combinedData);
}
}
else {
return res.redirect('/');
}
}
//게시글 고민상담소 리스트 조회
exports.getWorryList = async function (req, res) {
const token = req.cookies.x_auth;
if (token) {
try {
const decodedToken = jwt.verify(token, process.env.jwtsecret);
const user_id = decodedToken.user_id;
if (!user_id) {
return res.send(baseResponse.USER_USERIDX_EMPTY);
}
if (user_id <= 0) {
return res.send(baseResponse.USER_USERIDX_LENGTH);
}
if (!req.query.page){
const existingQueryString = req.query;
if (Object.keys(existingQueryString).length === 0) {
const newURL = `${req.protocol}://${req.get('host')}${req.originalUrl}?page=1&page1=1`;
return res.redirect(newURL);
}
}
let page = req.query.page;
let page1 = req.query.page1;
const communityDataResult = await communityService.retrieveWorryCommunity(
user_id,
page
);
console.log(communityDataResult);
const communityMyDataResult = await communityService.retrieveMyWorryCommunity(
user_id,
page1
);
console.log(communityMyDataResult);
const combinedData = {
communityDataResult: communityDataResult,
communityMyDataResult: communityMyDataResult
};
return res.render('community/community1.ejs', combinedData);
} catch (err) {
return res.send('Error occurred during token verification or community retrieval.');
}
} else {
return res.redirect('/');
}
};
//게시글 정보공유 리스트 조회
exports.getInfoList = async function (req, res) {
const token = req.cookies.x_auth;
if (token) {
try {
const decodedToken = jwt.verify(token, process.env.jwtsecret);
const user_id = decodedToken.user_id;
if (!user_id) {
return res.send(baseResponse.USER_USERIDX_EMPTY);
}
if (user_id <= 0) {
return res.send(baseResponse.USER_USERIDX_LENGTH);
}
if (!req.query.page){
const existingQueryString = req.query;
if (Object.keys(existingQueryString).length === 0) {
const newURL = `${req.protocol}://${req.get('host')}${req.originalUrl}?page=1&page1=1`;
return res.redirect(newURL);
}
}
let page = req.query.page;
let page1 = req.query.page1;
const communityDataResult = await communityService.retrieveInfoCommunity(
user_id,
page
);
console.log(communityDataResult);
const communityMyDataResult = await communityService.retrieveMyInfoCommunity(
user_id,
page1
);
console.log(communityMyDataResult);
const combinedData = {
communityDataResult: communityDataResult,
communityMyDataResult: communityMyDataResult
};
return res.render('community/community2.ejs', combinedData);
} catch (err) {
return res.send('Error occurred during token verification or community retrieval.');
}
} else {
return res.redirect('/');
}
};
//side 게시글 조회 (다른 게시글 보기)
exports.getComment = async function (req, res) {
const board_id = req.params.board_id;
const title = req.params.title;
const commentResult = await communityService.retrieveSide(board_id, title);
console.log(commentResult);
//console.log(communityResult.title);
return res.render('community/side.ejs', { commentResult: commentResult});
};
//게시글 작성
exports.postBoard = async function (req, res) {
const token = req.cookies.x_auth;
if (token) {
const decodedToken = jwt.verify(token, process.env.jwtsecret); // 토큰 검증, 복호화
const user_id = decodedToken.user_id; // user_id를 추출
// console.log(req.body);
var updated_at = new Date();
console.log(updated_at);
//validation
if(!user_id) {
return res.send(errResponse(baseResponse.USER_USERIDX_EMPTY));
}
if (user_id <= 0) {
return res.send(errResponse(baseResponse.USER_USERIDX_LENGTH));
}
const {
category_name,
title,
content
} = req.body;
// console.log(req.body.content);
const createCommunResponse = await communityService.createBoard(
category_name,
user_id,
title,
content,
updated_at
);
if (createCommunResponse == "성공") {
if (category_name == "정보게시판"){
return res.status(200).send(`
<script>
if (confirm('게시글 등록에 성공했습니다.')) {
window.location.href = "/community/infoList";
}
</script>
`);
}
else{
return res.status(200).send(`
<script>
if (confirm('게시글 등록에 성공했습니다.')) {
window.location.href = "/community/worryList";
}
</script>
`);
}
} else {
return res.send(`
<script>
if (confirm('게시글 등록에 실패했습니다.')) {
window.location.href = "/community/infoList";
}
</script>
`);
}
}
else {
return res.send('community req error(token)');
}
};
exports.postComment = async function (req, res) {
const token = req.cookies.x_auth;
if (token) {
const decodedToken = jwt.verify(token, process.env.jwtsecret); // 토큰 검증, 복호화
const user_id = decodedToken.user_id; // user_id를 추출
// console.log(req.body);
// var updated_at = new Date();
// console.log(updated_at);
//validation
if(!user_id) {
return res.send(errResponse(baseResponse.USER_USERIDX_EMPTY));
}
if (user_id <= 0) {
return res.send(errResponse(baseResponse.USER_USERIDX_LENGTH));
}
const {
category_name,
board_id,
content
} = req.body;
// console.log(req.body.content);
const createCommentResponse = await communityService.createComment(
user_id,
category_name,
board_id,
content,
0
);
if (createCommentResponse == "성공") {
return res.status(200).send(`
<script>
if (confirm('게시글 등록에 성공했습니다.')) {
const board_id = ${req.body.board_id};
window.location.href = "/community/write/" + board_id;
}
</script>
`);
} else
{
return res.send(`
<script>
if (confirm('게시글 등록에 실패했습니다.')) {
const board_id = ${req.body.board_id};
window.location.href = "/community/write/" + board_id;
}
</script>
`);
}
}
else {
return res.send('Comment req error(token)');
}
};