DEV Community

Code_Regina
Code_Regina

Posted on

|YelpCamp| Campgrounds CRUD

         -Introducing YelpCamp Final Project 
         -Creating the Basic Express App 
         -Campground Model Basics 
         -Seeding Campgrounds
         -Campground Index
         -Campground Show 
         -Campground New and Create 
         -Campground Edit and Update 
         -Campground Delete 
Enter fullscreen mode Exit fullscreen mode

Introducing YelpCamp Final Project

This is the beginning of building a single large application.
Here is an overview of what I will be building.
The purpose of YelpCamp is a place to find campgrounds and review them similar to how yelp functions.
The app will consist of a homepage/cluster map/authentication/create new campground post/review page

Alt Text

Alt Text

Alt Text

Alt Text

Alt Text

The code to get started:

must run npm install to add the node modules dependencies

Creating the Basic Express App

At the terminal type

npm init -y 

Enter fullscreen mode Exit fullscreen mode

then install express, mongoose, and ejs

npm i express mongoose ejs

Enter fullscreen mode Exit fullscreen mode

Then set up the additional file structure that will build out this project.

Campground Model Basics

Setting up a mongoose model for the campgrounds.
Each campground will have a name/title, price, description and a location.


const mongoose = require('mongoose'); 
const Schema = mongoose.Schema; 

const CampgroundSchema = new Schema({
    title: String, 
    prince: String, 
    description: String, 
    location: String
  }); 

mondule.exports = mongoose.model('Campground', CampgroundSchema); 

Enter fullscreen mode Exit fullscreen mode

Then the next thing is to make sure that mongoose is connected and functional.

Seeding Campgrounds

Now it is time to set up the campgrounds with data.
Here is where to find the data set to seed the database with.
https://github.com/Colt/YelpCamp/blob/c12b6ca9576b48b579bc304f701ebb71d6f9879a/seeds/cities.js

Campground Index

Now time to set up basic routes and templates functionality.


setting up the route 
app.get('/campgrounds', async (req,res) => {
  const campground = await Campground.find({})
  res.render('campgrounds/index')
 })

Enter fullscreen mode Exit fullscreen mode

setting up the html template


<h1>All Campgrounds</h1> 
<ul> 
  <% for (let campgrounds of campgrounds %> 
<li><%= campgrounds.title %></li>
  <% }%>
</ul> 

Enter fullscreen mode Exit fullscreen mode

Campground Show

The show route will eventually become a details page for the campgrounds.

setting up the route


app.get('/campgrounds/:id', async(req, res) => {
    res.render('campgrounds/show')
 })

Enter fullscreen mode Exit fullscreen mode

setting up the html template


<h1><%= campground.title %></h1> 

<h2><%= campground.location %></h2> 

Enter fullscreen mode Exit fullscreen mode

Campground New and Create

Rendering a form


<form action="/campgrounds" methods="POST">
  <div>
   <label for="title">Title</label> 
   <input type="text" id="title" name="campground[title]">
  </div>
  <div>
   <label for="location">Location</label> 
   <input type="text" id="location" name="campground[location]">
  </div>
<button>Add Campground</button>
</form> 

Enter fullscreen mode Exit fullscreen mode

setting up the route


app.post('/campgrounds', async (req, res) => {
  const campground = new Campground(req.body.campground); 
  await campground.save(); 
  res.redirect(`/campgrounds/${ campground._id }); 
 }); 

Enter fullscreen mode Exit fullscreen mode

Campground Edit and Update

The route that serves the form


app.get('/campgrounds/:id/edit', async(req, res) => {
  const campground = await Campground.findById(req.params.id) 
  res.render('campgrounds/edit', { campground })); 
})

Enter fullscreen mode Exit fullscreen mode

html template

<h1>Edit Campgrounds</h1> 
<form action="/campgrounds" methods="POST">
  <div>
   <label for="title">Title</label> 
   <input type="text" id="title" name="campground[title]">
  </div>
  <div>
   <label for="location">Location</label> 
   <input type="text" id="location" name="campground[location]">
  </div>
<button>Add Campground</button>
</form> 

Enter fullscreen mode Exit fullscreen mode

Campground Delete

The route will be a delete route


app.delete('/campgrounds/:id', async (req, res) => {
   const { id } = req.params; 
   await Campground.findByIdAndDelete(id);
res.redirect('/campgrounds');
})

Enter fullscreen mode Exit fullscreen mode

html template


<h1><%= campground.title %></h1> 
<h2><%= campground.location %></h2>

<p> <a href="/campgrounds/<%=campground_id%>/edit">Edit</a>
</p>
<p>
<form action="/campgrounds/<%=campground._id%>?_method=DELETE" method="POST"> 
<button>Delete</button>
</form>
</p>
<footer>
<a href="/campgrounds">All Campgrounds</a>
</footer>

Enter fullscreen mode Exit fullscreen mode

Now that the basic structure is set up, it is time to build out the complexities of the yelp camp web application.

Top comments (2)

Collapse
 
code_regina profile image
Code_Regina

Its a very in-depth course so much information which is why I am documenting it.
=D

Collapse
 
jdog787 profile image
JDOG787

ayyy someone else who took this couse :)