-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
346 lines (244 loc) · 7.69 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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
if(process.env.NODE_ENV !="production"){
require("dotenv").config();
}
const express = require("express");
const app = express();
const mongoose = require("mongoose");
const Listing = require("./models/listing.js");
const path = require("path");
const methodOverride = require("method-override");
const ejsMate = require('ejs-mate');
const Review = require("./models/review.js");
// const MONGO_URL = "mongodb://127.0.0.1:27017/RentoBuddy";
const dbUrl = process.env.ATLASDB_URL;
const MongoStore = require('connect-mongo');
const flash = require("connect-flash");
const session = require('express-session');
const multer = require("multer");
const {storage} = require('./cloudConfig.js') ;
const upload = multer({storage});
const passport = require("passport");
const LocalStrategy = require("passport-local");
const User = require("./models/user.js");
const cookieParser = require("cookie-parser");
// const {listingSchema, reviewSchema} = require("./schema.js");
const mbxGeocoding = require('@mapbox/mapbox-sdk/services/geocoding');
const mapToken = process.env.MAP_TOKEN;
const geocodingClient = mbxGeocoding({ accessToken: mapToken });
main()
.then(() => {
console.log("connected to DB");
})
.catch((err) => {
console.log(err);
});
async function main() {
await mongoose.connect(dbUrl);
}
app.set("view engine", "ejs");
app.set("views", path.join(__dirname, "views"));
app.use(express.urlencoded({ extended: true }));
app.use(methodOverride("_method"));
app.engine('ejs', ejsMate);
app.use(express.static(path.join(__dirname, "/public")));
app.use(cookieParser());
app.use(session({secret: "mysupersecretstring"}));
const store = MongoStore.create({
mongoUrl: dbUrl,
crypto:{
secret:"mysupersecretcode"
},
touchAfter:24*3600,
});
store.on("error",()=>{
console.log("SESSION STORE ERROR")
});
const sessionOption = {
store,
secret:"mysupersecretcode",
resave: false,
saveUninitialized:true,
cookie:{
expires: Date.now() + 7*24*60*60*1000,
maxAge:7*24*60*60*1000,
httpOnly:true,
},
};
const productSchema = new mongoose.Schema({
name: String,
location : String,
country : String,
});
const Product = mongoose.model('Product', productSchema);
// app.get('/', (req, res) => {
// res.render('main'); // Render the EJS main page
// });
app.get('/search', async (req, res) => {
const searchQuery = req.query.searchQuery;
// Use the `searchQuery` to filter listings from your database
// Example MongoDB Mongoose query:
const filteredListings = await Listing.find({ location: { $regex: searchQuery, $options: 'i' } });
res.render('listings/index.ejs', { allListings: filteredListings, searchLocation: searchQuery });
});
app.use(session(sessionOption));
app.use(flash());
app.use(passport.initialize());
app.use(passport.session());
passport.use(new LocalStrategy(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
app.use((req,res, next)=>{
res.locals.success = req.flash("success");
res.locals.error = req.flash("error");
res.locals.currUser = req.user;
next();
});
app.get("/signup",async (req,res)=>{
res.render("users/signup.ejs");
});
app.post("/signup",async (req,res)=>{
let{username, email, password} = req.body;
const newUser = new User({email, username});
const registeredUser = await User.register(newUser, password);
req.login(registeredUser, (err)=>{
if(err){
return next(err);
}
req.flash("success","Welcome to RentoBuddy")
res.redirect("/");
})
});
app.get("/login",async (req,res)=>{
res.render("users/login.ejs");
});
app.post("/login", passport.authenticate('local', { failureRedirect: '/login', failureFlash:true, }),async(req,res)=>{
req.flash("success", "wellcomeback to RentoBuddy")
res.redirect("/");
});
app.get("/logout", (req,res ,next)=>{
req.logOut((err)=>{
if(err){
return next(err)
}
req.flash("success","you are logged out!");
res.redirect("/");
})
});
// app.get("/", (req, res) => {
// res.send("Hi, I am root");
// });
//Index Route
app.get("/", async (req, res) => {
const allListings = await Listing.find({});
res.render("listings/index.ejs", { allListings });
});
app.get("/privacyPolicy", async (req , res)=>{
res.render("includes/privacyPolicy.ejs");
})
app.get("/contact-us", async (req , res)=>{
res.render("includes/contactUs.ejs");
})
app.get("/about-us", async (req , res)=>{
res.render("includes/aboutUs.ejs");
})
//New Route
app.get("/listings/new", (req, res) => {
if(!req.isAuthenticated()){
req.flash("error","You must be logged in to create listings.");
return res.redirect("/login");
}
res.render("listings/new.ejs");
});
//Show Route
app.get("/listings/:id", async (req, res) => {
let { id } = req.params;
const listing = await Listing.findById(id).populate({path: "reviews", populate:{
path:"author",
}}).populate("owner");
if(!listing){
req.flash("error","Listing you requested for does not found");
res.redirect("/listings");
}
console.log(listing );
res.render("listings/show.ejs", { listing });
});
//Create Route
app.post("/", upload.single('listing[image]') , async (req, res) => {
let response = await geocodingClient.forwardGeocode({
query: req.body.listing.location,
limit: 2
})
.send();
let url = req.file.path;
let filename = req.file.filename;
const newListing = new Listing(req.body.listing);
newListing.owner = req.user._id;
newListing.image ={url , filename};
newListing.geometry = response.body.features[0].geometry;
let savedListing = newListing.save();
console.log(savedListing);
req.flash("success", "New Listing Created!");
res.redirect("/");
// res.send(req.file);
});
//Edit Route
app.get("/listings/:id/edit", async (req, res) => {
if(!req.isAuthenticated()){
req.flash("error","You must be logged in to create listings.");
return res.redirect("/login");
}
let { id } = req.params;
const listing = await Listing.findById(id);
res.render("listings/edit.ejs", { listing });
});
//Update Route
app.put("/listings/:id", upload.single('listing[image]') , async (req, res) => {
if(!req.isAuthenticated()){
req.flash("error","You must be logged in to create listings.");
return res.redirect("/login");
}
let { id } = req.params;
let listing = await Listing.findByIdAndUpdate(id, { ...req.body.listing });
if(typeof req.file != "undefine"){
let url = req.file.path;
let filename = req.file.filename;
listing.image ={url , filename};
await listing.save();
}
res.redirect(`/listings/${id}`);
});
//Delete Route
app.delete("/listings/:id", async (req, res) => {
if(!req.isAuthenticated()){
req.flash("error","You must be logged in to create listings.");
return res.redirect("/login");
}
let { id } = req.params;
let deletedListing = await Listing.findByIdAndDelete(id);
console.log(deletedListing);
req.flash("success", "Listing Deleted");
res.redirect("/");
});
//Reviews
//Post Route
app.post("/listings/:id/reviews", async(req,res)=>{
let listing = await Listing.findById(req.params.id);
let newReview = new Review(req.body.review);
newReview.author = req.user._id;
listing.reviews.push(newReview);
await newReview.save();
await listing.save();
req.flash("success", "New review Created");
res.redirect(`/listings/${listing._id}`);
});
//DELETE ROUTE
app.delete("/listings/:id/reviews/:reviewId", async(req,res)=>{
let {id, reviewId} = req.params;
await Listing.findByIdAndUpdate(id , {$pull:{reviews: reviewId}});
await Review.findByIdAndDelete(reviewId);
req.flash("success", "Review Deleted");
res.redirect(`/listings/${id}`);
})
app.listen(8080, () => {
console.log("server is listening to port 8080");
});