-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 9f84c74
Showing
36 changed files
with
4,588 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.env | ||
node_modules/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
if (process.env.NODE_ENV != "production") { | ||
require("dotenv").config(); | ||
} | ||
const express = require("express"); | ||
const app = express(); | ||
const mongoose = require("mongoose"); | ||
const path = require("path"); | ||
const methodOverride = require("method-override"); | ||
const ejsmate = require("ejs-mate"); | ||
const ExpressError = require("./utils/ExpressError.js"); | ||
const session = require("express-session"); | ||
const MongoStore = require("connect-mongo"); | ||
const flash = require("connect-flash"); | ||
const passport = require("passport"); | ||
const LocalStrategy = require("passport-local"); | ||
const User = require("./models/user.js"); | ||
|
||
const listingRouter = require("./routes/listing.js"); | ||
const reviewRouter = require("./routes/review.js"); | ||
const userRouter = require("./routes/user.js"); | ||
|
||
const dbUrl = process.env.ATLASDB_URL; | ||
|
||
async function main() { | ||
await mongoose.connect(dbUrl); | ||
} | ||
main() | ||
.then(() => { | ||
console.log("connection successful"); | ||
}) | ||
.catch((err) => console.log(err)); | ||
|
||
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"))); | ||
|
||
const store = MongoStore.create({ | ||
mongoUrl: dbUrl, | ||
crypto: { | ||
secret: process.env.SECRET, | ||
}, | ||
touchAfter: 24 * 60 * 60, | ||
}); | ||
|
||
store.on("error", () => { | ||
console.log("ERROR in MONGO SESSION STORE", err); | ||
}); | ||
|
||
const sessionOption = { | ||
store, | ||
secret: process.env.SECRET, | ||
resave: false, | ||
saveUninitialized: true, | ||
cookie: { | ||
expires: Date.now() + 1000 * 60 * 60 * 24 * 7, | ||
maxAge: 1000 * 60 * 60 * 24 * 7, | ||
httpOnly: true, | ||
}, | ||
}; | ||
|
||
// app.get("/", (req, res) => { | ||
// res.send("Welcome"); | ||
// }); | ||
|
||
app.use(session(sessionOption)); | ||
app.use(flash()); //use before setting routes | ||
|
||
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.curUser = req.user; | ||
next(); | ||
}); | ||
|
||
app.use("/listings", listingRouter); | ||
app.use("/listings/:id/reviews", reviewRouter); | ||
app.use("/", userRouter); | ||
|
||
app.all("*", (req, res, next) => { | ||
next(new ExpressError(404, "PAGE NOT FOUND!!!")); | ||
}); | ||
|
||
app.use((err, req, res, next) => { | ||
let { status = 500, message = "Something Went Wrong!!!" } = err; | ||
res.status(status).render("error.ejs", { message }); | ||
// res.status(status).send(message); | ||
}); | ||
app.listen(8080, () => { | ||
console.log("Listening on 8080"); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
const cloudinary = require("cloudinary").v2; | ||
const { CloudinaryStorage } = require("multer-storage-cloudinary"); | ||
|
||
cloudinary.config({ | ||
cloud_name: process.env.CLOUD_NAME, | ||
api_key: process.env.CLOUD_API_KEY, | ||
api_secret: process.env.CLOUD_API_SECRET, | ||
}); | ||
|
||
const storage = new CloudinaryStorage({ | ||
cloudinary: cloudinary, | ||
params: { | ||
folder: "wanderlust_DEV", | ||
allowedFormats: ["png", "jpg", "jpeg"], | ||
}, | ||
}); | ||
|
||
module.exports = { | ||
cloudinary, | ||
storage, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
const Listing = require("../models/listing"); | ||
|
||
module.exports.index = async (req, res) => { | ||
const allListings = await Listing.find({}); | ||
res.render("listings/index.ejs", { allListings }); | ||
}; | ||
|
||
module.exports.renderNewForm = (req, res) => { | ||
res.render("listings/newForm.ejs"); | ||
}; | ||
|
||
module.exports.show = 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", "No Listing Found!"); | ||
res.redirect("/listings"); | ||
} | ||
res.render("listings/show.ejs", { listing }); | ||
}; | ||
|
||
module.exports.create = async (req, res, next) => { | ||
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 }; | ||
await newListing.save(); | ||
|
||
req.flash("success", "New Listing Created!"); | ||
res.redirect("/listings"); | ||
}; | ||
|
||
module.exports.edit = async (req, res) => { | ||
let { id } = req.params; | ||
const listing = await Listing.findById(id); | ||
if (!listing) { | ||
req.flash("error", "No Listing Found!"); | ||
res.redirect("/listings"); | ||
} | ||
let img = listing.image.url; | ||
let newImg = img.replace("/upload", "/upload/w_250"); | ||
res.render("listings/edit.ejs", { listing, newImg }); | ||
}; | ||
|
||
module.exports.update = async (req, res) => { | ||
let { id } = req.params; | ||
let listing = await Listing.findByIdAndUpdate(id, { ...req.body.listing }); | ||
if (typeof req.file !== "undefined") { | ||
let url = req.file.path; | ||
let filename = req.file.filename; | ||
listing.image = { url, filename }; | ||
await listing.save(); | ||
} | ||
|
||
req.flash("success", "Listing Updated!"); | ||
res.redirect(`/listings/${id}`); | ||
}; | ||
|
||
module.exports.delete = async (req, res) => { | ||
let { id } = req.params; | ||
await Listing.findByIdAndDelete(id); | ||
req.flash("success", "Listing Deleted!"); | ||
res.redirect("/listings"); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
const Review = require("../models/review.js"); | ||
const Listing = require("../models/listing.js"); | ||
|
||
module.exports.create = 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}`); | ||
}; | ||
|
||
module.exports.delete = 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}`); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
const User = require("../models/user.js"); | ||
|
||
|
||
module.exports.renderSignup = (req, res) => { | ||
res.render("users/signup.ejs"); | ||
}; | ||
|
||
module.exports.renderLogin = (req, res) => { | ||
res.render("users/login.ejs"); | ||
}; | ||
|
||
module.exports.signupUser = async (req, res, next) => { | ||
try { | ||
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", "YOU HAVE REGISTERED!"); | ||
res.redirect("/listings"); | ||
}); | ||
} catch (err) { | ||
req.flash("error", err.message); | ||
res.redirect("/signup"); | ||
} | ||
}; | ||
|
||
module.exports.loginUser = async (req, res) => { | ||
req.flash("success", "LOGIN SUCCESSFULLY!"); | ||
let redirectUrl = res.locals.redirectUrl || "/listings"; | ||
res.redirect(redirectUrl); | ||
}; | ||
|
||
module.exports.logout = (req, res, next) => { | ||
req.logout((err) => { | ||
if (err) { | ||
return next(err); | ||
} | ||
req.flash("success", "Logged out SUCCESSFULLY!!"); | ||
res.redirect("/listings"); | ||
}); | ||
}; |
Oops, something went wrong.