categori.js
5.56 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
var express = require('express');
var router = express.Router();
var CategoriModel = require('../models/CategoriModel');
var VideoModel = require('../models/VideoModel');
var Youtube = require('youtube-node');
var youtube = new Youtube();
var loginRequired = require('../libs/loginRequired');
var limit = 10; // 출력 갯수
youtube.setKey('AIzaSyCAaeW1qMSInEdN1OzU20FZlToIZYkb1bc'); // API 키 입력
router.get('/', function (req, res) {
res.send('categori main page');
});
router.get('/products', function (req, res) {
CategoriModel.find(function (err, products) {
res.render(
'category/products',
{ categories: products }
//ProductModel의 products를 받아서
//categori/products로 response를 보낸다.
);
});
});
router.get('/categories/write', loginRequired, function (req, res) {
res.render('category/form', { categories: '' });
});
router.post('/categories/write', loginRequired, function (req, res) {
var category = new CategoriModel({
title: req.body.title,
description: req.body.description,
username: req.user.username,
});
//이 아래는 수정되지 않았음
var validationError = category.validateSync();
if (validationError) {
res.send(validationError);
} else {
category.save(function (err) {
res.redirect('/categori/products');
});
}
//이 위는 수정되지 않았음
});
router.get('/products/detail/:id', function (req, res) {
//url 에서 변수 값을 받아올떈 req.params.id 로 받아온다
var word = req.query.keyword;
CategoriModel.findOne({ _id: req.params.id }, function (err, product) {
var video = [];
//제품정보를 받고 그안에서 댓글을 받아온다.
CategoriModel.find({ product_id: req.params.id }, function (err, comments) {
if (word != null) {
var count = 0;
youtube.addParam('order', 'rating'); // 평점 순으로 정렬
youtube.addParam('type', 'video'); // 타입 지정
youtube.addParam('videoLicense', 'creativeCommon'); // 크리에이티브 커먼즈 아이템만 불러옴
youtube.search(word, limit, function (err, result) {
// 검색 실행
console.log(word);
if (err) {
console.log(err);
} // 에러일 경우 에러공지하고 빠져나감
//console.log(JSON.stringify(result, null, 2)); // 받아온 전체 리스트 출력
var items = result['items']; // 결과 중 items 항목만 가져옴
for (var i in items) {
var it = items[i];
for (var j in it) {
if (it[j]['title'] != null) {
var title = it[j]['title'];
}
if (it[j]['videoId'] != null) {
var video_id = it[j]['videoId'];
}
var urls = 'https://www.youtube.com/watch?v=' + video_id;
}
var item = {
id: count,
title: title,
video_id: video_id,
urls: urls,
categori: product.title,
};
count++;
video.push(item);
}
res.render('category/productsDetail', {
product: product,
comments: comments,
videos: video,
});
});
} else {
res.render('category/productsDetail', {
product: product,
comments: comments,
videos: video,
});
}
});
});
});
router.post('/products/detail/:id', loginRequired, function (req, res) {
var item = [];
var count = 1;
for (var i in req.body.videoNum) {
item.push(req.body.videoNum[i].split('///'));
var video = new VideoModel({
categori: item[i][2],
id: count,
title: item[i][1],
video_id: item[i][3],
urls: item[i][4],
});
var validationError = video.validateSync();
if (validationError) {
res.send(validationError);
} else {
video.save(function (err) {});
}
count++;
}
res.redirect('/categori/products');
});
router.get('/products/edit/:id', loginRequired, function (req, res) {
//기존에 폼에 value안에 값을 셋팅하기 위해 만든다.
CategoriModel.findOne({ _id: req.params.id }, function (err, product) {
res.render('category/form', {
categories: product,
});
});
});
router.post('/products/edit/:id', loginRequired, function (req, res) {
//그전에 지정되 있는 파일명을 받아온다
CategoriModel.findOne({ _id: req.params.id }, function (err, product) {
var query = {
name: req.body.name,
thumbnail: req.file ? req.file.filename : product.thumbnail,
price: req.body.price,
description: req.body.description,
};
CategoriModel.update(
{ id: req.params.id },
{ $set: query },
function (err) {
res.redirect('/categori/products/detail/' + req.params.id);
}
);
});
});
router.get('/products/delete/:id', function (req, res) {
CategoriModel.deleteMany({ _id: req.params.id }, function (err) {
res.redirect('/categori/products');
});
});
router.post('/products/ajax_comment/insert', function (req, res) {
var comment = new CategoriModel({
content: req.body.content,
product_id: parseInt(req.body.product_id),
});
comment.save(function (err, comment) {
res.json({
id: comment.id,
content: comment.content,
message: 'success',
});
});
});
router.post('/products/ajax_comment/delete', function (req, res) {
CategoriModel.remove({ _id: req.body.comment_id }, function (err) {
res.json({ message: 'success' });
});
});
module.exports = router;