Skip to content

Commit f0e1ef9

Browse files
committed
Adding the comment feature
1 parent 4dce5a0 commit f0e1ef9

File tree

6 files changed

+98
-23
lines changed

6 files changed

+98
-23
lines changed

Diff for: app.js

+14-22
Original file line numberDiff line numberDiff line change
@@ -3,51 +3,41 @@ var app = express();
33
var request = require("request");
44
var bodyParser = require("body-parser");
55
var mongoose = require("mongoose");
6+
//requiring seedsDB
7+
var Campground = require("./models/campground");
8+
var seedDB = require("./seeds");
69

710
// Connect mongoose
8-
mongoose.connect("mongodb://localhost/yelp_camp");
11+
mongoose.connect("mongodb://localhost:27017/yelp_camp", {useNewUrlParser: true, useUnifiedTopology: true});
12+
913
app.use(bodyParser.urlencoded({extended: true}));
1014
app.use(express.static("public"));
1115
app.set("view engine", "ejs");
1216

13-
// Schema setup
14-
var campgroundSchema = new mongoose.Schema({
15-
Name: String,
16-
Image: String,
17-
Desc: String
18-
});
19-
20-
//Mongoose will automatically make the 'C' to 'c' //And the var below(Campground) will be used for create
21-
//We now have the "campgrounds" collection in our database
22-
var Campground = mongoose.model("Campground", campgroundSchema);
23-
24-
//Adding the first element
25-
//We will use all the functions like-->Campground.function()
26-
// console.log(Campground);---->model{ Campground }
27-
28-
29-
// Tell our code that the css file and JS file will be included in the public folder
17+
seedDB();
3018

3119

20+
// Start routing
3221
app.get("/", function (req, res) {
3322
res.render("landing");
3423
});
3524

25+
// INDEX----->Show all campgrounds
3626
app.get("/index", function (req, res) {
3727
//Get all the campgrounds from the database and call it allcampgrounds
3828
Campground.find({}, function (err, allcampgrounds) {
3929
if(err){
4030
console.log(err);
4131
}else{
4232
res.render("index", {campgrounds:allcampgrounds});
43-
//Pass allcampgrounds fetced as campgrounds to the render
33+
//Pass allcampgrounds fetched as campgrounds to the render
4434
}
4535
});
4636
});
4737

4838

49-
50-
app.post("/index", function (req, res) {
39+
// CREATE -- add new campground
40+
app.post("/index", function (req, res) {
5141
//Get data from form and add it to campgrounds array
5242
//Redirect back to campgrounds list page
5343

@@ -70,12 +60,14 @@ app.post("/index", function (req, res) {
7060
});
7161

7262

63+
// NEW------->Show form to create new campground
7364
app.get("/index/new", function (req, res) {
7465
//This is the place to fill the form
7566
res.render("new");
7667
});
7768

78-
//SHOW ROUTE
69+
70+
//SHOW -- show more information for the perticular id
7971
app.get("/index/:id", function (req, res) {
8072
Campground.findById(req.params.id, function (err, foundCampground) {
8173
if(err){

Diff for: models/campground.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
var mongoose = require("mongoose");
2+
// Schema setup
3+
var campgroundSchema = new mongoose.Schema({
4+
Name: String,
5+
Image: String,
6+
Desc: String,
7+
Comments: [
8+
{
9+
type:mongoose.Schema.Types.ObjectId,
10+
ref: "Comment"
11+
}
12+
]
13+
});
14+
15+
//Mongoose will automatically make the 'C' to 'c' //And the var below(Campground) will be used for create
16+
//We now have the "campgrounds" collection in our database
17+
module.exports = mongoose.model("Campground", campgroundSchema);

Diff for: models/comment.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
var mongoose = require("mongoose");
2+
3+
var commentSchema = new mongoose.Schema({
4+
content:String,
5+
author:String
6+
});
7+
8+
module.exports = mongoose.model("Comment", commentSchema);

Diff for: models/user.js

Whitespace-only changes.

Diff for: seeds.js

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
var mongoose = require("mongoose");
2+
// Schema setup
3+
var Campground = require("./models/campground");
4+
var Comment = require("./models/comment");
5+
6+
var data = [
7+
{
8+
Name: "Cloud's Rest",
9+
Image: "https://farm4.staticflickr.com/3795/10131087094_c1c0a1c859.jpg",
10+
Desc: "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum"
11+
},
12+
{
13+
Name: "Desert Mesa",
14+
Image: "https://farm6.staticflickr.com/5487/11519019346_f66401b6c1.jpg",
15+
Desc: "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum"
16+
},
17+
{
18+
Name: "Canyon Floor",
19+
Image: "https://farm1.staticflickr.com/189/493046463_841a18169e.jpg",
20+
Desc: "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum"
21+
}
22+
];
23+
24+
function seedDB() {
25+
//remove all campgrounds
26+
Campground.remove({}, function (err, campgrounds) {
27+
if(err){
28+
console.log(err);
29+
}else{
30+
console.log("Removed campgrounds");
31+
//add new campgrounds
32+
data.forEach(function (seed) {
33+
Campground.create(seed,function (err, campground) {
34+
if(err){
35+
console.log(err);
36+
}else{
37+
console.log("Added campgrounds");
38+
//Create a new comment
39+
Comment.create({
40+
content:"Hello this is a comment",
41+
author:"Mehul Chaturvedi"
42+
}, function (err, comment) {
43+
if (err) {
44+
console.log(err);
45+
}else{
46+
campground.Comments.push(comment);
47+
campground.save();
48+
49+
}
50+
});
51+
}
52+
});
53+
});
54+
}
55+
});
56+
}
57+
58+
module.exports = seedDB;

Diff for: views/landing.ejs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<%- include("partials/header") %>
22

33
<h1>Landing Page</h1>
4-
<a href="/campgrounds">View all campgrounds</a>
4+
<a href="/index">View all campgrounds</a>
55

66
<%- include('partials/footer') %>

0 commit comments

Comments
 (0)