text: const express = require("express");
  const router = express.Router({mergeParams: true});
  const Campground = require("../models/campground");
  const Comment = require("../models/comment");
  const middleware = require("../middleware"); //if a dir is required "index.js" is automatically required
  
  //Comments New
  router.get("/new", middleware.isLoggedIn, (req, res) => {
    Campground.findById(req.params.id, (err, foundCampground) => {
      if (err) {
        console.log(err);
      } else {
        res.render("comments/new", {campground: foundCampground});
      }
    });
  });
  
  //Comments Create
  router.post("/", middleware.isLoggedIn, (req, res) => {
    Campground.findById(req.params.id, (err, foundCampground) => {
      if (err) {
        console.log(err);
        res.redirect("/campgrounds");
      } else {
        Comment.create(req.body.comment, (err, comment) => {
          if (err) {
            req.flash("error", "Something went wrong");
            console.log(err);
          } else {
            //add username and id to comment
            comment.author.id = req.user._id;
            if (req.user.displayName) {
              comment.author.displayName = req.user.displayName;
            }
            else {
              comment.author.displayName = req.body.comment.displayName;
            }
            //save comment
            comment.save();
            foundCampground.comments.push(comment);
            foundCampground.save(err => {
              if (err) {
                req.flash("error", "Something went wrong");
                console.log(err);
              } else {
                req.flash("success", "Successfully added comment");
                res.redirect("/campgrounds/" + foundCampground._id);
              }
            });
          }
        });
      }
    });
  });
  
  // EDIT COMMENT ROUTE
  router.get("/:comment_id/edit", middleware.checkCommentOwnership, (req, res) => {
    Campground.findById(req.params.id, (err, foundCampground) => {
      if (err || !foundCampground) {
        req.flash("error", "Campground not found");
        return res.redirect("back");
      }
      Comment.findById(req.params.comment_id, (err, foundComment) => {
        if (err) {
          res.redirect("back");
        } else {
          res.render("comments/edit", {campground_id: req.params.id, comment: foundComment});
        }
      });
    })
  
  });
  // UPDATE COMMENT ROUTE
  router.put("/:comment_id", middleware.checkCommentOwnership, (req, res) => {
    Comment.findByIdAndUpdate(req.params.comment_id, req.body.comment, (err, updatedComment) => {
      if (err) {
        res.redirect("back");
      } else {
        req.flash("success", "Comment updated.");
        res.redirect("/campgrounds/" +req.params.id);
      }
    });
    //redirect somewhere
  });
  
  // DESTROY COMMENT ROUTE
  router.delete("/:comment_id", middleware.checkCommentOwnership, (req, res) => {
    Comment.findByIdAndRemove(req.params.comment_id, err => {
      if (err) {
        res.redirect("back");
      } else {
        req.flash("success", "Comment deleted.");
        res.redirect("/campgrounds/" + req.params.id);
      }
    });
  });
  
  
  module.exports = router;
  