-
Notifications
You must be signed in to change notification settings - Fork 0
/
admin_products.js
343 lines (268 loc) · 11 KB
/
admin_products.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
var express = require('express');
var router = express.Router();
var mkdirp = require('mkdirp');
var fs = require('fs-extra');
var resizeImg = require('resize-img');
var auth = require('../config/auth');
var isAdmin = auth.isAdmin;
//cho thêm isAdmin vào mỗi phần get để khi mà người dùng bấm /admin/... trên thanh url thì sẽ không đc nữa
//get product models, lấy Product từ product.js trong models
var Product = require('../models/product');
//get category trong models
var Category = require('../models/category');
const { fsyncSync } = require('fs-extra');
/*
* get product index
*/
// localhost:3000/admin/products đường dẫn thiết lập bên index.js ngoài cùng
router.get('/', isAdmin, function(req, res) {
var count;
Product.count(function(err, c) {
count = c;
});
Product.find(function(err, products) {
res.render('admin/products', {
products: products,
count: count,
});
});
});
//get add product render ra trang thêm trang của admin còn cái này /admin/products/add-product
router.get('/add-product', isAdmin, function(req, res) {
var title = "";
var desc = "";
var price = "";
//để category bên ngoài khi thêm sản phẩm sẽ chọn sản phẩm đó thuộc danh mục nào, giống như thể loại truyện trong hako vaayk, truyện dịch, sáng tác, máy dich,...
Category.find(function(err, categories) {
res.render('admin/add-product', {
title: title,
desc: desc,
categories: categories,
price: price,
});
});
});
//post add product thêm sản phẩm
router.post('/add-product', function(req, res) {
if(!req.files){
imageFile ="";
}
if(req.files){
var imageFile = typeof(req.files.image) !== "undefined" ? req.files.image.name : "";
} //imageFile khi làm ở mọi bài toán(chắc thế)
req.checkBody('title', 'Title must have value').notEmpty();
req.checkBody('desc', 'Description must have value').notEmpty();
req.checkBody('price', 'Price must have value').isDecimal();
req.checkBody("image", 'You must upload your image').isImage(imageFile);
var title = req.body.title;
var slug = title.replace(/\s+/g, '-').toLowerCase();
var desc = req.body.desc;
var price = req.body.price;
var category = req.body.category;
var errors = req.validationErrors();
if(errors){ //lỗi thì hiển thị thông báo ko đc để trống
Category.find(function(err, categories) {
res.render('admin/add-product', {
errors: errors,
title: title,
desc: desc,
categories: categories,
price: price,
});
});
}else{
Product.findOne({slug: slug}, function(err, product) {
if(product){ //findOne khi thêm, nếu cái product đấy tồn tại thì in ra lỗi
req.flash('danger', 'Product title exists, choose another');
Category.find(function(err, categories) {
res.render('admin/add-product', {
title: title,
desc: desc,
categories: categories,
price: price,
});
});
} else { //add vào mongoDB
var price2 = parseFloat(price).toFixed(2);
var product = new Product({
title: title,
slug: slug,
price: price2,
desc: desc,
category: category,
image: imageFile,
});
product.save(function(err) {
if (err)
return console.log(err);
fs.mkdir('public/product_images/' +product._id, { recursive: true }); //thực hiện tạo folder có tên là id của sản phẩm
fs.mkdirp('public/product_images/' +product._id + '/gallery', { recursive: true }); //thêm folder gellery sau cái folder trên
fs.mkdirp('public/product_images/' +product._id + '/gallery/thumbs', { recursive: true }); //tương tự
if(imageFile != "") {
var productImage = req.files.image;
var path = 'public/product_images/' + product._id + '/' + imageFile; //lưu ảnh vào folder id
productImage.mv(path, function(err) {
return console.log(err);
})
}
req.flash('success', 'Product added successfully!!!');
res.redirect('/admin/products');
});
}
});
}
});
//get trang sửa product
router.get('/edit-product/:id', isAdmin, function(req, res) {
var errors;
if(req.session.errors)
errors = req.session.errors;
req.session.errors = null;
Category.find(function(err, categories) {
Product.findById(req.params.id, function(err, product) {
if(err){
console.log(err);
res.redirect('/admin/products');
} else {
var galleryDir = 'public/product_images/' + product._id + '/gallery';
var galleryImages = null;
fs.readdir(galleryDir, function(err, files) {
if(err) {
console.log(err);
} else {
galleryImages = files;
res.render('admin/edit-product', {
title: product.title,
errors: errors,
desc: product.desc,
categories: categories,
category: product.category.replace(/\s+/g, '-').toLowerCase(),
price: parseFloat(product.price).toFixed(2),
image: product.image,
galleryImages: galleryImages,
id: product._id,
});
}
});
}
});
});
});
//post trang sua product
router.post('/edit-product/:id', function(req, res) {
if(!req.files){
imageFile ="";
}
if(req.files){
var imageFile = typeof(req.files.image) !== "undefined" ? req.files.image.name : "";
} //imageFile khi làm ở mọi bài toán(chắc thế)
req.checkBody('title', 'Title must have value').notEmpty();
req.checkBody('desc', 'Description must have value').notEmpty();
req.checkBody('price', 'Price must have value').isDecimal();
req.checkBody("image", 'You must upload your image').isImage(imageFile);
var title = req.body.title;
var slug = title.replace(/\s+/g, '-').toLowerCase();
var desc = req.body.desc;
var price = req.body.price;
var category = req.body.category;
var pimage = req.body.pimage;
var id = req.params.id;
var errors = req.validationErrors();
if (errors) {
req.session.errors = errors;
res.redirect('/admin/products/edit-product/' + id);
} else {
Product.findOne({slug: slug, _id:{'$ne': id}}, function (err, p) {
if(err)
console.log(err);
if (p) {
req.flash('danger', 'Product title exists choose another!!!');
res.redirect('/admin/products/edit-product/' + id);
} else {
Product.findById(id, function(err, p) {
if (err)
console.log(err);
p.title = title;
p.slug = slug;
p.desc = desc;
p.price = parseFloat(price).toFixed(2);
p.category = category;
if(imageFile != "") {
p.image = imageFile;
}
p.save(function(err) {
if(err)
console.log(err);
if (imageFile != "") {
if(pimage != "") {
fs.remove('public/product_images/' + id + '/' + pimage, function(err) {
if(err)
console.log(err);
});
}
var productImage = req.files.image;
var path = 'public/product_images/' + id + '/' + imageFile; //lưu ảnh vào folder id
productImage.mv(path, function(err) {
return console.log(err);
});
}
req.flash('success', 'Product edited successfully!!!');
res.redirect('/admin/products/edit-product/' + id);
});
});
}
});
}
});
//post product gallery
router.post('/product-gallery/:id', function(req, res) {
var productImage = req.files.file;
var id = req.params.id;
var path = 'public/product_images/' + id + '/gallery/' + req.files.file.name;
var thumbsPath = 'public/product_images/' + id + '/gallery/thumbs/' + req.files.file.name;
productImage.mv(path, function(err) {
if (err)
console.log(err);
resizeImg(fs.readFileSync(path), {width:100, height:100})
.then(function(buf) {
fs.writeFileSync(thumbsPath, buf);
});
});
res.sendStatus(200);
});
//get delele gallery image
router.get('/delete-image/:image', isAdmin, function(req, res) {
var originalImage = 'public/product_images/' + req.query.id + '/gallery/' + req.params.image;
var thumbImage = 'public/product_images/' + req.query.id + '/gallery/thumbs/' + req.params.image;
fs.remove(originalImage, function(err) {
if (err) {
console.log(err);
} else {
fs.remove(thumbImage, function(err) {
if (err) {
console.log(err);
} else {
req.flash('success','Image deleted');
res.redirect('/admin/products/edit-product/' + req.query.id);
}
});
}
});
});
//get trang xóa trang
router.get('/delete-product/:id', isAdmin, function(req, res) {
var id = req.params.id;
var path = 'public/product_images/' + id;
fs.remove(path, function(err) {
if (err) {
console.log(err);
} else {
Product.findByIdAndRemove(id, function(err) {
console.log(err);
});
req.flash('success','Product deleted');
res.redirect('/admin/products');
}
});
});
module.exports = router;