-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
128 lines (103 loc) · 3.28 KB
/
server.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
var express = require("express");
var app = express();
var bodyParser = require("body-parser");
var serverState = require("./testData.json");
// --- TEST DATA ---
var states = {
FAILURE: "FAILURE",
SUCCESS: "SUCCESS",
DATABASE_ERROR: "DATABASE_ERROR",
INVALID_USER: "INVALID_USER",
INVALID_PASSWORD: "INVALID_PASSWORD",
INVALID_SESSION: "INVALID_SESSION",
RESET_SUCCES: "RESET_SUCCESS"
};
function warning (request, errorState) {
return {
request: request,
error: errorState
}
}
function getUserBookedRides (userId) {
var userRides = serverState.users[userId].bookedRides;
userRides.map(function (ride) {
return serverState.rides[ride];
})
}
function resolveBookedRides () {
var ride1 = serverState.rides["51"];
var ride2 = serverState.rides["52"];
var ride3 = serverState.rides["53"];
ride1.driver = serverState.drivers[ride1.driver];
ride2.driver = serverState.drivers[ride2.driver];
ride3.driver = serverState.drivers[ride3.driver];
ride1.car = serverState.cars[ride1.car];
ride2.car = serverState.cars[ride2.car];
ride3.car = serverState.cars[ride3.car];
ride1.start = serverState.matchpoints[ride1.start];
ride2.start = serverState.matchpoints[ride2.start];
ride3.start = serverState.matchpoints[ride3.start];
ride1.destination = serverState.matchpoints[ride1.destination];
ride2.destination = serverState.matchpoints[ride2.destination];
ride3.destination = serverState.matchpoints[ride3.destination];
return [ride1, ride2, ride3];
}
function checkSession (userId, sessionId) {
return sessionId === serverState.users[userId].session.id;
}
// --- ROUTES ---
app.use(bodyParser.json());
app.use("/assets", express.static(__dirname + "/assets"));
app.post("/login", function (req, res) {
console.log(req);
var testUser = serverState.users["11"];
if (req.body.email === testUser.email) {
if (req.body.password === testUser.password) {
res.send({session: testUser.session.id, user: testUser});
} else {
res.send(warning(req.body, states.INVALID_PASSWORD));
}
} else {
res.send(warning(req.body, states.INVALID_USER));
}
});
app.post("/booked-rides", function (req, res) {
var testUser = serverState.users["11"];
var rides = serverState.rides;
if (checkSession(req.body.userId, req.headers.session)) {
if (req.body.userId === testUser.id) {
res.send({rides: [rides["51"], rides["52"], rides["53"]]});
} else {
res.send(warning(req.body, states.INVALID_USER));
}
} else {
res.send(warning(req.body, states.INVALID_SESSION));
}
});
app.post("/update-user", function (req, res) {
res.send(states.SUCCESS);
});
app.post("/account-details", function (req, res) {
res.send(user);
});
app.post("/matchpoints", function (req, res) {
res.send([location, destination]);
});
app.post("/available-rides", function (req, res) {
res.send([ride]);
});
app.post("/book-ride", function (req, res) {
res.send(states.SUCCESS);
});
app.post("/cancel-ride", function (req, res) {
res.send(states.SUCCESS);
});
app.get("/reset", function (req, res) {
serverState = require("./testData.json");
resolveBookedRides();
res.send(states.RESET_SUCCESS);
})
app.listen(3000, function () {
resolveBookedRides();
console.log("Server listening on port 3000!");
});