-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
287 lines (260 loc) · 7.38 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
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
// Requires
let express = require('express');
let morgan = require('morgan');
let bodyParser = require('body-parser');
let mongoose = require('mongoose');
let bcrypt = require('bcryptjs');
let { UserList } = require('./model');
const {DATABASE_URL, PORT} = require('./config');
// Setup
let app = express();
let jsonParser = bodyParser.json();
app.use(express.static('public'));
app.use(morgan('combined'));
app.use('/favicon.ico', express.static('/favicon.ico'));
// API
app.get('/dashboard', (req, res) => {
res.sendFile('/public/dashboard.html', {root: __dirname});
});
app.get('/about', (req, res) => {
res.sendFile('/public/about.html', {root: __dirname});
});
app.post('/api/register', jsonParser, (req, res) => {
let {email, password} = req.body;
if(!email || !password) {
return res.status(406).json({
status: 406,
message: "Missing Params"
});
}
bcrypt.hash(password, 10).then(hashPasss => {
UserList.getByEmail(email).then( response => {
if(response.length == 0) {
//Initialize Pomodoro
let pomodoro = {
workLength: 25,
breakLength: 5,
longBreakLength: 30
};
console.log(hashPasss);
UserList.postUser({email, password: hashPasss, pomodoro}).then(user => {
console.log(user);
return res.status(200).json(user);
}).catch(err => {
console.log(err);
return res.status(500).json(err);
});
} else {
res.statusMessage = "User Already Exists";
return res.status(409).json({message: "User Already Exists"});
}
}).catch( err => {
return res.status(500).json({message: "Internal Server Error"});
})
}).catch(err => {
return res.status(500).json({message: "Internal Server Error"});
})
});
app.post('/api/login', jsonParser, (req, res) => {
let {email, password} = req.body;
UserList.getByEmail(email).then( user => {
if(user.length == 0) {
res.statusMessage = "User or password is incorrect";
return res.status(401).json({
message : "User or password is incorrect",
status : 401
});
}
bcrypt.compare(password, user[0].password).then( response => {
if(response) {
return res.status(200).json({
message: "Success",
status: 201
});
}
res.statusMessage = "User or password is incorrect";
return res.status(401).json({
message : "User or password is incorrect",
status : 401
});
}).catch(err => {
res.statusMessage = "Internal Server Error"
return res.status(500).json({
message : "Internal Server Error",
status : 500
});
});
}).catch( error => {
console.log(error);
res.statusMessage = "Internal Server Error"
return res.status(500).json({
message : "Internal Server Error",
status : 500
});
});
});
app.get('/api/User', jsonParser, (req, res) => {
let email = req.query.email;
UserList.getByEmail(email).then( response => {
console.log(response);
return res.status(200).json(response);
}).catch( err => {
console.log(err);
return res.status(500).json(err);
})
});
app.post('/api/createCourse', jsonParser, (req, res) => {
let {email, name, allottedTime} = req.body;
UserList.postCourse(email, {name, allottedTime, spentTime: 0}).then(response => {
console.log(response);
if(response == 404) {
res.statusMessage = "User not found";
return res.status(404).json({message: "User not found"});
}
if(response == 409) {
res.statusMessage = "Course already Exists";
return res.status(409).json({message: "Course already Exist"});
}
return res.status(200).json(response);
}).catch( err => {
console.log(err);
return res.status(500).json(err);
})
});
app.put('/api/updateCourse', jsonParser, (req, res) => {
let {email, course} = req.body;
UserList.putCourse(email, course).then( response => {
console.log(response);
if(response == 404) {
res.statusMessage = "Course not found";
return res.status(404).json({message: "Course not found"});
}
return res.status(200).json(response);
}).catch( error => {
console.log(error);
return res.status(500).json(error);
})
});
app.delete('/api/deleteCourse', jsonParser, (req, res) => {
let {email, name} = req.body;
UserList.deleteCourse(email, name).then( response => {
return res.status(202).json(response);
}).catch(err => {
console.log(err);
return res.status(500).json(err);
})
});
app.post('/api/createTask', jsonParser, (req, res) => {
let { email, name, description} = req.body;
UserList.createTask(email, name, {description, complete: false}).then( response => {
console.log(response);
return res.status(200).json(response);
}).catch(err => {
console.log(err);
return res.status(500).json(err);
})
});
app.delete('/api/deleteTask', jsonParser, (req, res) => {
let {email, name, id} = req.body;
UserList.deleteTask(email, name, id).then( response => {
console.log(response);
return res.status(200).json(response);
}).catch(error => {
console.log(error);
return res.status(500).json(error);
})
});
app.put('/api/updateTask', jsonParser, (req, res) => {
let {email, name, task} = req.body;
UserList.updateTask(email, name, task)
.then(response => {
console.log(response);
return res.status(200).json(response);
})
.catch(error => {
console.log(error);
return res.status(500).json(error);
})
});
app.put('/api/updatePomodoro', jsonParser, (req, res) => {
let { email, pomodoro } = req.body;
UserList.updatePomodoro(email, pomodoro).then( response => {
return res.status(200).json(response);
}).catch(err => {
return res.status(500).json(err);
});
});
app.post('/api/createHistory', jsonParser, (req, res) => {
let {email, name, pomodoroCount} = req.body;
UserList.createHistory(email, {name, pomodoroCount}).then(response => {
console.log(response);
if(response == 404) {
res.statusMessage = "User not found";
return res.status(404).json({message: "User not found"});
}
if(response == 409) {
res.statusMessage = "History already exists";
return res.status(409).json({message: "History already exists"});
}
return res.status(200).json(response);
}).catch( err => {
console.log(err);
return res.status(500).json(err);
})
});
app.put('/api/updateHistory', jsonParser, (req, res) => {
let { email, name } = req.body;
UserList.updateHistory(email, name).then( response => {
return res.status(200).json(response);
}).catch(err => {
return res.status(500).json(err);
})
});
app.delete('/api/deleteHistory', jsonParser, (req, res) => {
let {email, name} = req.body;
UserList.deleteHistory(email, name).then( response => {
return res.status(202).json(response);
}).catch(err => {
console.log(err);
return res.status(500).json(err);
})
});
let server;
function runServer(port, databaseUrl) {
return new Promise( function(resolve, reject) {
mongoose.connect(databaseUrl, function(error) {
if (error) {
return reject(error);
}
else {
server = app.listen(port, function() {
console.log('You have entered a cursed land ' + port);
resolve();
}).on('error', function(error) {
mongoose.disconnect();
return reject(error);
});
}
});
});
};
runServer(PORT, DATABASE_URL).catch(function(error) {
console.log(error);
});
function closeServer(){
return mongoose.disconnect().then(() => {
return new Promise((resolve, reject) => {
console.log('Closing the server');
server.close( err => {
if (err){
return reject(err);
}
else{
resolve();
}
});
});
});
}
module.exports = { app, runServer, closeServer };
// Exports