Skip to content

Commit

Permalink
added data seeder
Browse files Browse the repository at this point in the history
  • Loading branch information
moonpatel committed Aug 14, 2022
1 parent dcf83f2 commit 3a8834e
Show file tree
Hide file tree
Showing 5 changed files with 9,162 additions and 24 deletions.
16 changes: 16 additions & 0 deletions TODO.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
1. SEEDING CAMPGROUNDS:
seed data into your database. Data is available in the seeds/ directory.

2. CAMPGROUND INDEX:
add a /campgrounds express and respond with a list of all the campgrounds.
Make a template in /views/campgrounds/index.ejs for displaying campgrounds.

3. CAMPGROUND SHOW
add the feature to show any campground on a differemt page.

4. CAMPGROUND NEW & CREATE
add a form to create a new campground. also add that campground to the databse and show the
campground on campgrounds page.

5. CAMPGROUND EDIT & UPDATE

87 changes: 63 additions & 24 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,74 @@ const Campground = require('./models/campgrounds')
const port = 3000

// connect to MongoDB database
mongoose.connect('mongodb://localhost:27017/yelpcamp')
mongoose.connect('mongodb://localhost:27017/yelpcamp', {
})

// check the status of database connection
const db = mongoose.connection
db.on("error", console.error.bind(console,"connection error"))
db.once("open", () => {
console.log("Database connected")
})
// const db = mongoose.connection
// db.on("error", console.error.bind(console,"connection error"))
// db.once("open", () => {
// console.log("Database connected")
// })

// create an express application object
const app = express()

// set parameters for rendering templates
app.set('view engine','ejs')
app.set('views',path.join(__dirname,'views/'))

// RECEIVE REQUESTS
// main page
app.get('/',(req,res) => {
res.render('home')
})

// campgrounds
app.get('/campgrounds',(req,res) => {
const camp = new Campground({title:'Backyard'})
res.send(camp)

const cities = require('./seeds/cities')
const {places,descriptors} = require('./seeds/seedHelpers')

const sample = data => data[Math.floor(Math.random()*data.length)]

const seedDB = async () => {
await Campground.deleteMany({}) // delete all the data first

for (let i = 0; i < 50; i++) {
const city = sample(cities)
const desc = `${sample(descriptors)} ${sample(places)}}`
const cg = new Campground({
title: desc,
location: `${city.city, city.state}`
})
console.log(desc,`${city.city, city.state}`)
await cg.save()
}
}

seedDB()
.then(() => {
console.log('Data added')
mongoose.connection.close();
})
.catch((err) => {
console.log('Error in seeding data',err)
})

// listen for incoming requests
app.listen(port, () => {
console.log(`LISTENING ON PORT ${port}`)
})





// // create an express application object
// const app = express()

// // set parameters for rendering templates
// app.set('view engine','ejs')
// app.set('views',path.join(__dirname,'views/'))

// // RECEIVE REQUESTS
// // main page
// app.get('/',(req,res) => {
// res.render('home')
// })

// // campgrounds
// app.get('/campgrounds',(req,res) => {
// const camp = new Campground({title:'Backyard'})
// res.send(camp)
// })

// // listen for incoming requests
// app.listen(port, () => {
// console.log(`LISTENING ON PORT ${port}`)
// })
Loading

0 comments on commit 3a8834e

Please sign in to comment.