-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
174 lines (139 loc) · 5 KB
/
app.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
var bodyParser = require('body-parser');
var express = require('express');
var ejs = require('ejs');
var fs = require('fs');
var cookie = require('cookie');
var app = express();
app.set('view engine', 'ejs');
var mysql = require('mysql');
var pool = mysql.createPool({
connectionLimit : 100, //important
host : 'localhost',
user : 'uczen',
password : 'qwerty',
database : 'OGLOSZENIOWA',
debug : false
});
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
function getCategories(connection,callback) {
dBquery = "select * from kategoria"
connection.query(dBquery, function(err, rows2, fields) {
if (err) {
throw err;
} else {
return callback(rows2);
}
});
}//TODO: tutaj reguła 1000 ifów albo 1000 caseów i nowy parametr
app.use('/rsrc', express.static('rsrc'));
app.get('/', function(req,res) {
pool.getConnection(function(err,connection) {
getCategories(connection, function(res0) {
var cookies = cookie.parse(req.headers.cookie || '');
var name = cookies.user;
res.render('pages/main', {categories: res0, name: name}); ///////// TODO
connection.release();
});
});
});
app.use('/login', function(req,res){
res.render('pages/signin');
});
app.post('/LogInIN', function(req, res){
console.log('test log ');
pool.getConnection(function(err,connection) {
console.log('connected as id '+ connection.threadId);
var dBquery = "SELECT haslo FROM uzytkownicy WHERE mail ='" + req.body.mail +"'";
connection.query(dBquery, function(err, rows, fields) {
//var query = JSON.parse(rows2);
console.log(rows[0].haslo);
if(req.body.haslo == rows[0].haslo){
console.log("Dobre haslo"); // DOBRE HASLO LOGOWANIE
res.setHeader('Set-Cookie', cookie.serialize('user', req.body.mail, {
httpOnly: true,
maxAge: 60 * 60 * 24 * 2 // 2 DNI
}));
res.redirect('/');
} else{ // ZŁE HASLO LOGOWANIE
console.log("Zle haslo");
res.redirect('/login/');
}
if (err) {
throw err;
} else {
console.log("ksz ksz");
}
connection.release();
});
});
//express.static('home_page')
});
app.post('/RegIn', function(req, res){
pool.getConnection(function(err,connection) {
//console.log('connected as id ' + connection.threadId);
console.log('connected as id '+ connection.threadId);
var dBquery = "INSERT INTO uzytkownicy (mail, imie, nazwisko, nick, haslo, datarej, punkty, ogloszenia) VALUES ('"
+ req.body.mail + "','"
+ req.body.imie + "','"
+ req.body.nazwisko + "','"
+ req.body.nick + "','"
+ req.body.haslo + "',"
+ "CURDATE()"+ ","
+ 0 + ","
+ 0 +")";
var checkMail = "SELECT * FROM uzytkownicy WHERE mail ='" + req.body.mail +"' OR nick ='" + req.body.nick +"'";
connection.query(checkMail, function(err, rows2, fields) {
if (err) {
throw err;
} else {
console.log("Sprawdzam maila");
console.log("ksz ksz");
if(rows2[0] == undefined){
console.log("Dodawanie uzytkownika");
console.log("ksz ksz");
connection.query(dBquery, function(err, rows2, fields) {
if (err) {
throw err;
} else {
console.log("ksz ksz");
}
connection.release();
});
}else{
////////////////////////////////////////////////////////////// TODO JEST TEN MAIL
}
}
});
});
res.redirect('/login/');
//express.static('home_page')
});
app.get('/search', function(req,res) {
pool.getConnection(function(err,connection) {
console.log('connected as id ' + connection.threadId);
getCategories(connection,function(res0){
var dBquery="";
if (req.query.q)
dBquery = "select * from ogloszenie where lower(name) like lower('%" + req.query.q + "%')";
else if (req.query.cat)
dBquery = "select * from ogloszenie where kat_id = " + req.query.cat;
connection.query(dBquery, function(err, rows, fields) {
if (err) {
console.log('sql error (empty query or something)');
res.render('pages/search', {entriesEJ: [], categories: res0});
} else {
res.render('pages/search', {entriesEJ: rows, categories: res0}); //TODO: jak dojdzie więcej parametrów to zagnieździć w pizdu
}
connection.release(); //najważniejsza linia w całym kodzie
});
});
});
});
var server = app.listen(3000, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});