-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
327 lines (289 loc) · 10.3 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
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
var express = require("express"),
app = express(),
dotenv = require("dotenv").load(),
bodyParser = require("body-parser"),
methodOverride = require("method-override"),
db = require("./models"),
session = require("cookie-session"),
loginMiddleware = require("./middleware/loginHelper"),
routeMiddleware = require("./middleware/routeHelper"),
morgan = require("morgan");
userFavs = require("./getUserFavs");
app.use(morgan('tiny'));
app.set('view engine', 'ejs');
app.use(methodOverride('_method'));
app.use(express.static(__dirname + '/public'));
app.use(bodyParser.urlencoded({extended:true}));
app.use(loginMiddleware);
app.use(userFavs);
var yelp = require("yelp").createClient({
consumer_key: process.env.CONSUMER_KEY,
consumer_secret: process.env.CONSUMER_SECRET,
token: process.env.TOKEN,
token_secret: process.env.TOKEN_SECRET
});
app.use(session({
maxAge: 360000,
secret: 'whoknows',
name: "tcho chocolate"
}));
app.use(loginMiddleware);
//Root Dir & landing page
app.get('/', function(req, res){ //do not need routeMiddleware.preventLoginSignup bc the only login is a POST, user will be authenticated (or not) at this point
//need to add logic for object
var myObj = {errorStr: undefined};
var pic = "images/getsmitten.jpg";
res.render("landingPage", {errorStr: undefined, pic:pic});
});
//----------- USER ROUTES -------------------//
//Login
app.post('/login', function(req, res) { //do not need middleware bc of authenticate
db.User.authenticate(req.body.user,
function(err, user){
if (err) {
var pic = "images/getsmitten.jpg";
res.render("landingPage", {errorStr: err, pic:pic});
} else if(!err && user !== null) {
if (user.userImage === null) {
user.userImage = "images/user-default.png";
}
req.login(user);
//WHY WON'T THIS PRINT ON LOGIN?
console.log(user);
res.redirect("/users/" + user._id); //here we are stating that it is clearly a mongo id - should i update to a session id
} else {
var pic = "images/getsmitten.jpg";
res.render("landingPage", {errorStr: "Error: Could not authenticate", pic:pic});
}
});
});
//Signup - show sign up form
app.get('/signup', function(req, res){
var signUpPic = "images/Robyn_Smitten.jpeg";
res.render("users/signup", {signUpPic:signUpPic});
});
//CREATE User -- Send data to server
app.post('/signup', function(req, res){
var newUser = req.body.user; //"user" is from ejs
db.User.create(newUser, function(err, savedUser){ //savedUser refers to the new user created and saved. save has a callback function with (err and newUser)
//create saves to database
//savedUser is what gets created from the save function (save function is within the create function)
if (err) {
console.log(err);
} else if (savedUser){
if(newUser.userImage === null){
newUser.userImage = "images/user-default.png";
}
req.login(savedUser); //user is now logged in, we have access to req.session.id
res.redirect('/users/' + req.session.id);
}
else {
console.log("something else happend");
}
});
});
//------------------- USER SHOW - Profile Page -------------------------------//
app.get('/users/:user_id', routeMiddleware.ensureLoggedIn, function(req, res){
//within User collection, find a user with user_id and store to oneUser
//ensure user exists
db.User.findById(req.params.user_id, function(err, oneUser){
var tempUserImg;
if (err) throw err;
//within Favorite collection, find key 'user' with property value of 'req.params.user_id'
db.Favorite.find({user: req.params.user_id},
//return favorites in an array and save to 'favoritesByUserId'
function(err, favoritesByUserId) {
if (err) throw err;
//where is the empty array 'favorites' coming from
//adding a property to our object. this property will only be available for the scope of this function.
//the favorites property will create an association on the user, to access the favorites
oneUser.favorites = favoritesByUserId;
if(oneUser.userImage === ""){
tempUserImg = "images/user-default.png";
}
//console.log(favoritesByUserId);
//console.log("one favorite ", oneUser.favorites[0]);
db.Comment.find({user: req.params.user_id},
function(err, commentsByUserId) {
if (err) {
console.log(err);
res.render("errors/404");
}
else {
oneUser.favorites.forEach(function(favorite) {
commentsByUserId.forEach(function(comment) {
if (favorite.yelpBusId === comment.busId) {
favorite.comment = comment.content;
}
});
});
res.render('users/show', {oneUser:oneUser, id:req.session.id, tempUserImg:tempUserImg});
}
});
});
});
});
//------------------- USER EDIT -------------------------------//
app.get('/users/:user_id/edit', routeMiddleware.ensureLoggedIn, function(req, res){
db.User.findById(req.params.user_id,
function(err, user){
// console.log(req.session.id);
res.render("users/edit", {user:user, id: req.session.id});
});
});
//UPDATE User
app.put('/users/:user_id', routeMiddleware.ensureLoggedIn, function(req, res){
db.User.findByIdAndUpdate(req.params.user_id, req.body.user, function(err, user){
if(err){
if (err)throw err;
} else {
res.redirect("/users/" + req.session.id);
}
});
});
//LOGOUT
app.get('/users/:user_id/logout', function(req, res){
req.logout();
res.redirect("/");
});
//------------------- YELP SEARCH & RESULTS -------------------------------//
//Search
app.get('/users/:user_id/search', routeMiddleware.ensureLoggedIn, function(req, res){
var term = req.query.term;
yelp.search({
term: term,
location: req.query.location,
limit: 10
},
//find all where user_id == comments.user_id && comment.businessId == yelp.businessId
function(err, results){
if (err) {
console.log(err);
res.render("errors/404");
} else {
db.User.findById(req.session.id, function(err, foundUser){ //foundUser is an object
if (err) {
console.log(err);
res.render("errors/404");
} else {
// CALL FIND USER FAVS
// maybe all i really wanted was more middleware similar to loginHelper?
app.get("/getUserFavs/");
var favs = findFavsForResults({user: req.params.user_id}, results, function(resultsDataArg){
console.log(resultsDataArg);
res.render("search/results", {results:results, foundUser:foundUser, term:term, id:req.session.id, favs:favs});
});
} // else find user
}); // db.User.findById(.. {..
} // else results
} // search callback, params
); // search params
}); // app.get
// ---------------- REMOVED FIND USER FAVS (see getUserFavs.js) ----------------
function findFavsForResults(paramsUserId, results, annonFunc){
var trueArr = [];
db.Favorite.find(paramsUserId,
function(err, favoritesByUserId) {
if (err) {
console.log(err);
//res.render("errors/404");
} else {
results.businesses.forEach(function(business){
business.isFavorited = false;
favoritesByUserId.forEach(function(favorite){
if (business.id === favorite.yelpBusId){
business.isFavorited = true;
//trueArr.push(business.id);
}
});
});
} // end lse favoritesByUserId
annonFunc(results);
return results;
}
); // end db.Favorite.find(.. {..
}
// function resRender(route, data) {
// res.render(route, data);
// }
// function findCommsForResults(paramsUserId, results){
// db.Comment.find(paramsUserId,
// //question: can i search by session id only? would be safer, so you not just any number can be typed in?
// //answer: you should be checking for 'is logged in' at the route, thus if someone types in a random number they cannot access someone else's data
// //ansser: you could search by {user: req.params.user_id} but then you wouldn't need user_id in your route, and then your route wouldn't be restful
// function(err, commentsByUserId) {
// if(err){
// console.log(err);
// //res.render("errors/404");
// }
// else {
// results.businesses.forEach(function(business) {
// commentsByUserId.forEach(function(comment) {
// if (business.id === comment.busId) {
// business.comment = comment.content;
// }
// });
// });
// //res.render("search/results", {results:results, foundUser:foundUser, term:term, id:req.session.id});
// } //end else commentsByUserId
// return commentsByUserId;
// }
// );
// }
//---------------- Favorites -------------------------//
//INDEX -- Click "Favorites in Menu Bar" -- See all Favorites
app.get('/users/:user_id/favorites', routeMiddleware.ensureLoggedIn, function(req, res){
db.User.findById(req.params.user_id).exec(function(err, user){
if (err) throw err;
//user below refers to the key within the Favorite model
db.Favorite.find({user: req.params.user_id}, //can i do, find by id and just use req.params.user_id so that i get the entire object. i want access to user.name
function(err, favoritesByUserId){
if (err) throw err;
user.favorites = favoritesByUserId;
console.log(user.favorites[0]); //why doesn't fav have favImage or reivew count?
res.render('favorites/index', {user:user, id:req.session.id});
});
});
});
//CREATE favorite from AJAX
app.post('/users/:user_id/favorites', routeMiddleware.ensureLoggedIn, function (req, res){
var favDataObj = req.body.fav;
db.Favorite.create(favDataObj,
function (err, savedFav){
if (err) {
console.log("savedFav ERROR" + err);
} else {
res.json({savedFav:savedFav});
console.log("this is the saved fav" + savedFav);
}
});
});
//COMMENT - create comment from users/:user_id/search
app.post('/users/:user_id/comments', function (req, res){
var commDataObj = req.body.comm;
db.Comment.create(commDataObj,
function(err, savedComm){
if (err){
console.log("saved ERROR" + err);
} else {
res.json({savedComm:savedComm});
console.log("this is the saved comment" + savedComm);
}
});
});
// CATCH ALL
app.get('*', function(req,res){
res.render('errors/404');
});
// START SERVER
app.listen(process.env.PORT || 3000, function(){
"Server is listening";
});
/*
TO DO:
view more - results - results.ejs
implement user authority - app.js
fix err with url not clearing data
if favorite is clicked again, it will be removed from user's favorites
if click comment, leave comment field blank, then click save, 'comment' button should reappear
*/