├── views ├── partials │ ├── footer.ejs │ └── header.ejs ├── comments │ ├── new.ejs │ └── edit.ejs ├── login.ejs ├── register.ejs ├── landing.ejs └── campgrounds │ ├── index.ejs │ ├── new.ejs │ ├── edit.ejs │ └── show.ejs ├── README.md ├── models ├── user.js ├── comment.js └── campground.js ├── public ├── stylesheets │ ├── main.css │ └── landing.css └── scripts │ └── search.js ├── package.json ├── routes ├── index.js ├── comments.js └── campgrounds.js ├── app.js ├── middleware └── index.js └── seeds.js /views/partials/footer.ejs: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Travel-Camp 2 | Travel-Camp project 3 | This project build a website of camping activities with Node.js. 4 | The link is: https://travel-camp.herokuapp.com 5 | -------------------------------------------------------------------------------- /models/user.js: -------------------------------------------------------------------------------- 1 | var mongoose = require("mongoose"); 2 | var passportLocalMongoose = require("passport-local-mongoose"); 3 | 4 | var UserSchema = new mongoose.Schema({ 5 | username: String, 6 | password: String 7 | }); 8 | 9 | UserSchema.plugin(passportLocalMongoose); 10 | 11 | module.exports = mongoose.model("User", UserSchema); -------------------------------------------------------------------------------- /models/comment.js: -------------------------------------------------------------------------------- 1 | var mongoose = require("mongoose"); 2 | 3 | var commentSchema = new mongoose.Schema({ 4 | text:String, 5 | author: { 6 | id: { 7 | type: mongoose.Schema.Types.ObjectId, 8 | ref: "User" 9 | }, 10 | username: String 11 | } 12 | }); 13 | 14 | module.exports = mongoose.model("Comment", commentSchema); -------------------------------------------------------------------------------- /public/stylesheets/main.css: -------------------------------------------------------------------------------- 1 | .thumbnail img { 2 | width: 100%; 3 | } 4 | 5 | .thumbnail { 6 | padding: 0; 7 | } 8 | 9 | .thumbnail .caption-full { 10 | padding-left: 9px; 11 | } 12 | 13 | .delete-form { 14 | display: inline; 15 | } 16 | 17 | .imagee { 18 | width: 100%; 19 | height: 200px; 20 | background-size: cover; 21 | background-position: center center; 22 | } 23 | 24 | #map { 25 | height: 400px; 26 | width: 100%; 27 | } 28 | 29 | .flex-wrap { 30 | display:flex; 31 | flex-wrap: wrap; 32 | } -------------------------------------------------------------------------------- /models/campground.js: -------------------------------------------------------------------------------- 1 | var mongoose = require("mongoose"); 2 | 3 | var campgroundSchema = new mongoose.Schema({ 4 | name:String, 5 | price: String, 6 | image:String, 7 | location: String, 8 | lat: Number, 9 | lng: Number, 10 | description:String, 11 | comments:[ 12 | { 13 | type: mongoose.Schema.Types.ObjectId, 14 | ref:"Comment" 15 | } 16 | ], 17 | author:{ 18 | id: { 19 | type: mongoose.Schema.Types.ObjectId, 20 | ref:"User" 21 | }, 22 | username: String 23 | } 24 | }); 25 | 26 | module.exports = mongoose.model("Campground", campgroundSchema); -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "yelpcamp", 3 | "version": "1.0.0", 4 | "description": "yelpcamp app", 5 | "main": "app.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1", 8 | "start": "node app.js" 9 | }, 10 | "author": "Funky Arui", 11 | "license": "ISC", 12 | "dependencies": { 13 | "body-parser": "^1.17.2", 14 | "connect-flash": "^0.1.1", 15 | "ejs": "^2.5.6", 16 | "express": "^4.15.3", 17 | "express-session": "^1.15.3", 18 | "geocoder": "^0.2.3", 19 | "method-override": "^2.3.9", 20 | "mongoose": "^4.10.8", 21 | "passport": "^0.3.2", 22 | "passport-local": "^1.0.0", 23 | "passport-local-mongoose": "^4.0.0" 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /views/comments/new.ejs: -------------------------------------------------------------------------------- 1 | <% include ../partials/header %> 2 |
3 |
4 |

Create a new Comment to <%= camp.name %>

5 |
6 |
7 |
8 | 9 |
10 |
11 | 12 |
13 |
14 | Go back 15 |
16 |
17 |
18 | <% include ../partials/footer %> 19 | -------------------------------------------------------------------------------- /views/comments/edit.ejs: -------------------------------------------------------------------------------- 1 | <% include ../partials/header %> 2 |
3 |
4 |

Edit Comment

5 |
6 |
7 |
8 | 9 |
10 |
11 | 12 |
13 |
14 | Go back 15 |
16 |
17 |
18 | <% include ../partials/footer %> 19 | 20 | -------------------------------------------------------------------------------- /views/login.ejs: -------------------------------------------------------------------------------- 1 | <% include ./partials/header %> 2 |
3 |
4 |

Login

5 |
6 |
7 |
8 | 9 |
10 |
11 | 12 |
13 |
14 | 15 |
16 |
17 | Go Back 18 |
19 |
20 |
21 | <% include ./partials/footer %> -------------------------------------------------------------------------------- /views/register.ejs: -------------------------------------------------------------------------------- 1 | <% include ./partials/header %> 2 |
3 |
4 |

Sign Up

5 |
6 |
7 |
8 | 9 |
10 |
11 | 12 |
13 |
14 | 15 |
16 |
17 | Go Back 18 |
19 |
20 |
21 | <% include ./partials/footer %> -------------------------------------------------------------------------------- /public/scripts/search.js: -------------------------------------------------------------------------------- 1 | $('#campground-search').on('input', function() { 2 | var search = $(this).serialize(); 3 | if(search === "search=") { 4 | search = "all" 5 | } 6 | $.get('/campgrounds?' + search, function(data) { 7 | $('#campground-grid').html(''); 8 | data.forEach(function(campground) { 9 | $('#campground-grid').append(` 10 |
11 |
12 |
13 |
14 |

${ campground.name }

15 |
16 |

17 | More Info 18 |

19 |
20 |
21 | `); 22 | }); 23 | }); 24 | }); 25 | 26 | $('#campground-search').submit(function(event) { 27 | event.preventDefault(); 28 | }); -------------------------------------------------------------------------------- /views/landing.ejs: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | YelpCamp 5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | <% if(error && error.length > 0){ %> 13 | 16 | <% } %> 17 | <% if(success && success.length > 0){ %> 18 | 21 | <% } %> 22 |
23 | 24 |
25 |

Welcome to YelpCamp!

26 | View All Campgrounds 27 |
28 | 29 | 36 | 37 | <% include partials/footer %> -------------------------------------------------------------------------------- /routes/index.js: -------------------------------------------------------------------------------- 1 | var express =require("express"); 2 | var passport = require("passport"); 3 | var router = express.Router(); 4 | var User = require("../models/user"); 5 | 6 | router.get("/", function(req, res) { 7 | res.render("landing"); 8 | }); 9 | 10 | router.get("/register", function(req, res) { 11 | res.render("register"); 12 | }); 13 | 14 | router.post("/register", function(req, res) { 15 | var newUser = {username:req.body.username}; 16 | User.register(newUser, req.body.password, function(err, user){ 17 | if(err) { 18 | req.flash("error", err.message); 19 | return res.redirect("/register"); 20 | } else { 21 | passport.authenticate("local")(req, res, function() { 22 | req.flash("success", "Welcome to YelpCamp, " + user.username); 23 | res.redirect("/campgrounds"); 24 | }); 25 | } 26 | }); 27 | }); 28 | 29 | // login form 30 | router.get("/login", function(req, res) { 31 | res.render("login"); 32 | }); 33 | 34 | router.post("/login", passport.authenticate("local", { 35 | successRedirect: "/campgrounds", 36 | failureRedirect: "/login", 37 | failureFlash: true, 38 | successFlash: 'Welcome to YelpCamp!' 39 | }), function(req, res) { 40 | }); 41 | 42 | //log out 43 | router.get("/logout", function(req, res) { 44 | req.logout(); 45 | req.flash("success", "Logged you out!"); 46 | res.redirect("/campgrounds"); 47 | }); 48 | 49 | module.exports = router; -------------------------------------------------------------------------------- /views/campgrounds/index.ejs: -------------------------------------------------------------------------------- 1 | <% include ../partials/header %> 2 | 3 |
4 |
5 |
6 |

Welcome to YelpCamp!

7 |
View all beautiful camps!
8 |

9 | Add new camp 10 |

11 |

12 |

17 |

18 |
19 |
20 | 21 |
22 | <% campgrounds.forEach(function(camp) { %> 23 |
24 |
25 |
26 |
27 |

<%= camp.name%>

28 |
29 |

30 | >More Info 31 |

32 |
33 |
34 | <% }); %> 35 |
36 |
37 | <% include ../partials/footer %> 38 | 39 | -------------------------------------------------------------------------------- /views/campgrounds/new.ejs: -------------------------------------------------------------------------------- 1 | <% include ../partials/header %> 2 |
3 |
4 |

Create a new Camp!

5 |
6 |
7 |
8 | 9 |
10 |
11 | 12 |
13 |
14 | 15 |
16 |
17 | 18 |
19 |
20 | 21 | 22 |
23 |
24 | 25 |
26 |
27 | Go back 28 |
29 |
30 |
31 | <% include ../partials/footer %> 32 | -------------------------------------------------------------------------------- /views/partials/header.ejs: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | TravelCamp 5 | 6 | 7 | 11 | 12 | 13 | 14 | 32 | 33 |
34 | <% if (error && error.length > 0) { %> 35 | 38 | <% } %> 39 | <% if (success && success.length > 0) { %> 40 | 43 | <% } %> 44 |
45 | -------------------------------------------------------------------------------- /views/campgrounds/edit.ejs: -------------------------------------------------------------------------------- 1 | <% include ../partials/header %> 2 |
3 |
4 |

Edit <%= camp.name %>

5 |
6 |
7 |
8 | 9 |
10 |
11 | 12 |
13 |
14 | 15 |
16 |
17 | 18 |
19 |
20 | 21 | 22 |
23 |
24 | 25 |
26 |
27 | Go back 28 |
29 |
30 |
31 | <% include ../partials/footer %> 32 | -------------------------------------------------------------------------------- /public/stylesheets/landing.css: -------------------------------------------------------------------------------- 1 | body { 2 | background-color: #000; 3 | } 4 | 5 | #landing-header { 6 | z-index: 1; 7 | position: relative; 8 | text-align: center; 9 | padding-top: 40vh; 10 | } 11 | 12 | #landing-header h1 { 13 | color: #fff; 14 | } 15 | 16 | .slideshow { 17 | position: fixed; 18 | width: 100%; 19 | height: 100%; 20 | top: 0; 21 | left: 0; 22 | z-index: 0; 23 | list-style: none; 24 | margin: 0; 25 | padding: 0; 26 | } 27 | 28 | .slideshow li { 29 | width: 100%; 30 | height: 100%; 31 | position: absolute; 32 | top: 0; 33 | left: 0; 34 | background-size: cover; 35 | background-position: 50% 50%; 36 | background-repeat: no-repeat; 37 | opacity: 0; 38 | z-index: 0; 39 | animation: imageAnimation 50s linear infinite; 40 | } 41 | 42 | .slideshow li:nth-child(1) { 43 | background-image: url(http://i.imgur.com/K3mPv14.jpg) 44 | } 45 | .slideshow li:nth-child(2) { 46 | background-image: url(http://i.imgur.com/SBEmFpv.jpg); 47 | animation-delay: 10s; 48 | } 49 | .slideshow li:nth-child(3) { 50 | background-image: url(http://i.imgur.com/emvhOnb.jpg); 51 | animation-delay: 20s; 52 | } 53 | .slideshow li:nth-child(4) { 54 | background-image: url(http://i.imgur.com/2LSMCmJ.jpg); 55 | animation-delay: 30s; 56 | } 57 | .slideshow li:nth-child(5) { 58 | background-image: url(http://i.imgur.com/TVGe0Ef.jpg); 59 | animation-delay: 40s; 60 | } 61 | 62 | @keyframes imageAnimation { 63 | 0% { 64 | opacity: 0; 65 | animation-timing-function: ease-in; 66 | } 67 | 10% { 68 | opacity: 1; 69 | animation-timing-function: ease-out; 70 | } 71 | 20% { 72 | opacity: 1 73 | } 74 | 30% { 75 | opacity: 0 76 | } 77 | } 78 | 79 | /* Older browser support - .no-cssanimations class added by modernizr */ 80 | .no-cssanimations .slideshow li { 81 | opacity: 1; 82 | } -------------------------------------------------------------------------------- /app.js: -------------------------------------------------------------------------------- 1 | var express = require("express"); 2 | var bodyParser = require("body-parser"); 3 | var mongoose = require("mongoose"); 4 | var seedDB = require("./seeds"); 5 | var passport = require("passport"); 6 | var LocalStrategy = require("passport-local"); 7 | var Campground = require("./models/campground"); 8 | var Comment = require("./models/comment"); 9 | var User = require("./models/user"); 10 | var methodOverride = require("method-override"); 11 | var flash = require("connect-flash"); 12 | var app = express(); 13 | 14 | 15 | // requiring routes 16 | var commentRoutes = require("./routes/comments"); 17 | var campgroundsRoutes = require("./routes/campgrounds"); 18 | var indexRoutes = require("./routes/index"); 19 | 20 | var url = process.env.DATABASEURL || "mongodb://localhost/yelp_camp_v9"; 21 | mongoose.connect(url); 22 | 23 | app.use(bodyParser.urlencoded({extended:true})); 24 | app.set("view engine", "ejs"); 25 | app.use(express.static(__dirname+"/public")); 26 | app.use(methodOverride("_method")); 27 | app.use(flash()); 28 | // seedDB(); 29 | 30 | //passport configuration 31 | app.use(require("express-session")({ 32 | secret: "Winter is Coming!", 33 | resave: false, 34 | saveUninitialized: false 35 | })); 36 | app.use(passport.initialize()); 37 | app.use(passport.session()); 38 | passport.use(new LocalStrategy(User.authenticate())); 39 | passport.serializeUser(User.serializeUser()); 40 | passport.deserializeUser(User.deserializeUser()); 41 | 42 | app.use(function(req, res, next){ 43 | res.locals.currentUser = req.user; 44 | res.locals.error = req.flash("error"); 45 | res.locals.success = req.flash("success"); 46 | next(); 47 | }); 48 | 49 | app.use(indexRoutes); 50 | app.use(campgroundsRoutes); 51 | app.use(commentRoutes); 52 | 53 | app.listen(process.env.PORT, process.env.IP, function() { 54 | console.log("Server Started!"); 55 | }); -------------------------------------------------------------------------------- /middleware/index.js: -------------------------------------------------------------------------------- 1 | var Comment = require("../models/comment"); 2 | var Campground = require("../models/campground"); 3 | var middlewareObj = {}; 4 | 5 | middlewareObj.isLoggedIn = function (req, res, next){ 6 | if(req.isAuthenticated()) { 7 | return next(); 8 | } else { 9 | req.flash("error", "You need to be logged in to do that!"); 10 | res.redirect("/login"); 11 | } 12 | }; 13 | 14 | middlewareObj.checkCommentOwnership = function(req, res, next) { 15 | if (req.isAuthenticated()) { 16 | Comment.findById(req.params.comment_id, function(err, foundComment) { 17 | if(err) { 18 | res.redirect("back"); 19 | } else { 20 | if (foundComment.author.id.equals(req.user._id)) { 21 | next(); 22 | } else { 23 | req.flash("error", "You don't have permission to do that"); 24 | res.redirect("back"); 25 | } 26 | } 27 | }); 28 | } else { 29 | req.flash("error", "You need to be logged in to do that"); 30 | res.redirect("back"); 31 | } 32 | }; 33 | 34 | middlewareObj.checkCampgroundOwnership = function(req, res, next) { 35 | if (req.isAuthenticated()) { 36 | Campground.findById(req.params.id, function(err, foundCampground) { 37 | if(err) { 38 | req.flash("error","Campground not found"); 39 | res.redirect("back"); 40 | } else { 41 | if (foundCampground.author.id.equals(req.user._id)) { 42 | next(); 43 | } else { 44 | req.flash("error","You don't have permission to do that"); 45 | res.redirect("back"); 46 | } 47 | } 48 | }); 49 | } else { 50 | req.flash("error", "You need to be logged in to do that"); 51 | res.redirect("back"); 52 | } 53 | }; 54 | 55 | module.exports = middlewareObj; -------------------------------------------------------------------------------- /seeds.js: -------------------------------------------------------------------------------- 1 | var Campground = require("./models/campground"); 2 | var Comment = require("./models/comment"); 3 | var User = require("./models/user"); 4 | 5 | // var admin = { 6 | // username: "admin", 7 | // password: "12345" 8 | // } 9 | var seeds = [ 10 | { 11 | name: "West Wolf", 12 | image:"https://farm4.staticflickr.com/3270/2617191414_c5d8a25a94.jpg", 13 | description:"blah blah blah" 14 | }, 15 | { 16 | name: "East Wood", 17 | image:"https://farm4.staticflickr.com/3872/14435096036_39db8f04bc.jpg", 18 | description:"blah blah blah" 19 | }, 20 | { 21 | name: "North Banana", 22 | image:"https://farm2.staticflickr.com/1281/4684194306_18ebcdb01c.jpg", 23 | description:"blah blah blah" 24 | }, 25 | { 26 | name: "South Beauty", 27 | image:"https://farm2.staticflickr.com/1363/1342367857_2fd12531e7.jpg", 28 | description:"blah blah blah" 29 | } 30 | ] 31 | 32 | function seedDB() { 33 | // User.create(admin, function(err) { 34 | // if (err) { 35 | // console.log(err); 36 | // } 37 | // }); 38 | Comment.remove({}, function(err) { 39 | 40 | }); 41 | User.remove({}, function(err) { 42 | 43 | }); 44 | Campground.remove({}, function(err) { 45 | // if (err) { 46 | // console.log(err); 47 | // } else { 48 | // console.log("removed campgrounds"); 49 | // } 50 | // seeds.forEach(function(seed) { 51 | // Campground.create(seed, function(err, camp){ 52 | // if(err) { 53 | // console.log(err); 54 | // } else { 55 | // console.log("added a camp!"); 56 | // Comment.create( 57 | // { 58 | // text: "Good camp! But without any internet, maybe I'm too greedy:)", 59 | // author:"Arui" 60 | // }, function(err, comment) { 61 | // if(err) { 62 | // console.log(err); 63 | // } else { 64 | // camp.comments.push(comment); 65 | // camp.save(); 66 | // console.log("Added comment to a camp!"); 67 | // } 68 | // }); 69 | // } 70 | // }); 71 | // }); 72 | }); 73 | }; 74 | 75 | module.exports = seedDB; -------------------------------------------------------------------------------- /routes/comments.js: -------------------------------------------------------------------------------- 1 | var express =require("express"); 2 | var router = express.Router(); 3 | var Campground = require("../models/campground"); 4 | var Comment = require("../models/comment"); 5 | var middleware = require("../middleware"); 6 | 7 | router.get("/campgrounds/:id/comments/new", middleware.isLoggedIn, function(req, res) { 8 | Campground.findById(req.params.id, function(err, camp){ 9 | if(err){ 10 | console.log(err); 11 | } else { 12 | res.render("comments/new", {camp: camp}); 13 | } 14 | }); 15 | }); 16 | 17 | router.post("/campgrounds/:id/comments", middleware.isLoggedIn, function(req, res) { 18 | Campground.findById(req.params.id, function(err, camp) { 19 | if(err) { 20 | console.log(err); 21 | res.redirect("/campgrounds"); 22 | } else { 23 | Comment.create(req.body.comment, function(err, comment){ 24 | if(err) { 25 | req.flash("error", "Something went wrong"); 26 | console.log(err); 27 | } else { 28 | comment.author.id = req.user.id; 29 | comment.author.username = req.user.username; 30 | comment.save(); 31 | camp.comments.push(comment); 32 | camp.save(); 33 | console.log("comment added successfully"); 34 | req.flash("success", "Successfully added comment"); 35 | res.redirect("/campgrounds" + '/' + req.params.id); 36 | } 37 | }); 38 | } 39 | }); 40 | }); 41 | 42 | // edit the comment 43 | router.get("/campgrounds/:id/comments/:comment_id/edit", middleware.checkCommentOwnership, function(req, res) { 44 | Comment.findById(req.params.comment_id, function(err, foundComment) { 45 | if(err) { 46 | res.redirect("back"); 47 | } else { 48 | res.render("comments/edit", {comment: foundComment, camp_id: req.params.id}); 49 | } 50 | }); 51 | }); 52 | 53 | // update 54 | router.put("/campgrounds/:id/comments/:comment_id", middleware.checkCommentOwnership, function(req, res) { 55 | Comment.findByIdAndUpdate(req.params.comment_id, req.body.comment, function(err, updatedComment) { 56 | if (err) { 57 | console.log(err); 58 | res.redirect("back"); 59 | } else { 60 | res.redirect("/campgrounds/" + req.params.id); 61 | } 62 | }); 63 | }); 64 | 65 | // destory 66 | router.delete("/campgrounds/:id/comments/:comment_id", middleware.checkCommentOwnership, function(req, res) { 67 | Comment.findByIdAndRemove(req.params.comment_id, function(err) { 68 | if(err) { 69 | console.log(err); 70 | res.redirect("back"); 71 | } else { 72 | req.flash("success", "Comment deleted"); 73 | res.redirect("/campgrounds/" + req.params.id); 74 | } 75 | }); 76 | }); 77 | 78 | module.exports = router; 79 | -------------------------------------------------------------------------------- /views/campgrounds/show.ejs: -------------------------------------------------------------------------------- 1 | <% include ../partials/header %> 2 |
3 |
4 |
5 |

YelpCamp

6 |
7 |
  • Info1
  • 8 |
  • Info2
  • 9 |
  • Info3
  • 10 |
    11 |
    12 |
    13 |
    14 |
    15 | > 16 |
    17 |

    $<%= campground.price %>/night

    18 |

    <%= campground.name %>

    19 |

    <%= campground.description %>

    20 |

    21 | Submitted by <%= campground.author.username%> 22 |

    23 | <% if (currentUser && campground.author.id.equals(currentUser._id)) {%> 24 | Edit 25 |
    26 | 27 |
    28 | <% } %> 29 |
    30 |
    31 |
    32 |
    33 | Add new comment 34 |
    35 |
    36 | <% campground.comments.forEach(function(comment) { %> 37 |
    38 |
    39 | <%= comment.author.username%> 40 | 10 days ago 41 |

    42 | <%= comment.text %> 43 |

    44 | <% if (currentUser && comment.author.id.equals(currentUser._id)) {%> 45 | Edit 46 |
    47 | 48 |
    49 | <% } %> 50 |
    51 |
    52 | <% }) %> 53 |
    54 |
    55 |
    56 |
    57 | 58 | <% include ../partials/footer %> 59 | 60 | 87 | 88 | 89 | -------------------------------------------------------------------------------- /routes/campgrounds.js: -------------------------------------------------------------------------------- 1 | var express =require("express"); 2 | var router = express.Router(); 3 | var Campground = require("../models/campground"); 4 | var middleware = require("../middleware"); 5 | var geocoder = require("geocoder"); 6 | 7 | function escapeRegex(text) { 8 | return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"); 9 | }; 10 | 11 | 12 | router.get("/campgrounds",function(req, res) { 13 | // Campground.find({},function(err, allCampgrounds){ 14 | // if (err) { 15 | // console.log(err); 16 | // } else { 17 | // res.render("campgrounds/index", {campgrounds: allCampgrounds}); 18 | // } 19 | // }) 20 | if(req.query.search && req.xhr) { 21 | const regex = new RegExp(escapeRegex(req.query.search), 'gi'); 22 | // Get all campgrounds from DB 23 | Campground.find({name: regex}, function(err, allCampgrounds){ 24 | if(err){ 25 | console.log(err); 26 | } else { 27 | res.status(200).json(allCampgrounds); 28 | // res.status(200).render("campgrounds/index", {campgrounds: allCampgrounds, page: 'campgrounds'}); 29 | } 30 | }); 31 | } else { 32 | // Get all campgrounds from DB 33 | Campground.find({}, function(err, allCampgrounds){ 34 | if(err){ 35 | console.log(err); 36 | } else { 37 | if(req.xhr) { 38 | res.json(allCampgrounds); 39 | } else { 40 | res.render("campgrounds/index",{campgrounds: allCampgrounds, page: 'campgrounds'}); 41 | } 42 | } 43 | }); 44 | } 45 | }); 46 | 47 | router.post("/campgrounds", middleware.isLoggedIn, function(req, res) { 48 | var name = req.body.campName; 49 | var url = req.body.campUrl; 50 | var desc = req.body.campDesc; 51 | var price = req.body.campPrice; 52 | var author = { 53 | id: req.user._id, 54 | username: req.user.username 55 | }; 56 | geocoder.geocode(req.body.location, function (err, data) { 57 | var lat = data.results[0].geometry.location.lat; 58 | var lng = data.results[0].geometry.location.lng; 59 | var location = data.results[0].formatted_address; 60 | var newCamp = {name: name, image: url, description: desc, price: price, author:author, location: location, lat: lat, lng: lng}; 61 | Campground.create(newCamp, function(err, Camp) { 62 | if (err) { 63 | console.log(err); 64 | } else { 65 | res.redirect("/campgrounds"); 66 | } 67 | }); 68 | }) 69 | }); 70 | 71 | router.get("/campgrounds/new", middleware.isLoggedIn, function(req, res) { 72 | res.render("campgrounds/new"); 73 | }); 74 | 75 | router.get("/campgrounds/:id", function(req, res){ 76 | Campground.findById(req.params.id).populate("comments").exec(function(err, foundCampground) { 77 | if(err) { 78 | console.log(err); 79 | } else { 80 | res.render("campgrounds/show", {campground:foundCampground}); 81 | } 82 | }); 83 | }); 84 | 85 | //edit 86 | router.get("/campgrounds/:id/edit", middleware.checkCampgroundOwnership, function(req, res) { 87 | Campground.findById(req.params.id, function(err, foundCampground){ 88 | res.render("campgrounds/edit", {camp: foundCampground}); 89 | }); 90 | }); 91 | 92 | // deal with the put request 93 | router.put("/campgrounds/:id", middleware.checkCampgroundOwnership, function(req, res) { 94 | geocoder.geocode(req.body.editedCamp.location, function (err, data) { 95 | var lat = data.results[0].geometry.location.lat; 96 | var lng = data.results[0].geometry.location.lng; 97 | var editedCamp = req.body.editedCamp; 98 | editedCamp.lat = lat; 99 | editedCamp.lng = lng; 100 | console.log(data.results[0].geometry.location); 101 | console.log(lat); 102 | console.log(editedCamp); 103 | Campground.findByIdAndUpdate(req.params.id, editedCamp, function(err, UpdatedCamp){ 104 | if (err) { 105 | console.log(err); 106 | res.redirect("/campgrounds"); 107 | } else { 108 | res.redirect("/campgrounds/" + req.params.id); 109 | } 110 | }) 111 | }) 112 | }); 113 | 114 | //delete 115 | router.delete("/campgrounds/:id", middleware.checkCampgroundOwnership, function(req, res) { 116 | Campground.findByIdAndRemove(req.params.id, function(err) { 117 | if (err) { 118 | res.redirect("/campgrounds"); 119 | } else { 120 | res.redirect("/campgrounds"); 121 | } 122 | }); 123 | }); 124 | 125 | module.exports = router; --------------------------------------------------------------------------------