| @@ -0,0 +1,69 @@ | ||
| var express = require("express"); | ||
| var router = express.Router(); | ||
| var Campground = require("../models/campground"); | ||
|
|
||
| //INDEX - show all campgrounds | ||
| router.get("/", function(req, res){ //no primeiro paramento está implicito que é /campgrounds. foi definido no app.js que todos os paths desse arquivo seriam encurtados | ||
| // Get all campgrounds from DB | ||
| Campground.find({}, function(err, allCampgrounds){ | ||
| if(err){ | ||
| console.log(err); | ||
| } else { | ||
| res.render("campgrounds/index",{campgrounds:allCampgrounds}); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| //CREATE - add new campground to DB | ||
| router.post("/", isLoggedIn, function(req, res){ | ||
| // get data from form and add to campgrounds array | ||
| var name = req.body.name; | ||
| var image = req.body.image; | ||
| var desc = req.body.description; | ||
| var author = { | ||
| id: req.user._id, | ||
| username: req.user.username | ||
| } | ||
| var newCampground = {name: name, image: image, description: desc, author:author} | ||
| /* cria um objeto para ser "pushado" no db | ||
| name = var name & image = var image */ | ||
| // Create a new campground and save to DB | ||
| Campground.create(newCampground, function(err, newlyCreated){ | ||
| if(err){ | ||
| console.log(err); | ||
| } else { | ||
| //redirect back to campgrounds page | ||
| res.redirect("/campgrounds"); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| //NEW - show form to create new campground | ||
| router.get("/new", isLoggedIn, function(req, res){ | ||
| res.render("campgrounds/new"); | ||
| }); | ||
|
|
||
| // SHOW - shows more info about one campground | ||
| router.get("/:id", function(req, res){ | ||
| //find the campground with provided ID | ||
| Campground.findById(req.params.id).populate("comments").exec(function(err, foundCampground){ | ||
| if(err){ | ||
| console.log(err); | ||
| } else { | ||
| console.log(foundCampground) | ||
| //render show template with that campground | ||
| res.render("campgrounds/show", {campground: foundCampground}); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| //middleware | ||
| function isLoggedIn(req, res, next){ | ||
| if(req.isAuthenticated()){ | ||
| return next(); | ||
| } | ||
| res.redirect("/login"); | ||
| } | ||
|
|
||
|
|
||
| module.exports = router; |
| @@ -0,0 +1,54 @@ | ||
| var express = require("express"); | ||
| var router = express.Router({mergeParams: true}); | ||
| var Campground = require("../models/campground"); | ||
| var Comment = require("../models/comment"); | ||
|
|
||
| //Comments New | ||
| router.get("/new", isLoggedIn, function(req, res){ | ||
| // find campground by id | ||
| console.log(req.params.id); | ||
| Campground.findById(req.params.id, function(err, campground){ | ||
| if(err){ | ||
| console.log(err); | ||
| } else { | ||
| res.render("comments/new", {campground: campground}); | ||
| } | ||
| }) | ||
| }); | ||
|
|
||
| //Comments Create | ||
| router.post("/",isLoggedIn,function(req, res){ | ||
| //lookup campground using ID | ||
| Campground.findById(req.params.id, function(err, campground){ | ||
| if(err){ | ||
| console.log(err); | ||
| res.redirect("/campgrounds"); | ||
| } else { | ||
| Comment.create(req.body.comment, function(err, comment){ | ||
| if(err){ | ||
| console.log(err); | ||
| } else { | ||
| //add username and id to comment | ||
| comment.author.id = req.user._id; | ||
| comment.author.username = req.user.username; | ||
| //save comment | ||
| comment.save(); | ||
| campground.comments.push(comment); | ||
| campground.save(); | ||
| res.redirect('/campgrounds/' + campground._id); | ||
| } | ||
| }); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| //middleware | ||
| function isLoggedIn(req, res, next){ | ||
| if(req.isAuthenticated()){ | ||
| return next(); | ||
| } | ||
| res.redirect("/login"); | ||
| } | ||
|
|
||
|
|
||
| module.exports = router; |
| @@ -0,0 +1,58 @@ | ||
| var express = require("express"); | ||
| var router = express.Router(); | ||
| var passport = require("passport"); | ||
| var User = require("../models/user"); | ||
|
|
||
| //root route | ||
| router.get("/", function(req, res){ | ||
| res.render("landing"); | ||
| }); | ||
|
|
||
| // show register form | ||
| router.get("/register", function(req, res){ | ||
| res.render("register"); | ||
| }); | ||
|
|
||
| //handle sign up logic | ||
| router.post("/register", function(req, res){ | ||
| var newUser = new User({username: req.body.username}); | ||
| User.register(newUser, req.body.password, function(err, user){ | ||
| if(err){ | ||
| console.log(err); | ||
| return res.render("register"); | ||
| } | ||
| passport.authenticate("local")(req, res, function(){ | ||
| res.redirect("/campgrounds"); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| // show login form | ||
| router.get("/login", function(req, res){ | ||
| res.render("login"); | ||
| }); | ||
|
|
||
| // handling login logic | ||
| router.post("/login", passport.authenticate("local", | ||
| { | ||
| successRedirect: "/campgrounds", | ||
| failureRedirect: "/login" | ||
| }), function(req, res){ | ||
| }); | ||
|
|
||
| // logout route | ||
| router.get("/logout", function(req, res){ | ||
| req.logout(); | ||
| res.redirect("/campgrounds"); | ||
| }); | ||
|
|
||
| //middleware | ||
| function isLoggedIn(req, res, next){ | ||
| if(req.isAuthenticated()){ | ||
| return next(); | ||
| } | ||
| res.redirect("/login"); | ||
| } | ||
|
|
||
|
|
||
| module.exports = router; |
| @@ -1,4 +1,5 @@ | ||
| <% include ../partials/header %> | ||
|
|
||
| <div class="container"> | ||
| <header class="jumbotron"> | ||
| <div class="container"> | ||
| @@ -0,0 +1,10 @@ | ||
| <% include ./partials/header %> | ||
|
|
||
| <h1>Login!</h1> | ||
| <form action="/login" method="POST"> | ||
| <input type="text" name="username" placeholder="username"> | ||
| <input type="password" name="password" placeholder="password"> | ||
| <input type="submit" value="Login!"> | ||
| </form> | ||
|
|
||
| <% include ./partials/footer %> |
| @@ -0,0 +1,9 @@ | ||
| <% include ./partials/header %> | ||
| <h1>Sign Up!</h1> | ||
|
|
||
| <form action="/register" method="post"> | ||
| <input type="text" name="username" placeholder="username"> | ||
| <input type="password" name="password" placeholder="password"> | ||
| <button>Sign Up</button> | ||
| </form> | ||
| <% include ./partials/footer %> |