-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
executable file
·396 lines (327 loc) · 11.5 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
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
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var multer = require('multer');
var mongoose = require('mongoose');
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
var cookieParser = require('cookie-parser');
var session = require('express-session')
app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
app.use(multer()); // for parsing multipart/form-data
app.use(session({ secret: "this is the secret"}));
app.use(cookieParser());
app.use(passport.initialize());
app.use(passport.session());
// Allow user to access profile.html and other static resources
app.use(express.static(__dirname + '/public'));
passport.use(new LocalStrategy(
function(username, password, done)
{
console.log("in passport");
UserModel.findOne({username: username, password: password}, function (err, user){
if(user)
{
console.log("in passport");
console.log(user);
// if user is found return user
return done(null, user);
}
// otherwise we return FALSEEE
return done(null, false, {message: 'Unable to login'});
});
}));
passport.serializeUser(function(user, done){
done(null, user);
});
passport.deserializeUser(function(user, done){
done(null, user);
});
/*
* DATABASE CONNECTION / SCHEMA
*/
var connectionString = process.env.OPENSHIFT_MONGODB_DB_URL || 'mongodb://localhost/db';
var db = mongoose.connect(connectionString);
// information for a user
var UserSchema = new mongoose.Schema({
username: String,
password: String,
location: String,
about: String
});
// car info for database so we avoid making API calls for just the vehicle name
var CarSchema = new mongoose.Schema({
edmundsID: Number,
vehicleName: String,
modelYear: Number
});
var UserToCarFavoritesSchema = new mongoose.Schema({
favoritedUsername: String,
edmundsID: Number
});
// represent a user following another user
var UserToUserFollowSchema = new mongoose.Schema({
followerUsername: String,
followingUsername: String
});
// represent a user's comment on a car
var UserToCarCommentSchema = new mongoose.Schema({
username: String,
edmundsID: Number,
comment: String,
date: { type: Date, default: Date.now }
});
// represent a user/s comment on a user's profile
var UserToUserCommentSchema = new mongoose.Schema({
username: String,
commenterUsername: String,
comment: String,
date: { type: Date, default: Date.now }
});
/*// schema for comments on car detail pages
var CarCommentSchema = new mongoose.Schema({
edmundsID: Number,
vehicleName: String,
username: String,
comment: String,
date: { type: Date, default: Date.now }
});*/
// model for maintaining user data
var UserModel = mongoose.model("UserModel", UserSchema);
// model for maintainng car data
var CarModel = mongoose.model("CarModel", CarSchema);
// model for maintaining user favorites
var UserToCarFavoritesModel = mongoose.model("UserToCarFavoritesModel", UserToCarFavoritesSchema);
// model for maintaining user follows
var UserToUserFollowModel = mongoose.model("UserToUserFollowModel", UserToUserFollowSchema);
// model for maintaining user car comments
var UserToCarCommentModel = mongoose.model("UserToCarCommentModel", UserToCarCommentSchema);
// model for maintaining user profile comments
var UserToUserCommentModel = mongoose.model("UserToUserCommentModel", UserToUserCommentSchema);
/*
* API DEFINITIONS
*/
app.post("/login", passport.authenticate('local'), function(req, res){
res.json(req.user);
});
app.get("/loggedin", function(req, res){
res.send(req.isAuthenticated() ? req.user : '0');
});
app.post("/logout", function(req, res){
req.logOut();
res.send(200);
});
app.post("/register", function (req, res){
UserModel.findOne({username: req.body.username}, function(err, user){
// if user already exists, return null
if(user)
{
res.json(null);
return;
}
else
{
var newUser = new UserModel(req.body);
newUser.save(function(err, user){
req.login(user, function(err)
{
if(err) {return next(err); }
res.json(user);
});
});
}
});
var newUser = req.body;
console.log(newUser);
});
// retrieve a user based on username
app.get("/getUser/:username", function (req, res){
UserModel.findOne({username: req.params.username}, function(err, user){
res.json(user);
});
});
// retrieve user content for a given Edmunds vehicle ID
app.get("/getCarContent/:vehicleId", function (req, res){
CarModel.findOne({vehicleId : req.params.vehicleId}, function(err, car){
res.json(car);
});
});
// favorite a car, add car ID to user entry and username to car entry
app.post("/favoriteCar/:username/:vehicleId/:vehicleName/:modelYear", function(req, res){
var newUserToCarFavorite = new UserToCarFavoritesModel({favoritedUsername: req.params.username, edmundsID: req.params.vehicleId});
newUserToCarFavorite.save();
// if we don't have this car in our local database yet, add it
CarModel.findOne({edmundsID : req.params.vehicleId}, function(err, car){
if (!car)
{
// create new car to store in our database to maintain user interactions
console.log("adding car to favorites" + car)
var newCar = new CarModel({edmundsID: req.params.vehicleId, vehicleName: req.params.vehicleName, modelYear: req.params.modelYear});
newCar.save();
}
});
res.send(200);
});
// unfavorite a car, remove relationship between user and vehicle
app.post("/deleteFavoriteCar/:username/:vehicleId", function(req, res){
console.log("trying to remove edmundsID " + req.params.vehicleId + " attached to user " + req.params.username);
UserToCarFavoritesModel.remove( {$and: [{favoritedUsername: req.params.username}, {edmundsID: req.params.vehicleId}]}, function(err,removed)
{
console.log("within remove error: " + err);
console.log("within remove removed: " + removed);
});
res.send(200);
});
// retrieve the car objects that a user likes
app.get("/getUsersFavorites/:username", function(req, res){
var result;
// find the relational entries
UserToCarFavoritesModel.find({favoritedUsername: req.params.username}, function(err, relations)
{
console.log("Got array of cars user likes");
// get a list of IDs of all cars the user likes from the relations, and find all entries in car model that match the IDs
CarModel.find({edmundsID: {$in: relations.map(function(item){ return item.edmundsID})}}, function(err, cars){
console.log("cars found are ");
console.log(cars);
// return all matches
res.json(cars);
});
});
});
// retrieve the IDs of the cars the current user has liked
app.get("/getUsersFavoritesIDs/:username", function(req, res){
var result;
// find the relational entries
UserToCarFavoritesModel.find({favoritedUsername: req.params.username}, function(err, relations)
{
// get a list of IDs of all cars the user likes from the relations, and find all entries in car model that match the IDs
result = relations.map(function(item){return item.edmundsID});
res.json(result);
});
});
// get the users that have favorited a given car
app.get("/getCarFavorites/:vehicleId", function (req, res){
UserToCarFavoritesModel.find({edmundsID: req.params.vehicleId}, function(err, relations)
{
// get a list of users who like the given car
UserModel.find({username: {$in: relations.map(function(item){ return item.favoritedUsername})}}, function(err, users){
// return all matches
res.json(users);
});
});
});
// add user1 to user2's followers
app.post("/follow/:username1/:username2", function(req, res){
var newFollow = new UserToUserFollowModel({followerUsername: req.params.username1, followingUsername: req.params.username2});
newFollow.save();
res.send(200);
});
// unfollow user1 from user2's followers
app.post("/unfollow/:username1/:username2", function(req, res){
UserToUserFollowModel.remove({followerUsername : req.params.username1, followingUsername: req.params.username2}, function (err, response){
});
res.send(200);
});
// retrieve followers of given user
app.get("/getFollowers/:username", function(req, res){
UserToUserFollowModel.find({followingUsername: req.params.username}, function (err, followers){
// get a list of users who like the given car
UserModel.find({username: {$in: followers.map(function(item){ return item.followerUsername})}}, function(err, users){
// return all user matches
res.json(users);
});
});
});
// retrieve the users followed by a given user
app.get("/getFollowing/:username", function(req, res){
UserToUserFollowModel.find({followerUsername: req.params.username}, function (err, following){
// get a list of users who like the given car
UserModel.find({username: {$in: following.map(function(item){ return item.followingUsername})}}, function(err, users){
// return all user matches
res.json(users);
});
});
});
// add a comment
app.post("/submitVehicleComment/:username/:vehicleId/:comment", function(req, res){
var newComment = new UserToCarCommentModel({username: req.params.username, edmundsID: req.params.vehicleId, comment: req.params.comment});
newComment.save();
res.send(200);
});
// add username1's comment to username2's profile
app.post("/submitUserComment/:username1/:username2/:comment", function(req, res){
var newComment = new UserToUserCommentModel({username: req.params.username2, commenterUsername: req.params.username1, comment: req.params.comment});
newComment.save();
res.send(200);
});
// retrieve all the comments on a given user's profile
app.get("/getProfileComments/:username", function(req, res){
UserToUserCommentModel.find({username: req.params.username}, function (err, comments){
res.json(comments);
});
});
// retrieve all the comments on a given vehicle's detail page
app.get("/getVehicleComments/:vehicleId", function(req, res){
UserToCarCommentModel.find({edmundsID: req.params.vehicleId}, function (err, comments){
res.json(comments);
});
});
// retrieve all comments a user has made on vehicles
app.get("/getAllUserVehicleComments/:username", function (req, res){
UserToCarCommentModel.find({username: req.params.username}, function (err, comments){
res.json(comments);
})
})
// retrieve all comments a user has made on profiles
app.get("/getAllUserProfileComments/:username", function (req, res){
UserToUserCommentModel.find({commenterUsername : req.params.username}, function (err, comments){
res.json(comments);
})
})
/*
// represent a user/s comment on a user's profile
var UserToUserCommentSchema = new mongoose.Schema({
username: String,
commenterUsername: String,
comment: String,
date: { type: Date, default: Date.now }
});
// represent a user's comment on a car
var UserToCarCommentSchema = new mongoose.Schema({
username: String,
edmundsID: String
comment: String,
date: { type: Date, default: Date.now }
});
// represent a user following another user
var UserToUserSchema = new mongoose.Schema({
followerUsername: String,
followingUsername: String
});
*/
/*)
// information for a user
var UserSchema = new mongoose.Schema({
username: String,
password: String,
});
// car info for database so we avoid making API calls for just the vehicle name
var CarSchema = new mongoose.Schema({
edmundsID: Number,
vehicleName: String,
modelYear: Number
});
var UserToCarFavoritesSchema = new mongoose.Schema({
favoritedUsername: String,
edmundsID: Number
});
*/
// get processes running
app.get('/process', function(req, res) {
res.json(process.env);
});
// define IP and port to listen on
var ip = process.env.OPENSHIFT_NODEJS_IP || '127.0.0.1';
var port = process.env.OPENSHIFT_NODEJS_PORT || 3000;
app.listen(port, ip);