-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpostServer.js
More file actions
123 lines (111 loc) · 4.79 KB
/
Copy pathpostServer.js
File metadata and controls
123 lines (111 loc) · 4.79 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
var Var = require('./variables.js');
var sql = require('./sql.js');
var queue = require('./makeQueue.js');
//Регистрация водителя, пассажира
//На будущее: проверить номера. Номер должен быт 9-значным. Не должен совпадать в базе
Var.app.post('/registration', function(request, response) {
var body = request.body;
var name = body['name'];
var phone = body['phone'];
var human = body['human'];
var logTime = new Date().toLocaleTimeString();
console.log(logTime, " :: /registration", name, phone, human);
if (phone.length != 11) {
console.log("phone number is not 11-symbols");
response.send("phone number is not 11-symbols");
return;
}
if (name.length == 0) {
console.log("name is NULL");
response.send("name is NULL");
return;
}
if (human == 'driver' || human == 'passenger') {
sql.main('INSERT INTO ' + human + '(name, phone) VALUES ("' + name + '", "' + phone +
'") ON DUPLICATE KEY UPDATE name = VALUES(name);', function (error, rows) {
sql.main("SELECT id FROM " + human + " WHERE phone = " + phone + ";", function (error, rows) {
console.log("\t\tID: ", rows[0]["id"]);
if (error) {
console.log(error);
response.send(JSON.stringify(error));
return;
}
response.send(JSON.stringify(rows[0]["id"]));
});
});
return;
}
response.send("not added");
});
//Встать в очередь
Var.app.post('/qdriver', function(request, response) {
var body = request.body;
var id = body["id"] - 0;
var seats = body["seats"] - 0;
var time = body["time"] - 0;
var direction = body["direction"] - 0;
var date = body["date"];
var logTime = new Date().toLocaleTimeString();
var human = "driver";
console.log(logTime, ":: driverId: " + id + ",\ttime: " + time + ",\tdirection: " + direction + ",\tseats:" + seats);
addingToQueue(response, id, seats, time, direction, date, human);
});
Var.app.post('/qpassenger', function(request, response) {
var body = request.body;
var id = body["id"] - 0;
var booked = body["booked"] - 0;
var time = body["time"] - 0;
var direction = body["direction"] - 0;
var date = body["date"] - 0;
var logTime = new Date().toLocaleTimeString();
var human = "passenger";
console.log(logTime, ":: qpassengerId: " + id + ",\ttime: " + time + ",\tdirection: " + direction + ",\tbooked:" + booked);
addingToQueue(response, id, booked, time, direction, date, human);
});
//This will be general function, because /qpassenger and /qdriver have lot of identical lines
function addingToQueue(response, id, bookedSeats, time, direction, date, human) {
//Проверка ошибок
//Проверка направления
if (isNaN(direction) || direction < Var.directionMin || direction > Var.directionMax) {
console.log("\tunknown direction");
response.send("unknown direction");
return;
}
//Проверка Времени
if (isNaN(time) || time < 0 || time > 8) {
console.log("\tincorrect number of time");
response.send("incorrect number of time");
return;
}
if (isNaN(bookedSeats) || bookedSeats < 0 || bookedSeats > 4) {
console.log("\tincorrect number of booked");
response.send("incorrect number of booked");
return;
}
if (isNaN(date) || date < 0) {
console.log("\tincorrect number of date");
response.send("incorrect numberof date");
return;
}
sql.main("SELECT id FROM " + human + " WHERE id = " + id + ";", function(error, rows){
if(rows[0] === undefined)
{
console.log("\tno such user", id);
response.send("error 404: userID");
return;
}
var titleBookedSeats = human == 'driver' ? 'seats' : 'booked';
var sqlQuery = 'INSERT INTO q' + human + '(id_' + human + ', id_time, id_direction, '
+ titleBookedSeats + ', date) VALUES(' + id + ', ' + time + ', ' + direction + ', ' +
bookedSeats + ', ' + date + ') ON DUPLICATE KEY UPDATE ' + titleBookedSeats + ' = VALUES(' + titleBookedSeats + ');';
sql.main(sqlQuery, function (error, rows){
if (error) {
console.log("\tDuplicate key? \t", error);
response.send("error with adding " + titleBookedSeats + " to queue");
return;
}
response.send("success added to Queue");
console.log("\tsuccess added");
});
});
}