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

Ian Halverson's solution to assignment_thoreddit #8

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.DS_Store
node_modules
.env
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

Names: Ian Halverson and Ed Triplett
88 changes: 88 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
var express = require("express");
var app = express();

var bodyParser = require("body-parser");
app.use(bodyParser.urlencoded({ extended: true }));
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();
// });
const methodOverride = require("method-override");
const getPostSupport = require("express-method-override-get-post-support");
app.use(
methodOverride(
getPostSupport.callback,
getPostSupport.options // { methods: ['POST', 'GET'] }
)
);
app.use((req, res, next) => {
req.session.backUrl = req.header("Referer") || "/";
next();
});
app.use(express.static(`${__dirname}/public`));
var morgan = require("morgan");
app.use(morgan("tiny"));
app.use((req, res, next) => {
console.log();
["query", "params", "body"].forEach(key => {
if (req[key]) {
var capKey = key[0].toUpperCase() + key.substr(1);
var value = JSON.stringify(req[key], null, 2);
console.log(`${capKey}: ${value}`);
}
});
next();
});

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

app.get("/", (req, res) => {
res.redirect("/users");
});
// not set up yet!!!
var usersRouter = require("./routers/users");
app.use("/users", usersRouter);

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

// var someRouter = require("./routers/");
// app.use("/", someRouter);

var expressHandlebars = require("express-handlebars");
var hbs = expressHandlebars.create({
partialsDir: "views/partials",
defaultLayout: "application"
});

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

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;
28 changes: 28 additions & 0 deletions config/mongo.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"development": {
"database": "thoreddit_development",
"host": "localhost"
},
"test": {
"database": "thoreddit_test",
"host": "localhost"
},
"production": {
"use_env_variable": "MONGODB_URI"
}
}















14 changes: 14 additions & 0 deletions models/comment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const mongoose = require("mongoose");
var Schema = mongoose.Schema;
var Scorable = require("./scorable");

var CommentSchema = new Schema(
{},
{
discriminatorKey: "kind"
}
);

var Comment = Scorable.discriminator("Comment", CommentSchema);

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

mongoose.Promise = bluebird;

const models = {};

// Load models and attach to models here
models.User = require("./user");
models.Scorable = require("./scorable");
models.Post = require("./post");
models.Comment = require("./comment");
models.Score = require("./score");

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

var PostSchema = new Schema(
{
title: String,
comments: [
{
type: Schema.Types.ObjectId,
ref: "Comment"
}
]
},
{
discriminatorKey: "kind"
}
);

var Post = Scorable.discriminator("Post", PostSchema);

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

var ScorableSchema = new Schema(
{
text: String,
score: {
type: Schema.Types.ObjectId,
ref: "Score"
},
user: {
type: Schema.Types.ObjectId,
ref: "User"
}
},
{
timestamps: true,
discriminatorKey: "kind"
}
);

ScorableSchema.plugin(findOrCreate);

var Scorable = mongoose.model("Scorable", ScorableSchema);

module.exports = Scorable;
26 changes: 26 additions & 0 deletions models/score.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const mongoose = require("mongoose");
var Schema = mongoose.Schema;
var findOrCreate = require("mongoose-findorcreate");

var ScoreSchema = new Schema(
{
user: {
type: Schema.Types.ObjectId,
ref: "User"
},
scorable: {
type: Schema.Types.ObjectId,
ref: "Scorable"
},
value: Number
},
{
timestamps: true
}
);

ScoreSchema.plugin(findOrCreate);

var Score = mongoose.model("Score", ScoreSchema);

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

var UserSchema = new Schema(
{
username: String,
email: String,
posts: [
{
type: Schema.Types.ObjectId,
ref: "Post"
}
]
},
{
timestamps: true
}
);

UserSchema.plugin(findOrCreate);

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

module.exports = User;
24 changes: 24 additions & 0 deletions mongo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const 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