Skip to content

Commit

Permalink
restructuring routes into different files
Browse files Browse the repository at this point in the history
  • Loading branch information
moonpatel committed Aug 31, 2022
1 parent 54b682c commit d8f9ae5
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 79 deletions.
93 changes: 14 additions & 79 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ const mongoose = require('mongoose')
const methodOverride = require('method-override')
const ejsMate = require('ejs-mate')
const path = require('path')
const catchAsync = require('./utils/catchAsync')
const ExpressError = require('./utils/ExpressError')
// require models
const Campground = require('./models/campgrounds')
const Review = require('./models/reviews')
const { campgroundSchema } = require('./schemas')
const { reviewSchema } = require('./schemas')

// routes
const campground = require('./routes/campgrounds')
const reviews = require('./routes/reviews')
// port number to listen for requests
const port = 3000

Expand All @@ -33,88 +31,25 @@ app.set('views', path.join(__dirname, 'views'))
app.use(express.urlencoded({ extended: true }))
app.use(methodOverride('_method'))

// 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()
}
// 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()
}

// RECEIVE REQUESTS
// main page
app.get('/', (req, res) => {
res.render('home')
})
// campgrounds index
app.get('/campgrounds', catchAsync(async (req, res) => {
const camps = await Campground.find({})
res.render('campgrounds/index', { camps })
}))
// add new campgrounds
app.get('/campgrounds/new', (req, res) => res.render('new'))
// add new campground to server
app.post('/campgrounds', validateCampground, catchAsync(async (req, res, next) => {
const cg = new Campground(req.body.campground)
await cg.save()
res.redirect(`/campgrounds/${cg._id}`)
}))
// add review to server
app.post('/campgrounds/:id/reviews', validateReview, catchAsync(async (req, res) => {
const campground = await Campground.findById(req.params.id)
const review = new Review(req.body.review)
campground.reviews.push(review)
await campground.save()
await review.save()
res.redirect(`/campgrounds/${req.params.id}`)
}))
app.delete('/campgrounds/:id/reviews/: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}`)
}))
// show individual campground
app.get('/campgrounds/: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
app.get('/campgrounds/:id/edit', catchAsync(async (req, res) => {
const cg = await Campground.findById(req.params.id)
res.render('edit', { cg })
}))
// send put request to save changes
app.put('/campgrounds/: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
app.delete('/campgrounds/:id', catchAsync(async (req, res) => {
await Campground.findByIdAndDelete(req.params.id)
res.redirect('/campgrounds')
}))
// if page unavailable
app.get('/', (req, res) => res.render('home'))
// campground routes
app.use('/campgrounds', campground)
// review routes
app.use('/campgrounds/:id/reviews', reviews)


// Page not found
app.all('*', (req, res, next) => {
next(new ExpressError('Page not found', 404))
})
// error handler
// all in one error handler
app.use((err, req, res, next) => {
const { message = 'Something went wrong', statusCode = 500 } = err
console.log(err)
res.status(statusCode).render('error', { err })
})

// listen for incoming requests
app.listen(port, () => console.log(`LISTENING ON PORT ${port}`))
56 changes: 56 additions & 0 deletions routes/campgrounds.js
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
41 changes: 41 additions & 0 deletions routes/reviews.js
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

0 comments on commit d8f9ae5

Please sign in to comment.