-
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.
restructuring routes into different files
- Loading branch information
Showing
3 changed files
with
111 additions
and
79 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
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,56 @@ | ||
const express = require('express') | ||
const router = express.Router() | ||
const catchAsync = require('../utils/catchAsync') | ||
const { campgroundSchema } = require('../schemas') | ||
|
||
|
||
const ExpressError = require('../utils/ExpressError') | ||
const Campground = require('../models/campgrounds') | ||
|
||
// validate campground object | ||
const validateCampground = (req, res, next) => { | ||
const { error } = campgroundSchema.validate(req.body.campground) | ||
if (error) { | ||
const msg = error.details.map(el => el.message).join(',') | ||
throw new ExpressError(msg, 400) | ||
} | ||
else next() | ||
} | ||
|
||
// campgrounds index | ||
router.get('/', catchAsync(async (req, res) => { | ||
const camps = await Campground.find({}) | ||
res.render('campgrounds/index', { camps }) | ||
})) | ||
// add new campgrounds | ||
router.get('/new', (req, res) => res.render('new')) | ||
// add new campground to server | ||
router.post('/', validateCampground, catchAsync(async (req, res, next) => { | ||
const cg = new Campground(req.body.campground) | ||
await cg.save() | ||
res.redirect(`/campgrounds/${cg._id}`) | ||
})) | ||
|
||
// show individual campground | ||
router.get('/:id', catchAsync(async (req, res) => { | ||
const c = await Campground.findOne({ _id: req.params.id }).populate('reviews') | ||
res.render('campgrounds/show', { c }) | ||
})) | ||
// render form to edit a campground | ||
router.get('/:id/edit', catchAsync(async (req, res) => { | ||
const cg = await Campground.findById(req.params.id) | ||
res.render('edit', { cg }) | ||
})) | ||
// send put request to save changes | ||
router.put('/:id', validateCampground, catchAsync(async (req, res) => { | ||
const { title, location } = req.body.campground | ||
await Campground.updateOne({ _id: req.params.id }, { title: title, location: location }) | ||
res.redirect(`/campgrounds/${req.params.id}`) | ||
})) | ||
// delete campground | ||
router.delete('/:id', catchAsync(async (req, res) => { | ||
await Campground.findByIdAndDelete(req.params.id) | ||
res.redirect('/campgrounds') | ||
})) | ||
|
||
module.exports = router |
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,41 @@ | ||
const express = require('express') | ||
const router = express.Router({ mergeParams: true }) | ||
const catchAsync = require('../utils/catchAsync') | ||
const { reviewSchema } = require('../schemas') | ||
|
||
|
||
const ExpressError = require('../utils/ExpressError') | ||
const Campground = require('../models/campgrounds') | ||
const Review = require('../models/reviews') | ||
|
||
|
||
// validate reviews object | ||
const validateReview = (req, res, next) => { | ||
const { error } = reviewSchema.validate(req.body.review) | ||
if (error) { | ||
const msg = error.details.map(el => el.message).join(',') | ||
throw new ExpressError(msg, 400) | ||
} | ||
else next() | ||
} | ||
|
||
// add review to server | ||
router.post('/', validateReview, catchAsync(async (req, res) => { | ||
console.log(req.params.id) | ||
const campground = await Campground.findById(req.params.id) | ||
console.log(campground) | ||
const review = new Review(req.body.review) | ||
campground.reviews.push(review) | ||
await campground.save() | ||
await review.save() | ||
res.redirect(`/campgrounds/${req.params.id}`) | ||
})) | ||
// delete review | ||
router.delete('/:reviewId', catchAsync(async (req, res, next) => { | ||
const { id, reviewId } = req.params | ||
await Review.findByIdAndDelete(reviewId) | ||
await Campground.findByIdAndUpdate(id, { $pull: { reviews: reviewId } }) | ||
res.redirect(`/campgrounds/${id}`) | ||
})) | ||
|
||
module.exports = router |