-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserv.js
443 lines (374 loc) · 17.1 KB
/
serv.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
// Подключение библиотек -------------------------------------------------------------------------------
var express = require('express');
require('dotenv').load();
var Pusher = require('pusher');
var path = require('path');
var bodyParser = require("body-parser");
var fs = require("fs");
var upload = require("express-fileupload");
const pg = require('pg');
var cookieParser = require('cookie-parser');
var bcrypt = require('bcrypt');
var app = express();
app.use(cookieParser());
const config = {
host: process.env.HOST,
database: process.env.DATABASE,
user: process.env.NAME,
password: process.env.PASS,
port: 5432,
ssl: true
};
const pool = new pg.Pool(config);
var urlencodedParser = bodyParser.urlencoded({ extended: false });
app.set('view engine', 'ejs');
app.use(express.static(path.join(__dirname, 'public')));
app.use('/auction/:id/',express.static(__dirname + '/public'));
app.use('/ok/:id/',express.static(__dirname + '/public'));
app.use('/no/:id/',express.static(__dirname + '/public'));
app.use('/filter/:id/',express.static(__dirname + '/public'));
app.use(upload());
// -------------------------------------------------------------------------------------------------------
// Проверка на подключение к БД --------------------------------------------------------------------------
app.get('/isDbConnected', function(req, res) {
pool.connect(function (err, client, done) {
if (err) {
done();
res.sendStatus(400);
}
else {
done();
res.sendStatus(200);
}
})
});
// ------------------------------------------------------------------------------------------------------
// Запрос для перехода на главную страницу --------------------------------------------------------------
app.get('/', function(req, res) {
pool.connect(function (err, client, done) {
if (err) {
console.log("Can not connect to the DB" + err);
}
client.query('select lots.lot_id, images.img_id, images.img_name, lots.model, lots.price, brands.brand ' +
'from lots, images, brands ' +
'where lots.lot_id = images.lot_id ' +
'and images.ismain = true ' +
'and lots.permiss = true ' +
'and lots.brand_id = brands.brand_id;', function (err, result) {
done();
if (err) {
console.log(err);
res.status(400).send(err);
}
if (req.cookies.name == undefined) {
res.render('index', {catalogJSON : JSON.stringify(result.rows), info : '', money : '', value : '', main : true});
}
else {
res.render('index', {catalogJSON : JSON.stringify(result.rows), info : req.cookies.name, money : req.cookies.cash, value : req.cookies.value, main : true});
}
})
})
});
app.get('/admin', function(req, res) {
if (req.cookies.value == 'true') {
pool.connect(function (err, client, done) {
if (err) {
console.log("Can not connect to the DB" + err);
}
client.query('select * from lots, images where lots.lot_id = images.lot_id and images.ismain = true and lots.permiss = false;', function (err, result) {
done();
if (err) {
console.log(err);
res.status(400).send(err);
}
var catalog = result.rows;
var catalogJSON = JSON.stringify(catalog);
if (req.cookies.name == undefined) {
res.render('index', {catalogJSON : catalogJSON, info : '', money : '', value : '', main : true});
}
else {
res.render('index', {catalogJSON : catalogJSON, info : req.cookies.name, money : req.cookies.cash, value : req.cookies.value, main : false});
}
})
})
}
else res.redirect('/');
});
app.get('/filter/:id', function(req, res) {
if (req.params.id != 'my') {
if (Number(req.params.id) == 0) {
var gender = true;
}
else {
var gender = false;
}
pool.connect(function (err, client, done) {
if (err) {
console.log("Can not connect to the DB" + err);
}
client.query('select lots.lot_id, images.img_id, images.img_name, lots.model, lots.price, brands.brand ' +
'from lots, images, brands ' +
'where lots.lot_id = images.lot_id ' +
'and images.ismain = true ' +
'and lots.permiss = true ' +
'and lots.brand_id = brands.brand_id ' +
'and lots.gender = $1;', [gender], function (err, result) {
done();
if (err) {
console.log(err);
res.status(400).send(err);
}
if (req.cookies.name == undefined) {
res.render('index', {catalogJSON : JSON.stringify(result.rows), info : '', money : '', value : '', main : true});
}
else {
res.render('index', {catalogJSON : JSON.stringify(result.rows), info : req.cookies.name, money : req.cookies.cash, value : req.cookies.value, main : true});
}
})
})
}
else {
if (req.cookies.name == undefined) {
res.redirect('/');
}
else {
pool.connect(function (err, client, done) {
if (err) {
console.log("Can not connect to the DB" + err);
}
client.query('select lots.lot_id, images.img_id, images.img_name, lots.model, lots.price, brands.brand ' +
'from lots, images, brands ' +
'where lots.lot_id = images.lot_id ' +
'and images.ismain = true ' +
'and lots.permiss = true ' +
'and lots.brand_id = brands.brand_id ' +
'and lots.user_id = $1;', [req.cookies.user_id], function (err, result) {
done();
if (err) {
console.log(err);
res.status(400).send(err);
}
res.render('index', {catalogJSON : JSON.stringify(result.rows), info : req.cookies.name, money : req.cookies.cash, value : req.cookies.value, main : 'my'});
})
})
}
}
});
// -------------------------------------------------------------------------------------------------------
// Вход, выход и регистрация в системе -------------------------------------------------------------------
app.get('/out', function(req, res) {
res.clearCookie('name');
res.clearCookie('user_id');
res.clearCookie('cash');
res.clearCookie('value');
res.redirect('/');
});
app.get('/log', function(req, res) {
res.render('log', {mess : ''});
});
app.get('/sign', function(req, res) {
res.render('sign', {mess : ''});
});
app.post('/login', urlencodedParser, function(req, res) {
pool.connect(function (err, client, done) {
client.query('select * from users where users.login = $1 and users.pass = $2', [req.body.email, bcrypt.hashSync(req.body.password, process.env.SALT)], function (err, result) {
done()
if (result.rows.length == 0) {
res.render('log', {mess : 'Логин или пароль введены неверно'});
}
else {
res.cookie('user_id', result.rows[0].user_id, {expires: new Date(Date.now() + 3000000), httpOnly: true});
res.cookie('name', result.rows[0].name, {expires: new Date(Date.now() + 3000000), httpOnly: true});
res.cookie('cash', result.rows[0].cash, {expires: new Date(Date.now() + 3000000), httpOnly: true});
res.cookie('value', result.rows[0].admin, {expires: new Date(Date.now() + 3000000), httpOnly: true});
//console.log(req.cookies);
res.redirect('/');
}
})
})
});
app.post('/signin', urlencodedParser, function(req, res) {
if ((req.body.password == req.body.password.match(/(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])[0-9a-zA-Z]{8,}/g)[0]) && (req.body.name == req.body.name.match(/[a-zA-Z]{1,}/g)[0])) {
pool.connect(function (err, client, done) {
client.query('select login from users where users.login = $1', [req.body.email], function (err, result) {
done()
if (result.rows.length == 0) {
client.query('INSERT INTO users(name, login, link, cash, pass, admin) VALUES($1, $2, $3, $4, $5, $6);', [req.body.name, req.body.email, req.body.link, 2500, bcrypt.hashSync(req.body.password, process.env.SALT), false], function (err, result) {
res.redirect('/');
})
}
else {
res.render('sign', {mess : 'Пользователь с таким email уже существует'});
}
})
})
}
else {
res.render('sign', {mess : 'Неверный формат данных'});
}
});
// ------------------------------------------------------------------------------------------------------
// Функционал опубликовки объявления --------------------------------------------------------------------
app.get('/sell', function(req, res) {
if (req.cookies.user_id == undefined) {
res.redirect('/sign');
}
else {
pool.connect(function (err, client, done) {
client.query('select * from cities;', function (err, result) {
var cities = result.rows;
client.query('select * from sizes;', function (err, result) {
var sizes = result.rows;
client.query('select * from conditions;', function (err, result) {
var conditions = result.rows;
client.query('select * from brands;', function (err, result) {
var brands = result.rows;
client.query('select * from categories;', function (err, result) {
var categories = result.rows;
client.query('select * from globals;', function (err, result) {
var globals = result.rows;
done();
res.render('new-offer', {cities : cities, sizes : sizes, conditions : conditions, brands : brands, categories : categories, globals : globals});
})
})
})
})
})
})
})
}
});
app.post('/place', urlencodedParser, function (req, res) {
var main = true;
var max = 0;
var number = 0;
if (req.body.sex == 0) {
var gender = false;
}
else {
var gender = true;
}
pool.connect(function (err, client, done) {
client.query('BEGIN', function (err, result) {
client.query('select max(lots.lot_id) from lots;', function (err, result) {
counter = result.rows[0].max + 1;
client.query('select max(images.img_id) from images;', function (err, result) {
max = result.rows[0].max + 1;
if (req.files.basePhoto.length == undefined) {
fs.writeFileSync("public/image" + Number(max) + ".jpg", req.files.basePhoto.data);
}
else {
for (i=0; i<req.files.basePhoto.length; i++){
fs.writeFileSync("public/image" + Number(max + i) + ".jpg", req.files.basePhoto[i].data);
}
}
client.query('INSERT INTO lots(lot_id, user_id, brand_id, comment, time, price, gender, category_id, city_id, size_id, condition_id, permiss, model) ' +
'VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13);',
[counter, req.cookies.user_id, req.body.brand, req.body.description, 333, Number(req.body.price), gender, req.body.category, req.body.city, req.body.size, req.body.condition, false, req.body.model],
function (err, result) {
if (err) {
console.log('ошибка1');
res.status(400).send(err);
}
if (req.files.basePhoto.length == undefined) {
client.query('INSERT INTO images(img_id, lot_id, img_name, ismain) VALUES($1, $2, $3, $4);', [max, counter, "image" + Number(max) + ".jpg", main], function (err, result) {
if (err) {
console.log('ошибка' + i);
}
})
}
else {
for (i=0; i<req.files.basePhoto.length; i++){
if (i>0) main = false;
client.query('INSERT INTO images(img_id, lot_id, img_name, ismain) VALUES($1, $2, $3, $4);', [max + i, counter, "image" + Number(max + i) + ".jpg", main], function (err, result) {
if (err) {
console.log('ошибка' + i);
}
})
}
}
client.query('COMMIT', function (err, result) {
done()
res.redirect('/');
})
})
})
})
})
})
})
// -----------------------------------------------------------------------------------------------------
// Получение информации о лоте и продавце --------------------------------------------------------------
app.get('/auction/:id', function(req, res) {
pool.connect(function (err, client, done) {
client.query('select lots.lot_id, users.name, users.link, lots.user_id, lots.comment, lots.time, lots.price, lots.gender, ' +
'lots.model, images.img_id, brands.brand, cities.city, categories.category, globals.global, sizes.size, conditions.condition ' +
'from lots, users, images, brands, cities, categories, globals, sizes, conditions ' +
'where lots.lot_id = images.lot_id ' +
'and lots.lot_id = $1 ' +
'and lots.brand_id = brands.brand_id ' +
'and lots.city_id = cities.city_id ' +
'and lots.category_id = categories.category_id ' +
'and categories.global_id = globals.global_id ' +
'and lots.size_id = sizes.size_id ' +
'and lots.condition_id = conditions.condition_id ' +
'and lots.user_id = users.user_id;', [Number(req.params.id)], function (err, result) {
shmot = result.rows;
client.query('select reviews.review, users.name, reviews.score ' +
'from reviews, users, users_reviews ' +
'where users_reviews.user_id = $1 ' +
'and users_reviews.review_id = reviews.review_id ' +
'and reviews.customer_id = users.user_id', [shmot[0].user_id], function (err, result) {
done()
if (req.cookies.name == undefined) {
res.render('auction', {shmot : shmot, info : '', money : '', fback : result.rows});
}
else {
res.render('auction', {shmot : shmot, info : req.cookies.name, money : req.cookies.cash, fback : result.rows});
}
})
})
})
});
app.post('/feedback', urlencodedParser, function(req, res) {
if (req.cookies.user_id == undefined) {
res.redirect('/sign');
}
else {
pool.connect(function (err, client, done) {
client.query('select max(reviews.review_id) from reviews;', function (err, result) {
max = result.rows[0].max + 1;
client.query('INSERT INTO reviews(review_id, review, score, customer_id, permiss) VALUES($1, $2, $3, $4, $5);', [max, req.body.feedback, req.body.score, req.cookies.user_id, true], function (err, result) {
client.query('INSERT INTO users_reviews(user_id, review_id) VALUES($1, $2);', [req.body.user_id, max], function (err, result) {
done();
res.redirect('/');
})
})
})
})
}
});
// ----------------------------------------------------------------------------------------------------
// Обработка объявлений админом -----------------------------------------------------------------------
app.get('/ok/:id', function(req, res) {
pool.connect(function (err, client, done) {
client.query('UPDATE lots SET permiss = $1 WHERE lot_id = $2', [true, Number(req.params.id)], function (err, result) {
if (err) {
console.log(err);
res.status(400).send(err);
}
done()
res.redirect('/admin');
})
})
});
app.get('/no/:id', function(req, res) {
pool.connect(function (err, client, done) {
client.query('DELETE FROM lots WHERE lot_id = $1;', [Number(req.params.id)], function (err, result) {
done()
res.redirect('/admin');
})
})
});
// --------------------------------------------------------------------------------------------------
app.listen(process.env.PORT);