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

THoreddit_Dan_Steph #5

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 Dan and Stephanie
48 changes: 48 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
const express = require("express");
const app = express();
const bodyParser = require("body-parser");
const exphbs = require("express-handlebars");
const mongoose = require("mongoose");
const methodOverride = require("method-override");
const getPostSupport = require("express-method-override-get-post-support");

app.use(bodyParser.urlencoded({ extended: true }));
app.use(methodOverride(getPostSupport.callback, getPostSupport.options));

const hbs = exphbs.create({
partialsDir: "views/partials",
defaultLayout: "main"
});
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]);
var sessionsRouter = require("./routers/sessions")(app);

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

app.use("/", sessionsRouter);

var usersRouter = require("./routers/users");
app.use("/users", usersRouter);
var postsRouter = require("./routers/posts");
app.use("/posts", postsRouter);
args.push(() => {
console.log(`Listening: http://${host}:${port}`);
});

app.get("/", (req, res) => {
res.redirect("/users");
});

app.listen.apply(app, args);
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"
}
}
12 changes: 12 additions & 0 deletions data_structure.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Users
username (string)
email (string)
posts (array)

Posts
title (string)
author (string)
body (text)
votes (integer)
topLevel (boolean)
comments (array)
15 changes: 15 additions & 0 deletions models/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
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");
models.Post = require("./post");
//... more models

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

var PostSchema = {
title: String,
author: { type: Schema.Types.ObjectId, ref: "User" },
body: String,
votes: Number,
topLevel: Boolean,
subPosts: [{ type: Schema.Types.ObjectId, ref: "Post" }]
};

var Post = mongoose.model("Post", PostSchema);

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

var UserSchema = {
username: String,
email: String,
posts: [{ type: Schema.Types.ObjectId, ref: "Post" }]
};
//[{ type: Schema.Types.ObjectId, ref: "Post" }]
var User = mongoose.model("User", UserSchema);

module.exports = User;
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);
};
29 changes: 29 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "assignment_thoreddit",
"version": "1.0.0",
"description": "A definitely-not-Reddit application for Vikings",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/Avonyel/assignment_thoreddit.git"
},
"author": "Dan and Stephanie",
"license": "ISC",
"bugs": {
"url": "https://github.com/Avonyel/assignment_thoreddit/issues"
},
"homepage": "https://github.com/Avonyel/assignment_thoreddit#readme",
"dependencies": {
"bluebird": "^3.5.0",
"body-parser": "^1.17.2",
"express": "^4.15.4",
"express-handlebars": "^3.0.0",
"express-method-override-get-post-support": "0.0.7",
"method-override": "^2.3.9",
"mongoose": "^4.11.6",
"mongooseeder": "^2.0.5"
}
}
209 changes: 209 additions & 0 deletions queries.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
const mongoose = require("mongoose");
var models = require("./models");
var cp = require("child_process");

Object.keys(models).forEach(modelName => {
global[modelName] = mongoose.model(modelName);
});

// ----------------------------------------
// Mongoose Queries
// ----------------------------------------

var _num = 0;

require("./mongo")()
// ----------------------------------------
// Seed
// ----------------------------------------

// ----------------------------------------
// Find
// ----------------------------------------
.then(() => {
return User.find().then(_lg("User.find"));
})
// .then(() => {
// return User.findOne().then(_lg("User.findOne"));
// })
// .then(user => {
// return User.findById(user.id).then(_lg("User.findById"));
// })
// .then(user => {
// return User.findByIdAndUpdate(user.id, {
// fname: "Updated!",
// lname: "Updated!"
// }).then(_lg("User.findByIdAndUpdate"));
// })
// .then(user => {
// return User.findByIdAndRemove(user.id).then(_lg("User.findByIdAndRemove"));
// })
// .then(user => {
// return User.findOne({}, { _id: 0, fname: 1, lname: 1 }).then(
// _lg("User.findOne Projection")
// );
// })
// // ----------------------------------------
// // Filtering Documents
// // ----------------------------------------
// .then(() => {
// return Rating.find({
// value: { $gte: 2 }
// })
// .limit(10)
// .then(_lg("Rating.find"));
// })
// // ----------------------------------------
// // $where
// // ----------------------------------------
// .then(() => {
// return Rating.$where("this.value >= 2")
// .limit(10)
// .then(_lg("Rating.$where"));
// })
// // ----------------------------------------
// // where
// // ----------------------------------------
// .then(() => {
// return Rating.where("value").gte(2).limit(10).then(_lg("Rating.where"));
// })
// // ----------------------------------------
// // Limiting and Offsetting
// // ----------------------------------------
// .then(() => {
// return Rating.find(
// {},
// {},
// {
// limit: 10,
// skip: 10
// }
// ).then(_lg("Rating.skip.limit"));
// })
// .then(() => {
// return Rating.find().limit(10).skip(10).then(_lg("Rating.skip.limit"));
// })
// // ----------------------------------------
// // Create
// // ----------------------------------------
// .then(() => {
// return User.create({
// fname: "Just",
// lname: "Created",
// username: "justcreated",
// email: "[email protected]"
// }).then(_lg("User.create"));
// })
// // ----------------------------------------
// // Update
// // ----------------------------------------
// .then(() => {
// return User.update({
// fname: "Update",
// lname: "Again!"
// }).then(_lg("User.update"));
// })
// .then(() => {
// return User.updateMany({
// fname: "All",
// lname: "Same"
// }).then(_lg("User.updateMany"));
// })
// // ----------------------------------------
// // Destroy
// // ----------------------------------------
// .then(() => {
// return User.remove({
// username: "justcreated"
// })
// .then(r => (r ? 1 : 0))
// .then(_lg("User.remove"));
// })
// // ----------------------------------------
// // Sorting
// // ----------------------------------------
// .then(() => {
// return Rating.find(
// {},
// {
// _id: 0,
// value: 1
// },
// {
// sort: { value: -1 }
// }
// ).then(_lg("Rating.find.sort"));
// })
// .then(() => {
// return Rating.find(
// {},
// {
// _id: 0,
// value: 1
// }
// )
// .sort({ value: -1 })
// .then(_lg("Rating.find.sort"));
// })
// // ----------------------------------------
// // Instances
// // ----------------------------------------
// .then(() => {
// return Motel.findOne().then(_lg("Motel.findOne"));
// })
// .then(motel => {
// motel.name = "Bates Motel";
// return motel.save().then(_lg("motel.save"));
// })
// // ----------------------------------------
// // Populate
// // ----------------------------------------
// .then(() => {
// return Rating.find()
// .limit(10)
// .populate("user")
// .then(_lg("Rating.find.populate"));
// })
// .then(() => {
// return Rating.find()
// .limit(10)
// .populate({
// path: "user",
// select: ["fname", "lname"]
// })
// .then(_lg("Rating.find.populate({ path, select })"));
// })
// // ----------------------------------------
// // Map Reduce
// // ----------------------------------------
// .then(() => {
// return Rating.mapReduce({
// map: function() {
// emit(this.ratable, this.value);
// },
// reduce: function(key, values) {
// if (values.length) {
// var total = values.reduce((sum, value) => {
// return (sum += value);
// }, 0);
// return total / values.length;
// }
//
// return 0;
// }
// }).then(_lg("Rating.mapReduce"));
// })
// // ----------------------------------------
// // Aggregate Pipeline
// // ----------------------------------------
// .then(() => {
// return Rating.aggregate([
// { $group: { _id: "$ratable", rating: { $avg: "$value" } } },
// { $sort: { rating: -1 } }
// ]).then(_lg("Rating.aggregate"));
// })
// // ----------------------------------------
// // Finish
// ----------------------------------------
.catch(e => console.error(e.stack))
.then(() => mongoose.disconnect());
Loading