Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

completed assignment #18

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# assignment_thoreddit
A social news web application for Viking thunder Gods

By Tyler Ketron.
120 changes: 120 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
var express = require('express');
var app = express();

// ----------------------------------------
// Body Parser
// ----------------------------------------
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: true }));

// ----------------------------------------
// Sessions/Cookies
// ----------------------------------------
var cookieSession = require('cookie-session');

app.use(
cookieSession({
name: 'session',
keys: ['asdf1234567890qwer']
})
);

app.use((req, res, next) => {
res.locals.session = req.session;
res.locals.currentUser = req.session.currentUser;
next();
});

// ----------------------------------------
// Method Override
// ----------------------------------------
const methodOverride = require('method-override');
const getPostSupport = require('express-method-override-get-post-support');

app.use(
methodOverride(
getPostSupport.callback,
getPostSupport.options // { methods: ['POST', 'GET'] }
)
);

// ----------------------------------------
// Referrer
// ----------------------------------------
app.use((req, res, next) => {
req.session.backUrl = req.header('Referer') || '/';
next();
});

// ----------------------------------------
// Public
// ----------------------------------------
app.use(express.static(`${__dirname}/public`));

// ----------------------------------------
// Logging
// ----------------------------------------
var morgan = require('morgan');
var morganToolkit = require('morgan-toolkit')(morgan);

app.use(morganToolkit());

// ----------------------------------------
// Mongoose
// ----------------------------------------
const mongoose = require('mongoose');
app.use((req, res, next) => {
if (mongoose.connection.readyState) {
next();
} else {
require('./mongo')().then(() => next());
}
});

// ----------------------------------------
// Routes
// ----------------------------------------
var sessionsRouter = require('./routers/sessions')(app);
app.use('/', sessionsRouter);

var usersRouter = require('./routers/users');
app.use('/users', usersRouter);

var postsRouter = require('./routers/posts');
app.use('/posts', postsRouter);

// ----------------------------------------
// Template Engine
// ----------------------------------------
var expressHandlebars = require('express-handlebars');

var hbs = expressHandlebars.create({
partialsDir: 'views/',
defaultLayout: 'application',
helpers: {
trimPostBody: function(bodyText) {
var trimmedBody = bodyText.substring(0, 50) + `...`;
return trimmedBody;
}
}
});

app.engine('handlebars', hbs.engine);
app.set('view engine', 'handlebars');

// ----------------------------------------
// Server
// ----------------------------------------
var port = process.env.PORT || process.argv[2] || 3000;
var host = 'localhost';

var args;
process.env.NODE_ENV === 'production' ? (args = [port]) : (args = [port, host]);

args.push(() => {
console.log(`Listening: http://${host}:${port}`);
});

app.listen.apply(app, args);

module.exports = app;
13 changes: 13 additions & 0 deletions config/mongo.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"development": {
"database": "thoreddit_development",
"host": "localhost"
},
"test": {
"database": "thoreddit_test",
"host": "localhost"
},
"production": {
"use_env_variable": "MONGODB_URI"
}
}
26 changes: 26 additions & 0 deletions models/childcomment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const mongoose = require('mongoose');
var Schema = mongoose.Schema;
var Votable = require('./votable');

var ChildCommentSchema = new Schema(
{
body: String,
parent: {
type: Schema.Types.ObjectId,
ref: 'Votable'
},
author: {
type: Schema.Types.ObjectId,
red: 'User'
}
},
{
timestamps: true,
discriminatorKey: 'kind'
}
);

// var Comment = mongoose.model('Comment', CommentSchema);
var ChildComment = Votable.discriminator('ChildComment', ChildCommentSchema);

module.exports = ChildComment;
32 changes: 32 additions & 0 deletions models/comment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const mongoose = require('mongoose');
var Schema = mongoose.Schema;
var Votable = require('./votable');

var CommentSchema = new Schema(
{
body: String,
parent: {
type: Schema.Types.ObjectId,
ref: 'Votable'
},
author: {
type: Schema.Types.ObjectId,
red: 'User'
},
children: [
{
type: Schema.Types.ObjectId,
ref: 'ChildComment'
}
]
},
{
timestamps: true,
discriminatorKey: 'kind'
}
);

// var Comment = mongoose.model('Comment', CommentSchema);
var Comment = Votable.discriminator('Comment', CommentSchema);

module.exports = Comment;
19 changes: 19 additions & 0 deletions models/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
var mongoose = require('mongoose');
var bluebird = require('bluebird');

// Set bluebird as the promise
// library for mongoose
mongoose.Promise = bluebird;

var models = {};

// Load models and attach to models here
// models.User = require('./user');
//... more models
models.User = require('./user');
models.Votable = require('./votable');
models.Post = require('./post');
models.Comment = require('./comment');
models.ChildComment = require('./childcomment');

module.exports = models;
23 changes: 23 additions & 0 deletions models/post.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const mongoose = require('mongoose');
var Schema = mongoose.Schema;
var Votable = require('./votable');

var PostSchema = new Schema(
{
title: String,
body: String,
author: {
type: Schema.Types.ObjectId,
red: 'User'
}
},
{
timestamps: true,
discriminatorKey: 'kind'
}
);

// var Post = mongoose.model('Post', PostSchema);
var Post = Votable.discriminator('Post', PostSchema);

module.exports = Post;
37 changes: 37 additions & 0 deletions models/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const mongoose = require('mongoose');
var Schema = mongoose.Schema;

var UserSchema = new Schema(
{
fname: String,
lname: String,
username: String,
email: String
},
{
timestamps: true
}
);

UserSchema.methods.name = function() {
return `${this.fname} ${this.lname}`;
};

UserSchema.statics.findByFirstName = function(fname) {
return User.find({ fname: fname });
};

UserSchema.virtual('fullname').set(function(name) {
console.log('Setting the name of the user');
name = name.toString();
var splat = name.split(' ');
var fname = splat[0] || this.fname;
var lname = splat[1] || this.lname;
this.fname = fname;
this.lname = lname;
return this.name();
});

var User = mongoose.model('User', UserSchema);

module.exports = User;
64 changes: 64 additions & 0 deletions models/votable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
const mongoose = require('mongoose');
var Schema = mongoose.Schema;

var VotableSchema = new Schema(
{
score: Number,
parent_post: {
type: Schema.Types.ObjectId,
ref: 'Post'
},
hasUpvoted: [String],
hasDownvoted: [String]
},
{
timestamps: true,
discriminatorKey: 'kind'
}
);

VotableSchema.methods.upvote = function(username) {
if (this.hasUpvoted.includes(username)) {
this.score = this.score - 1;
remove(this.hasUpvoted, username);
this.save();
} else if (this.hasDownvoted.includes(username)) {
this.score = this.score + 2;
remove(this.hasDownvoted, username);
this.hasUpvoted.push(username);
this.save();
} else {
this.hasUpvoted.push(username);
this.score = this.score + 1;
this.save();
}
};

VotableSchema.methods.downvote = function(username) {
if (this.hasDownvoted.includes(username)) {
this.score = this.score + 1;
remove(this.hasDownvoted, username);
this.save();
} else if (this.hasUpvoted.includes(username)) {
this.score = this.score - 2;
remove(this.hasUpvoted, username);
this.hasDownvoted.push(username);
this.save();
} else {
this.hasDownvoted.push(username);
this.score = this.score - 1;
this.save();
}
};

function remove(array, element) {
const index = array.indexOf(element);

if (index !== -1) {
array.splice(index, 1);
}
}

var Votable = mongoose.model('Votable', VotableSchema);

module.exports = Votable;
10 changes: 10 additions & 0 deletions mongo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
var mongoose = require('mongoose');
var env = process.env.NODE_ENV || 'development';
var config = require('./config/mongo')[env];

module.exports = () => {
var envUrl = process.env[config.use_env_variable];
var localUrl = `mongodb://${config.host}/${config.database}`;
var mongoUrl = envUrl ? envUrl : localUrl;
return mongoose.connect(mongoUrl);
};
Loading