Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
twonius committed Dec 14, 2017
0 parents commit 62ab273
Show file tree
Hide file tree
Showing 27 changed files with 2,531 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
.env
74 changes: 74 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
var express = require("express"),
app = express(),
bodyParser = require("body-parser"),
mongoose = require("mongoose"),
passport = require("passport"),
cookieParser = require("cookie-parser"),
LocalStrategy = require("passport-local"),
flash = require("connect-flash"),
Campground = require("./models/campground"),
Comment = require("./models/comment"),
User = require("./models/user"),
session = require("express-session"),
seedDB = require("./seeds"),
methodOverride = require("method-override");
// configure dotenv
require('dotenv').load();

//requiring routes
var commentRoutes = require("./routes/comments"),
campgroundRoutes = require("./routes/campgrounds"),
indexRoutes = require("./routes/index")

// assign mongoose promise library and connect to database
mongoose.Promise = global.Promise;

const databaseUri = process.env.MONGODB_URI || 'mongodb://localhost/yelp_camp';

mongoose.connect(databaseUri, { useMongoClient: true })
.then(() => console.log(`Database connected`))
.catch(err => console.log(`Database connection error: ${err.message}`));

app.use(bodyParser.urlencoded({extended: true}));
app.set("view engine", "ejs");
app.use(express.static(__dirname + "/public"));
app.use(methodOverride('_method'));
app.use(cookieParser('secret'));
//require moment
app.locals.moment = require('moment');
// seedDB(); //seed the database
seedDB();


// PASSPORT CONFIGURATION
app.use(require("express-session")({
secret: "Once again Rusty wins cutest dog!",
resave: false,
saveUninitialized: false
}));

app.use(flash());
app.use(passport.initialize());
app.use(passport.session());
passport.use(new LocalStrategy(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());

app.use(function(req, res, next){
res.locals.currentUser = req.user;
res.locals.success = req.flash('success');
res.locals.error = req.flash('error');
next();
});


app.use("/", indexRoutes);
app.use("/campgrounds", campgroundRoutes);
app.use("/campgrounds/:id/comments", commentRoutes);

const port = process.env.PORT || 3000;
const ip = process.env.IP;

app.listen(port, ip, function(){
console.log(`The YelpCamp Server Has Started! on ${ip} : ${port}`);
});
57 changes: 57 additions & 0 deletions middleware/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
var Comment = require('../models/comment');
var Campground = require('../models/campground');
module.exports = {
isLoggedIn: function(req, res, next){
if(req.isAuthenticated()){
return next();
}
req.flash('error', 'You must be signed in to do that!');
res.redirect('/login');
},
checkUserCampground: function(req, res, next){
Campground.findById(req.params.id, function(err, foundCampground){
if(err || !foundCampground){
console.log(err);
req.flash('error', 'Sorry, that campground does not exist!');
res.redirect('/campgrounds');
} else if(foundCampground.author.id.equals(req.user._id) || req.user.isAdmin){
req.campground = foundCampground;
next();
} else {
req.flash('error', 'You don\'t have permission to do that!');
res.redirect('/campgrounds/' + req.params.id);
}
});
},
checkUserComment: function(req, res, next){
Comment.findById(req.params.commentId, function(err, foundComment){
if(err || !foundComment){
console.log(err);
req.flash('error', 'Sorry, that comment does not exist!');
res.redirect('/campgrounds');
} else if(foundComment.author.id.equals(req.user._id) || req.user.isAdmin){
req.comment = foundComment;
next();
} else {
req.flash('error', 'You don\'t have permission to do that!');
res.redirect('/campgrounds/' + req.params.id);
}
});
},
isAdmin: function(req, res, next) {
if(req.user.isAdmin) {
next();
} else {
req.flash('error', 'This site is now read only thanks to spam and trolls.');
res.redirect('back');
}
},
isSafe: function(req, res, next) {
if(req.body.image.match(/^https:\/\/images\.unsplash\.com\/.*/)) {
next();
}else {
req.flash('error', 'Only images from images.unsplash.com allowed.\nSee https://youtu.be/Bn3weNRQRDE for how to copy image urls from unsplash.');
res.redirect('back');
}
}
}
30 changes: 30 additions & 0 deletions models/campground.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
var mongoose = require("mongoose");

var campgroundSchema = new mongoose.Schema({
name: String,
image: String,
description: String,
state: String,
party: String,
chamber: String,
general: Number,
primary: Number,
lat: Number,
long: Number,
createdAt: { type: Date, default: Date.now },
author: {
id: {
type: mongoose.Schema.Types.ObjectId,
ref: "User"
},
username: String
},
comments: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "Comment"
}
]
});

module.exports = mongoose.model("campground", campgroundSchema);
17 changes: 17 additions & 0 deletions models/comment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
var mongoose = require("mongoose");

var commentSchema = mongoose.Schema({
text: String,
amount: Number ,
campaignID : Number,
createdAt: { type: Date, default: Date.now },
author: {
id: {
type: mongoose.Schema.Types.ObjectId,
ref: "User"
},
username: String
}
});

module.exports = mongoose.model("Comment", commentSchema);
21 changes: 21 additions & 0 deletions models/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
var mongoose = require("mongoose");
var passportLocalMongoose = require("passport-local-mongoose");

var UserSchema = new mongoose.Schema({
username: String,
password: String,
isAdmin: {type: Boolean, default: false},
address: {
street1: String,
street2: String,
city: String,
state: String,
zip: String
},
profession: String,
employer: String
});

UserSchema.plugin(passportLocalMongoose)

module.exports = mongoose.model("User", UserSchema);
Loading

0 comments on commit 62ab273

Please sign in to comment.