From c2f96fe53fe93c77d0233b13a0c46abaa604b564 Mon Sep 17 00:00:00 2001 From: Kara Thrash Date: Tue, 8 Aug 2017 11:31:05 -0400 Subject: [PATCH 01/13] gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..40b878d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules/ \ No newline at end of file From 212ba9ada2aed533a5be99355afb91ea12465ff6 Mon Sep 17 00:00:00 2001 From: Stephanie Barker Date: Tue, 8 Aug 2017 11:47:46 -0400 Subject: [PATCH 02/13] initial setup --- .gitignore | 1 + README.md | 2 + app.js | 25 +++++++++ config/mongo.json | 13 +++++ data_structure.txt | 11 ++++ models/index.js | 15 ++++++ mongo.js | 10 ++++ package.json | 26 ++++++++++ views/index.handlebars | 1 + views/layouts/main.handlebars | 24 +++++++++ views/login.handlebars | 11 ++++ views/newProfile.handlebars | 97 +++++++++++++++++++++++++++++++++++ views/signup.handlebars | 11 ++++ 13 files changed, 247 insertions(+) create mode 100644 .gitignore create mode 100644 app.js create mode 100644 config/mongo.json create mode 100644 data_structure.txt create mode 100644 models/index.js create mode 100644 mongo.js create mode 100644 package.json create mode 100644 views/index.handlebars create mode 100644 views/layouts/main.handlebars create mode 100644 views/login.handlebars create mode 100644 views/newProfile.handlebars create mode 100644 views/signup.handlebars diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..40b878d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules/ \ No newline at end of file diff --git a/README.md b/README.md index 7513987..1a69fc5 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,4 @@ # assignment_thoreddit A social news web application for Viking thunder Gods + +by Dan and Stephanie \ No newline at end of file diff --git a/app.js b/app.js new file mode 100644 index 0000000..0d02a64 --- /dev/null +++ b/app.js @@ -0,0 +1,25 @@ +const express = require("express"); +const app = express(); +const bodyParser = require("body-parser"); +const exphbs = require("express-handlebars"); + +app.use(bodyParser.urlencoded({ extended: true })); + +const hbs = exphbs.create({ + partialsDir: "views/", + 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]); + +args.push(() => { + console.log(`Listening: http://${host}:${port}`); +}); + +app.listen.apply(app, args); diff --git a/config/mongo.json b/config/mongo.json new file mode 100644 index 0000000..36c68bc --- /dev/null +++ b/config/mongo.json @@ -0,0 +1,13 @@ +{ + "development": { + "database": "thoreddit_development", + "host": "localhost" + }, + "test": { + "database": "thoreddit_test", + "host": "localhost" + }, + "production": { + "use_env_variable": "MONGODB_URI" + } +} diff --git a/data_structure.txt b/data_structure.txt new file mode 100644 index 0000000..52fd232 --- /dev/null +++ b/data_structure.txt @@ -0,0 +1,11 @@ +Users + username (string) + email (string) + posts (array) + +Posts + title (string) + body (text) + votes (integer) + topLevel (boolean) + comments (array) \ No newline at end of file diff --git a/models/index.js b/models/index.js new file mode 100644 index 0000000..757645a --- /dev/null +++ b/models/index.js @@ -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; diff --git a/mongo.js b/mongo.js new file mode 100644 index 0000000..1f7a93c --- /dev/null +++ b/mongo.js @@ -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); +}; diff --git a/package.json b/package.json new file mode 100644 index 0000000..8584069 --- /dev/null +++ b/package.json @@ -0,0 +1,26 @@ +{ + "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", + "mongoose": "^4.11.6" + } +} diff --git a/views/index.handlebars b/views/index.handlebars new file mode 100644 index 0000000..baabcaf --- /dev/null +++ b/views/index.handlebars @@ -0,0 +1 @@ +

You logged in!

\ No newline at end of file diff --git a/views/layouts/main.handlebars b/views/layouts/main.handlebars new file mode 100644 index 0000000..ef01928 --- /dev/null +++ b/views/layouts/main.handlebars @@ -0,0 +1,24 @@ + + + + + + + OkOdin + + + + + + + + + + + +
+ {{{ body }}} +
+ + + diff --git a/views/login.handlebars b/views/login.handlebars new file mode 100644 index 0000000..a932c09 --- /dev/null +++ b/views/login.handlebars @@ -0,0 +1,11 @@ +
+
+ + +
+
+ + +
+ +
diff --git a/views/newProfile.handlebars b/views/newProfile.handlebars new file mode 100644 index 0000000..e1389ac --- /dev/null +++ b/views/newProfile.handlebars @@ -0,0 +1,97 @@ +
+
+ + +
+
+ + +
+
+ + +
+
+ + Male + Female +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + + + + +
+
+ + +
+ + +
+ + +
+
+ + +
+
+ + +
+
+ + +
+ + +
diff --git a/views/signup.handlebars b/views/signup.handlebars new file mode 100644 index 0000000..16fa78b --- /dev/null +++ b/views/signup.handlebars @@ -0,0 +1,11 @@ +
+
+ + +
+
+ + +
+ +
From 61cfc7f35675b57ef9efa49afd177d41adcf45ac Mon Sep 17 00:00:00 2001 From: Kara Thrash Date: Tue, 8 Aug 2017 12:14:59 -0400 Subject: [PATCH 03/13] added models started seeders --- app.js | 6 +++--- data_structure.txt | 3 ++- models/post.js | 18 ++++++++++++++++++ models/user.js | 15 +++++++++++++++ seeders/index.js | 33 +++++++++++++++++++++++++++++++++ 5 files changed, 71 insertions(+), 4 deletions(-) create mode 100644 models/post.js create mode 100644 models/user.js create mode 100644 seeders/index.js diff --git a/app.js b/app.js index 0d02a64..4512af7 100644 --- a/app.js +++ b/app.js @@ -6,8 +6,8 @@ const exphbs = require("express-handlebars"); app.use(bodyParser.urlencoded({ extended: true })); const hbs = exphbs.create({ - partialsDir: "views/", - defaultLayout: "main" + partialsDir: "views/", + defaultLayout: "main" }); app.engine("handlebars", hbs.engine); app.set("view engine", "handlebars"); @@ -19,7 +19,7 @@ var args; process.env.NODE_ENV === "production" ? (args = [port]) : (args = [port, host]); args.push(() => { - console.log(`Listening: http://${host}:${port}`); + console.log(`Listening: http://${host}:${port}`); }); app.listen.apply(app, args); diff --git a/data_structure.txt b/data_structure.txt index 52fd232..0f12f75 100644 --- a/data_structure.txt +++ b/data_structure.txt @@ -5,7 +5,8 @@ Users Posts title (string) + author (string) body (text) votes (integer) topLevel (boolean) - comments (array) \ No newline at end of file + comments (array) diff --git a/models/post.js b/models/post.js new file mode 100644 index 0000000..24eddc4 --- /dev/null +++ b/models/post.js @@ -0,0 +1,18 @@ +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" }] + }, + { timestamps: true } +); + +var Post = mongoose.model("Post", PostSchema); + +module.exports = Post; diff --git a/models/user.js b/models/user.js new file mode 100644 index 0000000..fca78f3 --- /dev/null +++ b/models/user.js @@ -0,0 +1,15 @@ +var mongoose = require("mongoose"); +var Schema = mongoose.Schema; + +var UserSchema = ( + { + username: String, + email: String, + posts: [{ type: Schema.Types.ObjectId, ref: "Post" }] + }, + { timestamps: true } +); + +var User = mongoose.model("User", UserSchema); + +module.exports = User; diff --git a/seeders/index.js b/seeders/index.js new file mode 100644 index 0000000..4fddc80 --- /dev/null +++ b/seeders/index.js @@ -0,0 +1,33 @@ +const mongoose = require("mongoose"); +const mongooseeder = require("mongooseeder"); +var env = process.env.NODE_ENV || "development"; +var config = require("./../config/mongo")[env]; +const models = require("../models"); +const { User, Post } = models; +const mongodbUrl = + process.env.NODE_ENV === "production" + ? process.env[config.use_env_variable] + : `mongodb://${config.host}/${config.database}`; + +const seeds = () => { + var promises = []; + [users, posts].forEach(collection => { + collection.forEach(model => { + promises.push(model.save()); + }); + }); + return Promise.all(promises); +}; + +mongooseeder.seed({ + mongodbUrl: mongodbUrl, + models: models, + clean: true, + mongoose: mongoose, + seeds: () => { + // Run your seeds here + // Example: + return models.User.create({ email }); + return models.Post.create({ email }); + } +}); From f20666360d4e3daa26b3e11e35357501d714ba85 Mon Sep 17 00:00:00 2001 From: Stephanie Barker Date: Tue, 8 Aug 2017 12:41:28 -0400 Subject: [PATCH 04/13] still working on seeders --- models/user.js | 12 ++++++------ package.json | 3 ++- seeders/index.js | 41 ++++++++++++++++++++++++++++++++++------- 3 files changed, 42 insertions(+), 14 deletions(-) diff --git a/models/user.js b/models/user.js index fca78f3..a1a30dd 100644 --- a/models/user.js +++ b/models/user.js @@ -2,12 +2,12 @@ var mongoose = require("mongoose"); var Schema = mongoose.Schema; var UserSchema = ( - { - username: String, - email: String, - posts: [{ type: Schema.Types.ObjectId, ref: "Post" }] - }, - { timestamps: true } + { + username: String, + email: String, + posts: [{ type: Schema.Types.ObjectId, ref: "Post" }] + }, + { timestamps: true } ); var User = mongoose.model("User", UserSchema); diff --git a/package.json b/package.json index 8584069..18c041c 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,7 @@ "body-parser": "^1.17.2", "express": "^4.15.4", "express-handlebars": "^3.0.0", - "mongoose": "^4.11.6" + "mongoose": "^4.11.6", + "mongooseeder": "^2.0.5" } } diff --git a/seeders/index.js b/seeders/index.js index 4fddc80..b01bc3f 100644 --- a/seeders/index.js +++ b/seeders/index.js @@ -2,7 +2,7 @@ const mongoose = require("mongoose"); const mongooseeder = require("mongooseeder"); var env = process.env.NODE_ENV || "development"; var config = require("./../config/mongo")[env]; -const models = require("../models"); +const models = require("./../models"); const { User, Post } = models; const mongodbUrl = process.env.NODE_ENV === "production" @@ -10,6 +10,38 @@ const mongodbUrl = : `mongodb://${config.host}/${config.database}`; const seeds = () => { + console.log("Creating Users"); + var users = []; + for (let i = 1; i < 11; i++) { + var user = new User({ + username: `foobar${i}`, + email: `foobar${i}@gmail.com`, + posts: [i, i + 10] + }); + users.push(user); + } + + console.log("Creating Posts"); + var posts = []; + var authorId; + + for (let i = 1; i < 21; i++) { + authorId = i; + + if (i > 10) { + authorId -= 10; + } + + var post = new Post({ + title: `Title of ${i}`, + author: authorId, + body: `Blah blah blah blah ${i}`, + topLevel: true, + subPosts: [] + }); + posts.push(post); + } + var promises = []; [users, posts].forEach(collection => { collection.forEach(model => { @@ -24,10 +56,5 @@ mongooseeder.seed({ models: models, clean: true, mongoose: mongoose, - seeds: () => { - // Run your seeds here - // Example: - return models.User.create({ email }); - return models.Post.create({ email }); - } + seeds: seeds }); From 7869188aa8bc016ccf982acfa7f9d2a20f64ac59 Mon Sep 17 00:00:00 2001 From: Kara Thrash Date: Tue, 8 Aug 2017 13:34:32 -0400 Subject: [PATCH 05/13] something something router and users update + seeds --- app.js | 13 +++ models/post.js | 19 ++-- models/user.js | 15 +-- queries.js | 209 +++++++++++++++++++++++++++++++++++ routers/posts.js | 102 +++++++++++++++++ routers/ratables.js | 41 +++++++ routers/sessions.js | 31 ++++++ routers/users.js | 102 +++++++++++++++++ seeders/index.js | 5 +- views/index.handlebars | 7 +- views/users/edit.handlebars | 51 +++++++++ views/users/index.handlebars | 59 ++++++++++ views/users/new.handlebars | 49 ++++++++ views/users/show.handlebars | 31 ++++++ 14 files changed, 711 insertions(+), 23 deletions(-) create mode 100644 queries.js create mode 100644 routers/posts.js create mode 100644 routers/ratables.js create mode 100644 routers/sessions.js create mode 100644 routers/users.js create mode 100644 views/users/edit.handlebars create mode 100644 views/users/index.handlebars create mode 100644 views/users/new.handlebars create mode 100644 views/users/show.handlebars diff --git a/app.js b/app.js index 4512af7..c4a7645 100644 --- a/app.js +++ b/app.js @@ -2,6 +2,7 @@ const express = require("express"); const app = express(); const bodyParser = require("body-parser"); const exphbs = require("express-handlebars"); +const mongoose = require("mongoose"); app.use(bodyParser.urlencoded({ extended: true })); @@ -17,9 +18,21 @@ var host = "localhost"; var args; process.env.NODE_ENV === "production" ? (args = [port]) : (args = [port, host]); +var sessionsRouter = require("./routers/sessions")(app); +app.use("/", sessionsRouter); + +var usersRouter = require("./routers/users"); +app.use("/users", usersRouter); args.push(() => { console.log(`Listening: http://${host}:${port}`); }); +app.use((req, res, next) => { + if (mongoose.connection.readyState) { + next(); + } else { + require("./mongo")().then(() => next()); + } +}); app.listen.apply(app, args); diff --git a/models/post.js b/models/post.js index 24eddc4..f4a133d 100644 --- a/models/post.js +++ b/models/post.js @@ -1,17 +1,14 @@ 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" }] - }, - { timestamps: true } -); +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); diff --git a/models/user.js b/models/user.js index a1a30dd..53eb7c7 100644 --- a/models/user.js +++ b/models/user.js @@ -1,15 +1,12 @@ var mongoose = require("mongoose"); var Schema = mongoose.Schema; -var UserSchema = ( - { - username: String, - email: String, - posts: [{ type: Schema.Types.ObjectId, ref: "Post" }] - }, - { timestamps: true } -); - +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; diff --git a/queries.js b/queries.js new file mode 100644 index 0000000..ef8890c --- /dev/null +++ b/queries.js @@ -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: "just@created.com" + // }).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()); diff --git a/routers/posts.js b/routers/posts.js new file mode 100644 index 0000000..6205934 --- /dev/null +++ b/routers/posts.js @@ -0,0 +1,102 @@ +var express = require("express"); +var router = express.Router(); +const mongoose = require("mongoose"); +var models = require("./../models"); +var Post = mongoose.model("Post"); + +// ---------------------------------------- +// Index +// ---------------------------------------- +router.get("/", (req, res) => { + Post.find({}) + .then(users => { + res.render("index", { users }); + }) + .catch(e => res.status(500).send(e.stack)); +}); + +// ---------------------------------------- +// New +// ---------------------------------------- +router.get("/new", (req, res) => { + res.render("new"); +}); + +// ---------------------------------------- +// Edit +// ---------------------------------------- +router.get("/:id/edit", (req, res) => { + Post.findById(req.params.id) + .then(post => { + res.render("posts/edit", { post }); + }) + .catch(e => res.status(500).send(e.stack)); +}); + +// ---------------------------------------- +// Show +// ---------------------------------------- +router.get("/:id", (req, res) => { + Post.findById(req.params.id) + .then(post => { + res.render("posts/show", { post }); + }) + .catch(e => res.status(500).send(e.stack)); +}); + +// ---------------------------------------- +// Create +// ---------------------------------------- +router.post("/", (req, res) => { + var post = new Post({ + fname: req.body.user.fname, + lname: req.body.user.lname, + username: req.body.user.username, + email: req.body.user.email + }); + + post + .save() + .then(post => { + res.redirect(`/posts/${post.id}`); + }) + .catch(e => res.status(500).send(e.stack)); +}); + +// ---------------------------------------- +// Update +// ---------------------------------------- +router.put("/:id", (req, res) => { + var postParams = { + fname: req.body.post.fname, + lname: req.body.post.lname, + username: req.body.post.postname, + email: req.body.post.email + }; + + User.findByIdAndUpdate(req.params.id, postParams) + .then(post => { + req.method = "GET"; + res.redirect(`/posts/${post.id}`); + }) + .catch(e => res.status(500).send(e.stack)); +}); + +// ---------------------------------------- +// Destroy +// ---------------------------------------- +router.delete("/:id", (req, res) => { + var currentPost = req.session.currentPost; + Post.findByIdAndRemove(req.params.id) + .then(() => { + req.method = "GET"; + if (currentPost.id === req.params.id) { + res.redirect("/logout"); + } else { + res.redirect("/posts"); + } + }) + .catch(e => res.status(500).send(e.stack)); +}); + +module.exports = router; diff --git a/routers/ratables.js b/routers/ratables.js new file mode 100644 index 0000000..b592c80 --- /dev/null +++ b/routers/ratables.js @@ -0,0 +1,41 @@ +var express = require('express'); +var router = express.Router(); +const mongoose = require('mongoose'); +var models = require('./../models'); +var Hotel = mongoose.model('Hotel'); +var Motel = mongoose.model('Motel'); + + +// ---------------------------------------- +// Index +// ---------------------------------------- +router.get('/', (req, res) => { + var hotels; + var motels; + Hotel.find({}).populate('ratings') + .then((results) => { + hotels = results; + return Motel.find({}).populate('ratings'); + }) + .then((results) => { + motels = results; + res.render('ratables/index', { hotels, motels }); + }) + .catch((e) => res.status(500).send(e.stack)); +}); + + + + +module.exports = router; + + + + + + + + + + + diff --git a/routers/sessions.js b/routers/sessions.js new file mode 100644 index 0000000..442d5c8 --- /dev/null +++ b/routers/sessions.js @@ -0,0 +1,31 @@ +var url = require("url"); +var express = require("express"); +var router = express.Router(); +const mongoose = require("mongoose"); +var models = require("./../models"); +var User = mongoose.model("User"); + +module.exports = app => { + // Auth + + // New + // var onNew = (req, res) => { + // if (req.session.currentUser) { + // res.redirect("/users"); + // } else { + // res.render("sessions/new"); + // } + // + // }; + router.get("/"); + router.get("/login"); + + // Create + + // Destroy + + router.get("/logout"); + router.delete("/logout"); + + return router; +}; diff --git a/routers/users.js b/routers/users.js new file mode 100644 index 0000000..c192d7d --- /dev/null +++ b/routers/users.js @@ -0,0 +1,102 @@ +var express = require("express"); +var router = express.Router(); +const mongoose = require("mongoose"); +var models = require("./../models"); +var User = mongoose.model("User"); + +// ---------------------------------------- +// Index +// ---------------------------------------- +router.get("/", (req, res) => { + User.find({}) + .then(users => { + res.render("users/index", { users }); + }) + .catch(e => res.status(500).send(e.stack)); +}); + +// ---------------------------------------- +// New +// ---------------------------------------- +router.get("/new", (req, res) => { + res.render("new"); +}); + +// ---------------------------------------- +// Edit +// ---------------------------------------- +router.get("/:id/edit", (req, res) => { + User.findById(req.params.id) + .then(user => { + res.render("users/edit", { user }); + }) + .catch(e => res.status(500).send(e.stack)); +}); + +// ---------------------------------------- +// Show +// ---------------------------------------- +router.get("/:id", (req, res) => { + User.findById(req.params.id) + .then(user => { + res.render("users/show", { user }); + }) + .catch(e => res.status(500).send(e.stack)); +}); + +// ---------------------------------------- +// Create +// ---------------------------------------- +router.post("/", (req, res) => { + var user = new User({ + fname: req.body.user.fname, + lname: req.body.user.lname, + username: req.body.user.username, + email: req.body.user.email + }); + + user + .save() + .then(user => { + res.redirect(`/users/${user.id}`); + }) + .catch(e => res.status(500).send(e.stack)); +}); + +// ---------------------------------------- +// Update +// ---------------------------------------- +router.put("/:id", (req, res) => { + var userParams = { + fname: req.body.user.fname, + lname: req.body.user.lname, + username: req.body.user.username, + email: req.body.user.email + }; + + User.findByIdAndUpdate(req.params.id, userParams) + .then(user => { + req.method = "GET"; + res.redirect(`/users/${user.id}`); + }) + .catch(e => res.status(500).send(e.stack)); +}); + +// ---------------------------------------- +// Destroy +// ---------------------------------------- +router.delete("/:id", (req, res) => { + var currentUser = req.session.currentUser; + User.findByIdAndRemove(req.params.id) + .then(() => { + req.method = "GET"; + if (currentUser.id === req.params.id) { + res.redirect("/logout"); + } else { + res.redirect("/users"); + } + }) + .catch(e => res.status(500).send(e.stack)); +}); + +module.exports = router; diff --git a/seeders/index.js b/seeders/index.js index b01bc3f..a8ed8ac 100644 --- a/seeders/index.js +++ b/seeders/index.js @@ -16,7 +16,7 @@ const seeds = () => { var user = new User({ username: `foobar${i}`, email: `foobar${i}@gmail.com`, - posts: [i, i + 10] + posts: [] }); users.push(user); } @@ -34,11 +34,12 @@ const seeds = () => { var post = new Post({ title: `Title of ${i}`, - author: authorId, + author: users[authorId], body: `Blah blah blah blah ${i}`, topLevel: true, subPosts: [] }); + users[authorId - 1].posts.push(post); posts.push(post); } diff --git a/views/index.handlebars b/views/index.handlebars index baabcaf..1fe33eb 100644 --- a/views/index.handlebars +++ b/views/index.handlebars @@ -1 +1,6 @@ -

You logged in!

\ No newline at end of file +

You logged in!

+ + +{{#each users as |user|}} +
{{user.username}}
+{{/each}} diff --git a/views/users/edit.handlebars b/views/users/edit.handlebars new file mode 100644 index 0000000..4911076 --- /dev/null +++ b/views/users/edit.handlebars @@ -0,0 +1,51 @@ + + + + + +
+ + +
+ + +
+ +
+ + +
+ +
+ + +
+ +
+ + +
+ +
+ +
+
+ + + + + + + + + + + + + + + + + diff --git a/views/users/index.handlebars b/views/users/index.handlebars new file mode 100644 index 0000000..204235f --- /dev/null +++ b/views/users/index.handlebars @@ -0,0 +1,59 @@ + + + + + +{{#if users.length }} + + + + + + + + + + + + + {{#each users as |user| }} + + + + + + + + + {{/each }} + +
First NameLast NameUsernameEmail
+ {{ user.fname }} + + {{ user.lname }} + + {{ user.username }} + + {{ user.email }} + + Edit + + Delete +
+{{else }} +

No users

+{{/if }} + + + + + + + + + + + diff --git a/views/users/new.handlebars b/views/users/new.handlebars new file mode 100644 index 0000000..c0c502a --- /dev/null +++ b/views/users/new.handlebars @@ -0,0 +1,49 @@ + + + + + +
+
+ + +
+ +
+ + +
+ +
+ + +
+ +
+ + +
+ +
+ +
+
+ + + + + + + + + + + + + + + + + diff --git a/views/users/show.handlebars b/views/users/show.handlebars new file mode 100644 index 0000000..4b06e22 --- /dev/null +++ b/views/users/show.handlebars @@ -0,0 +1,31 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + +
KeyValue
First Name{{ user.username }}
Last Name{{ user.email }}
Username{{ user.posts.length }}
From d8d47908eb05e1b57dfdb9fc7295301d5c4d81ab Mon Sep 17 00:00:00 2001 From: Stephanie Barker Date: Tue, 8 Aug 2017 13:58:11 -0400 Subject: [PATCH 06/13] making user routes --- app.js | 27 ++++++++++++++++++--------- package.json | 2 ++ routers/users.js | 4 ---- views/users/edit.handlebars | 11 ----------- views/users/index.handlebars | 8 -------- views/users/show.handlebars | 8 ++++---- 6 files changed, 24 insertions(+), 36 deletions(-) diff --git a/app.js b/app.js index c4a7645..7abc86a 100644 --- a/app.js +++ b/app.js @@ -3,12 +3,15 @@ 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/", - defaultLayout: "main" + partialsDir: "views/", + defaultLayout: "main" }); app.engine("handlebars", hbs.engine); app.set("view engine", "handlebars"); @@ -19,20 +22,26 @@ 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); args.push(() => { - console.log(`Listening: http://${host}:${port}`); + console.log(`Listening: http://${host}:${port}`); }); -app.use((req, res, next) => { - if (mongoose.connection.readyState) { - next(); - } else { - require("./mongo")().then(() => next()); - } + +app.get("/", (req, res) => { + res.redirect("/users"); }); app.listen.apply(app, args); diff --git a/package.json b/package.json index 18c041c..4b43c15 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,8 @@ "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" } diff --git a/routers/users.js b/routers/users.js index c192d7d..fda1e4e 100644 --- a/routers/users.js +++ b/routers/users.js @@ -49,8 +49,6 @@ router.get("/:id", (req, res) => { // ---------------------------------------- router.post("/", (req, res) => { var user = new User({ - fname: req.body.user.fname, - lname: req.body.user.lname, username: req.body.user.username, email: req.body.user.email }); @@ -68,8 +66,6 @@ router.post("/", (req, res) => { // ---------------------------------------- router.put("/:id", (req, res) => { var userParams = { - fname: req.body.user.fname, - lname: req.body.user.lname, username: req.body.user.username, email: req.body.user.email }; diff --git a/views/users/edit.handlebars b/views/users/edit.handlebars index 4911076..0bcfec1 100644 --- a/views/users/edit.handlebars +++ b/views/users/edit.handlebars @@ -7,17 +7,6 @@
- -
- - -
- -
- - -
-
diff --git a/views/users/index.handlebars b/views/users/index.handlebars index 204235f..0ea1548 100644 --- a/views/users/index.handlebars +++ b/views/users/index.handlebars @@ -10,8 +10,6 @@ - - @@ -21,12 +19,6 @@ {{#each users as |user| }} - - diff --git a/views/users/show.handlebars b/views/users/show.handlebars index 4b06e22..10eb1cf 100644 --- a/views/users/show.handlebars +++ b/views/users/show.handlebars @@ -1,7 +1,7 @@ @@ -16,15 +16,15 @@ - + - + - + From 2062ebd533810b9f161cd294f6309eaef1e528ff Mon Sep 17 00:00:00 2001 From: Kara Thrash Date: Tue, 8 Aug 2017 16:16:44 -0400 Subject: [PATCH 07/13] posts and partials --- app.js | 21 +++++++------- routers/posts.js | 26 +++++++++-------- seeders/index.js | 28 +++++++++++++++++-- views/Partials/postPartial.handlebars | 9 ++++++ views/posts/edit.handlebars | 40 +++++++++++++++++++++++++++ views/posts/index.handlebars | 16 +++++++++++ views/posts/new.handlebars | 24 ++++++++++++++++ views/posts/show.handlebars | 8 ++++++ 8 files changed, 149 insertions(+), 23 deletions(-) create mode 100644 views/Partials/postPartial.handlebars create mode 100644 views/posts/edit.handlebars create mode 100644 views/posts/index.handlebars create mode 100644 views/posts/new.handlebars create mode 100644 views/posts/show.handlebars diff --git a/app.js b/app.js index 7abc86a..319d7ba 100644 --- a/app.js +++ b/app.js @@ -10,8 +10,8 @@ app.use(bodyParser.urlencoded({ extended: true })); app.use(methodOverride(getPostSupport.callback, getPostSupport.options)); const hbs = exphbs.create({ - partialsDir: "views/", - defaultLayout: "main" + partialsDir: "views/", + defaultLayout: "main" }); app.engine("handlebars", hbs.engine); app.set("view engine", "handlebars"); @@ -24,24 +24,25 @@ 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()); - } + 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}`); + console.log(`Listening: http://${host}:${port}`); }); app.get("/", (req, res) => { - res.redirect("/users"); + res.redirect("/users"); }); app.listen.apply(app, args); diff --git a/routers/posts.js b/routers/posts.js index 6205934..ad84c77 100644 --- a/routers/posts.js +++ b/routers/posts.js @@ -8,9 +8,9 @@ var Post = mongoose.model("Post"); // Index // ---------------------------------------- router.get("/", (req, res) => { - Post.find({}) - .then(users => { - res.render("index", { users }); + Post.find({ topLevel: true }) + .then(posts => { + res.render("posts/index", { posts }); }) .catch(e => res.status(500).send(e.stack)); }); @@ -49,10 +49,12 @@ router.get("/:id", (req, res) => { // ---------------------------------------- router.post("/", (req, res) => { var post = new Post({ - fname: req.body.user.fname, - lname: req.body.user.lname, - username: req.body.user.username, - email: req.body.user.email + title: req.body.post.title, + author: req.body.post.author, + body: req.body.post.body, + votes: req.body.post.votes, + topLevel: req.body.post.topLevel, + subPosts: req.body.post.subPosts }); post @@ -68,10 +70,12 @@ router.post("/", (req, res) => { // ---------------------------------------- router.put("/:id", (req, res) => { var postParams = { - fname: req.body.post.fname, - lname: req.body.post.lname, - username: req.body.post.postname, - email: req.body.post.email + title: req.body.post.title, + author: req.body.post.author, + body: req.body.post.body, + votes: req.body.post.votes, + topLevel: req.body.post.topLevel, + subPosts: req.body.post.subPosts }; User.findByIdAndUpdate(req.params.id, postParams) diff --git a/seeders/index.js b/seeders/index.js index a8ed8ac..bfda01c 100644 --- a/seeders/index.js +++ b/seeders/index.js @@ -26,7 +26,7 @@ const seeds = () => { var authorId; for (let i = 1; i < 21; i++) { - authorId = i; + authorId = i - 1; if (i > 10) { authorId -= 10; @@ -39,10 +39,34 @@ const seeds = () => { topLevel: true, subPosts: [] }); - users[authorId - 1].posts.push(post); + users[authorId].posts.push(post); posts.push(post); } + /// + var subPosts = []; + var authorId; + + for (let i = 21; i < 41; i++) { + authorId = 1; + + if (authorId > 10) { + authorId -= 10; + } + + var subPost = new Post({ + title: `Title of ${i}`, + author: users[authorId], + body: `Blah blah blah blah ${i}`, + topLevel: false, + subPosts: [] + }); + posts[authorId].subPosts.push(subPost); + users[authorId].posts.push(post); + posts.push(subPost); + } + /// + var promises = []; [users, posts].forEach(collection => { collection.forEach(model => { diff --git a/views/Partials/postPartial.handlebars b/views/Partials/postPartial.handlebars new file mode 100644 index 0000000..95c7d86 --- /dev/null +++ b/views/Partials/postPartial.handlebars @@ -0,0 +1,9 @@ + + +{{#each posts as |post|}} +

{{ post.title }} {{ post.author }} {{ post.votes }}

+

{{ post.body }}

+ {{ if post.subPosts.length}} + {{>postPartial.handlebars}} + {{/if}} +{{/each }} diff --git a/views/posts/edit.handlebars b/views/posts/edit.handlebars new file mode 100644 index 0000000..0bcfec1 --- /dev/null +++ b/views/posts/edit.handlebars @@ -0,0 +1,40 @@ + + + + + + + +
+ + +
+ +
+ + +
+ +
+ +
+ + + + + + + + + + + + + + + + + + diff --git a/views/posts/index.handlebars b/views/posts/index.handlebars new file mode 100644 index 0000000..9ea23fb --- /dev/null +++ b/views/posts/index.handlebars @@ -0,0 +1,16 @@ + + + + + + {{#each posts as |post| }} + + {{/each }} diff --git a/views/posts/new.handlebars b/views/posts/new.handlebars new file mode 100644 index 0000000..44894a3 --- /dev/null +++ b/views/posts/new.handlebars @@ -0,0 +1,24 @@ + + + + + +
+
+ + +
+ +
+ + +
+ + + +
+ +
+ diff --git a/views/posts/show.handlebars b/views/posts/show.handlebars new file mode 100644 index 0000000..7e11e78 --- /dev/null +++ b/views/posts/show.handlebars @@ -0,0 +1,8 @@ + + + +

{{ post.title }} {{ post.author }} {{ post.votes }}

+

{{ post.body }}

+ {{#if post.subPosts.length}} + {{>postPartial.handlebars}} + {{/if}} From 944241dcb5c9a9c19c0a2c11e7a76ed9f69dace6 Mon Sep 17 00:00:00 2001 From: Stephanie Barker Date: Tue, 8 Aug 2017 16:17:13 -0400 Subject: [PATCH 08/13] alterations --- seeders/index.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/seeders/index.js b/seeders/index.js index a8ed8ac..ebcba73 100644 --- a/seeders/index.js +++ b/seeders/index.js @@ -26,9 +26,9 @@ const seeds = () => { var authorId; for (let i = 1; i < 21; i++) { - authorId = i; + authorId = i - 1; - if (i > 10) { + if (i > 9) { authorId -= 10; } @@ -36,10 +36,11 @@ const seeds = () => { title: `Title of ${i}`, author: users[authorId], body: `Blah blah blah blah ${i}`, + votes: 0, topLevel: true, subPosts: [] }); - users[authorId - 1].posts.push(post); + users[authorId].posts.push(post); posts.push(post); } From d10064280a9ee43465e5280b1c4adec5fdf57d54 Mon Sep 17 00:00:00 2001 From: Stephanie Barker Date: Tue, 8 Aug 2017 16:46:38 -0400 Subject: [PATCH 09/13] working on nesting subposts --- app.js | 18 ++++++++--------- routers/posts.js | 2 ++ seeders/index.js | 22 +++++++++++++++------ views/layouts/main.handlebars | 2 +- views/partials/postPartial.handlebars | 11 +++++++++++ views/{Partials => }/postPartial.handlebars | 2 -- views/posts/show.handlebars | 15 ++++++++------ 7 files changed, 48 insertions(+), 24 deletions(-) create mode 100644 views/partials/postPartial.handlebars rename views/{Partials => }/postPartial.handlebars (99%) diff --git a/app.js b/app.js index 319d7ba..7c2a1ad 100644 --- a/app.js +++ b/app.js @@ -10,8 +10,8 @@ app.use(bodyParser.urlencoded({ extended: true })); app.use(methodOverride(getPostSupport.callback, getPostSupport.options)); const hbs = exphbs.create({ - partialsDir: "views/", - defaultLayout: "main" + partialsDir: "views/partials", + defaultLayout: "main" }); app.engine("handlebars", hbs.engine); app.set("view engine", "handlebars"); @@ -24,11 +24,11 @@ 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()); - } + if (mongoose.connection.readyState) { + next(); + } else { + require("./mongo")().then(() => next()); + } }); app.use("/", sessionsRouter); @@ -38,11 +38,11 @@ app.use("/users", usersRouter); var postsRouter = require("./routers/posts"); app.use("/posts", postsRouter); args.push(() => { - console.log(`Listening: http://${host}:${port}`); + console.log(`Listening: http://${host}:${port}`); }); app.get("/", (req, res) => { - res.redirect("/users"); + res.redirect("/users"); }); app.listen.apply(app, args); diff --git a/routers/posts.js b/routers/posts.js index ad84c77..187a569 100644 --- a/routers/posts.js +++ b/routers/posts.js @@ -38,7 +38,9 @@ router.get("/:id/edit", (req, res) => { // ---------------------------------------- router.get("/:id", (req, res) => { Post.findById(req.params.id) + .populate("subPosts") .then(post => { + console.log(post); res.render("posts/show", { post }); }) .catch(e => res.status(500).send(e.stack)); diff --git a/seeders/index.js b/seeders/index.js index 162e43c..32eb14e 100644 --- a/seeders/index.js +++ b/seeders/index.js @@ -28,14 +28,14 @@ const seeds = () => { for (let i = 1; i < 21; i++) { authorId = i - 1; - if (i > 9) { + if (authorId > 9) { authorId -= 10; } var post = new Post({ title: `Title of ${i}`, author: users[authorId], - body: `Blah blah blah blah ${i}`, + body: `This is a post! ${i}`, votes: 0, topLevel: true, subPosts: [] @@ -48,20 +48,30 @@ const seeds = () => { var subPosts = []; var authorId; - for (let i = 21; i < 41; i++) { - authorId = 1; + for (let i = 0; i < 20; i++) { + authorId = i; - if (authorId > 10) { + if (authorId > 9) { authorId -= 10; } var subPost = new Post({ title: `Title of ${i}`, author: users[authorId], - body: `Blah blah blah blah ${i}`, + body: `This is a subpost! ${i}`, topLevel: false, subPosts: [] }); + + var subSubPost = new Post({ + title: `Title of ${i}`, + author: users[authorId], + body: `This is a sub-subpost! ${i}`, + topLevel: false, + subPosts: [] + }); + console.log(authorId); + subPost[authorId].subPosts.push(subSubPost); posts[authorId].subPosts.push(subPost); users[authorId].posts.push(post); posts.push(subPost); diff --git a/views/layouts/main.handlebars b/views/layouts/main.handlebars index ef01928..c0a72cc 100644 --- a/views/layouts/main.handlebars +++ b/views/layouts/main.handlebars @@ -17,7 +17,7 @@
- {{{ body }}} + {{{ body }}}
diff --git a/views/partials/postPartial.handlebars b/views/partials/postPartial.handlebars new file mode 100644 index 0000000..b09e7f2 --- /dev/null +++ b/views/partials/postPartial.handlebars @@ -0,0 +1,11 @@ +{{#each subPosts as |subPost|}} +
+
+

{{ subPost.title }} {{ subPost.author }} {{ subPost.votes }}

+

{{ subPost.body }}

+ {{#if subPost.subPosts.length}} + {{>postPartial subPosts=subPost.subPosts}} + {{/if}} +
+
+{{/each }} diff --git a/views/Partials/postPartial.handlebars b/views/postPartial.handlebars similarity index 99% rename from views/Partials/postPartial.handlebars rename to views/postPartial.handlebars index 95c7d86..0b2d54a 100644 --- a/views/Partials/postPartial.handlebars +++ b/views/postPartial.handlebars @@ -1,5 +1,3 @@ - - {{#each posts as |post|}}

{{ post.title }} {{ post.author }} {{ post.votes }}

{{ post.body }}

diff --git a/views/posts/show.handlebars b/views/posts/show.handlebars index 7e11e78..2e151c5 100644 --- a/views/posts/show.handlebars +++ b/views/posts/show.handlebars @@ -1,8 +1,11 @@ - -

{{ post.title }} {{ post.author }} {{ post.votes }}

-

{{ post.body }}

- {{#if post.subPosts.length}} - {{>postPartial.handlebars}} - {{/if}} +
+
+

{{ post.title }} {{ post.author }} {{ post.votes }}

+

{{ post.body }}

+ {{#if post.subPosts.length}} + {{>postPartial subPosts=post.subPosts}} + {{/if}} +
+
\ No newline at end of file From 2e2c0aef5532b8e31cabf7c7d186236d38dd4ea2 Mon Sep 17 00:00:00 2001 From: Kara Thrash Date: Tue, 8 Aug 2017 17:16:44 -0400 Subject: [PATCH 10/13] sub sub sub sub --- models/post.js | 1 - routers/posts.js | 1 + seeders/index.js | 3 ++- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/models/post.js b/models/post.js index f4a133d..3371aa1 100644 --- a/models/post.js +++ b/models/post.js @@ -9,7 +9,6 @@ var PostSchema = { topLevel: Boolean, subPosts: [{ type: Schema.Types.ObjectId, ref: "Post" }] }; - var Post = mongoose.model("Post", PostSchema); module.exports = Post; diff --git a/routers/posts.js b/routers/posts.js index 187a569..7ced904 100644 --- a/routers/posts.js +++ b/routers/posts.js @@ -38,6 +38,7 @@ router.get("/:id/edit", (req, res) => { // ---------------------------------------- router.get("/:id", (req, res) => { Post.findById(req.params.id) + .populate("subPosts") .populate("subPosts") .then(post => { console.log(post); diff --git a/seeders/index.js b/seeders/index.js index 32eb14e..c76cf72 100644 --- a/seeders/index.js +++ b/seeders/index.js @@ -71,10 +71,11 @@ const seeds = () => { subPosts: [] }); console.log(authorId); - subPost[authorId].subPosts.push(subSubPost); + subPost.subPosts.push(subSubPost); posts[authorId].subPosts.push(subPost); users[authorId].posts.push(post); posts.push(subPost); + posts.push(subSubPost); } /// From 99a1642f96d7a1dbc2ed61d3e4c3ca4b3b050501 Mon Sep 17 00:00:00 2001 From: Stephanie Barker Date: Tue, 8 Aug 2017 18:02:37 -0400 Subject: [PATCH 11/13] working on creating posts --- models/post.js | 13 +++++++------ routers/posts.js | 13 +++++++++++-- routers/users.js | 1 + seeders/index.js | 3 ++- views/partials/postPartial.handlebars | 2 ++ views/posts/index.handlebars | 18 ++++++++++-------- views/posts/show.handlebars | 2 ++ views/users/show.handlebars | 15 +++++++++++++++ 8 files changed, 50 insertions(+), 17 deletions(-) diff --git a/models/post.js b/models/post.js index 3371aa1..8deef47 100644 --- a/models/post.js +++ b/models/post.js @@ -2,13 +2,14 @@ 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" }] + 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; diff --git a/routers/posts.js b/routers/posts.js index 7ced904..8067d34 100644 --- a/routers/posts.js +++ b/routers/posts.js @@ -38,8 +38,17 @@ router.get("/:id/edit", (req, res) => { // ---------------------------------------- router.get("/:id", (req, res) => { Post.findById(req.params.id) - .populate("subPosts") - .populate("subPosts") + .populate({ + path: "subPosts", + populate: { + path: "subPosts", + model: "Post", + populate: { + path: "subPosts", + model: "Post" + } + } + }) .then(post => { console.log(post); res.render("posts/show", { post }); diff --git a/routers/users.js b/routers/users.js index fda1e4e..394e9f6 100644 --- a/routers/users.js +++ b/routers/users.js @@ -38,6 +38,7 @@ router.get("/:id/edit", (req, res) => { // ---------------------------------------- router.get("/:id", (req, res) => { User.findById(req.params.id) + .populate("posts") .then(user => { res.render("users/show", { user }); }) diff --git a/seeders/index.js b/seeders/index.js index c76cf72..b3bd87e 100644 --- a/seeders/index.js +++ b/seeders/index.js @@ -73,7 +73,8 @@ const seeds = () => { console.log(authorId); subPost.subPosts.push(subSubPost); posts[authorId].subPosts.push(subPost); - users[authorId].posts.push(post); + users[authorId].posts.push(subPost); + users[authorId].posts.push(subSubPost); posts.push(subPost); posts.push(subSubPost); } diff --git a/views/partials/postPartial.handlebars b/views/partials/postPartial.handlebars index b09e7f2..3b66772 100644 --- a/views/partials/postPartial.handlebars +++ b/views/partials/postPartial.handlebars @@ -3,6 +3,8 @@

{{ subPost.title }} {{ subPost.author }} {{ subPost.votes }}

{{ subPost.body }}

+ + {{#if subPost.subPosts.length}} {{>postPartial subPosts=subPost.subPosts}} {{/if}} diff --git a/views/posts/index.handlebars b/views/posts/index.handlebars index 9ea23fb..0fe9200 100644 --- a/views/posts/index.handlebars +++ b/views/posts/index.handlebars @@ -6,11 +6,13 @@ - {{#each posts as |post| }} - - {{/each }} +{{#each posts as |post| }} + +{{/each }} diff --git a/views/posts/show.handlebars b/views/posts/show.handlebars index 2e151c5..74634cd 100644 --- a/views/posts/show.handlebars +++ b/views/posts/show.handlebars @@ -4,6 +4,8 @@

{{ post.title }} {{ post.author }} {{ post.votes }}

{{ post.body }}

+ + {{#if post.subPosts.length}} {{>postPartial subPosts=post.subPosts}} {{/if}} diff --git a/views/users/show.handlebars b/views/users/show.handlebars index 10eb1cf..479b75d 100644 --- a/views/users/show.handlebars +++ b/views/users/show.handlebars @@ -29,3 +29,18 @@
First NameLast Name Username Email
- {{ user.fname }} - - {{ user.lname }} - {{ user.username }}
First NameUsername {{ user.username }}
Last NameEmail {{ user.email }}
UsernameNumber of Posts {{ user.posts.length }}
+ +{{#if user.posts.length}} + {{#each user.posts as |post|}} +
+ {{#if post.topLevel}} +
+ {{else}} +
+ {{/if}} +

{{ post.title }} {{ post.author }} {{ post.votes }}

+

{{ post.body }}

+
+
+ {{/each}} +{{/if}} From b41a3400704335d79f249efa8cbd6d0f6aee3e7c Mon Sep 17 00:00:00 2001 From: Kara Thrash Date: Tue, 8 Aug 2017 18:32:37 -0400 Subject: [PATCH 12/13] author or ID I need you to work --- routers/posts.js | 39 ++++++++++++++++++++++----- views/partials/postPartial.handlebars | 1 + views/posts/index.handlebars | 5 +++- views/posts/new.handlebars | 3 ++- views/posts/newSubPost.handlebars | 29 ++++++++++++++++++++ views/posts/show.handlebars | 3 ++- views/users/show.handlebars | 3 +++ 7 files changed, 74 insertions(+), 9 deletions(-) create mode 100644 views/posts/newSubPost.handlebars diff --git a/routers/posts.js b/routers/posts.js index 8067d34..163bad6 100644 --- a/routers/posts.js +++ b/routers/posts.js @@ -19,9 +19,15 @@ router.get("/", (req, res) => { // New // ---------------------------------------- router.get("/new", (req, res) => { - res.render("new"); + res.render("posts/new"); +}); +router.get("/:id/new", (req, res) => { + Post.findById(req.params.id) + .then(post => { + res.render("posts/newSubPost", { post }); + }) + .catch(e => res.status(500).send(e.stack)); }); - // ---------------------------------------- // Edit // ---------------------------------------- @@ -60,13 +66,14 @@ router.get("/:id", (req, res) => { // Create // ---------------------------------------- router.post("/", (req, res) => { + let rnd = Math.floor(Math.random() * 10 + 1); var post = new Post({ title: req.body.post.title, - author: req.body.post.author, + author: "foobar1", body: req.body.post.body, - votes: req.body.post.votes, - topLevel: req.body.post.topLevel, - subPosts: req.body.post.subPosts + votes: rnd, + topLevel: true, + subPosts: [] }); post @@ -76,7 +83,27 @@ router.post("/", (req, res) => { }) .catch(e => res.status(500).send(e.stack)); }); +// +router.post("/:id", (req, res) => { + let rnd = Math.floor(Math.random() * 10 + 1); + var post = new Post({ + title: req.body.post.title, + author: "foobar1", + body: req.body.post.body, + votes: rnd, + topLevel: false, + subPosts: [] + }); + post + .save() + .then(post => { + Post.findByIdAndUpdate(req.params.id, { $push: { subPosts: post } }); + + res.redirect(`/posts/${post.id}`); + }) + .catch(e => res.status(500).send(e.stack)); +}); // ---------------------------------------- // Update // ---------------------------------------- diff --git a/views/partials/postPartial.handlebars b/views/partials/postPartial.handlebars index 3b66772..995d5a0 100644 --- a/views/partials/postPartial.handlebars +++ b/views/partials/postPartial.handlebars @@ -5,6 +5,7 @@

{{ subPost.body }}

+ Comment {{#if subPost.subPosts.length}} {{>postPartial subPosts=subPost.subPosts}} {{/if}} diff --git a/views/posts/index.handlebars b/views/posts/index.handlebars index 0fe9200..4e24b75 100644 --- a/views/posts/index.handlebars +++ b/views/posts/index.handlebars @@ -2,7 +2,9 @@ @@ -14,5 +16,6 @@

{{ post.body }}

+ Comment
{{/each }} diff --git a/views/posts/new.handlebars b/views/posts/new.handlebars index 44894a3..ef0acf3 100644 --- a/views/posts/new.handlebars +++ b/views/posts/new.handlebars @@ -13,7 +13,8 @@
- +
diff --git a/views/posts/newSubPost.handlebars b/views/posts/newSubPost.handlebars new file mode 100644 index 0000000..26ba1c6 --- /dev/null +++ b/views/posts/newSubPost.handlebars @@ -0,0 +1,29 @@ + +
+
+

{{ post.title }} {{ post.author }} {{ post.votes }}

+

{{ post.body }}

+ + +
+
+

Post your comment: but be kind of nice

+ +
+
+ + +
+ +
+ + +
+ + + +
+ +
+
diff --git a/views/posts/show.handlebars b/views/posts/show.handlebars index 74634cd..b336924 100644 --- a/views/posts/show.handlebars +++ b/views/posts/show.handlebars @@ -6,8 +6,9 @@

{{ post.body }}

+ Comment {{#if post.subPosts.length}} {{>postPartial subPosts=post.subPosts}} {{/if}}
- \ No newline at end of file + diff --git a/views/users/show.handlebars b/views/users/show.handlebars index 479b75d..6eec1f7 100644 --- a/views/users/show.handlebars +++ b/views/users/show.handlebars @@ -41,6 +41,9 @@

{{ post.title }} {{ post.author }} {{ post.votes }}

{{ post.body }}

+ + + Comment {{/each}} {{/if}} From c85e77bd97da4ddc95e2a34392dfc30fa82dd758 Mon Sep 17 00:00:00 2001 From: Stephanie Barker Date: Tue, 8 Aug 2017 18:59:17 -0400 Subject: [PATCH 13/13] populate nested authors --- routers/posts.js | 162 +++++++++++++++----------- seeders/index.js | 3 +- views/partials/postPartial.handlebars | 2 +- views/posts/index.handlebars | 2 +- views/posts/show.handlebars | 2 +- 5 files changed, 99 insertions(+), 72 deletions(-) diff --git a/routers/posts.js b/routers/posts.js index 163bad6..a80a472 100644 --- a/routers/posts.js +++ b/routers/posts.js @@ -3,12 +3,14 @@ var router = express.Router(); const mongoose = require("mongoose"); var models = require("./../models"); var Post = mongoose.model("Post"); +var User = mongoose.model("User"); // ---------------------------------------- // Index // ---------------------------------------- router.get("/", (req, res) => { Post.find({ topLevel: true }) + .populate("author") .then(posts => { res.render("posts/index", { posts }); }) @@ -44,17 +46,35 @@ router.get("/:id/edit", (req, res) => { // ---------------------------------------- router.get("/:id", (req, res) => { Post.findById(req.params.id) - .populate({ - path: "subPosts", - populate: { + .populate([ + { path: "subPosts", - model: "Post", - populate: { - path: "subPosts", - model: "Post" - } + populate: [ + { + path: "subPosts", + model: "Post", + populate: [ + { + path: "subPosts", + model: "Post" + }, + { + path: "author", + model: "User" + } + ] + }, + { + path: "author", + model: "User" + } + ] + }, + { + path: "author", + model: "User" } - }) + ]) .then(post => { console.log(post); res.render("posts/show", { post }); @@ -67,79 +87,85 @@ router.get("/:id", (req, res) => { // ---------------------------------------- router.post("/", (req, res) => { let rnd = Math.floor(Math.random() * 10 + 1); - var post = new Post({ - title: req.body.post.title, - author: "foobar1", - body: req.body.post.body, - votes: rnd, - topLevel: true, - subPosts: [] - }); + User.findOne().then(user => { + var post = new Post({ + title: req.body.post.title, + author: user, + body: req.body.post.body, + votes: rnd, + topLevel: true, + subPosts: [] + }); - post - .save() - .then(post => { - res.redirect(`/posts/${post.id}`); - }) - .catch(e => res.status(500).send(e.stack)); + post + .save() + .then(post => { + res.redirect(`/posts/${post.id}`); + }) + .catch(e => res.status(500).send(e.stack)); + }); }); // router.post("/:id", (req, res) => { let rnd = Math.floor(Math.random() * 10 + 1); - var post = new Post({ - title: req.body.post.title, - author: "foobar1", - body: req.body.post.body, - votes: rnd, - topLevel: false, - subPosts: [] - }); + User.findOne().then(user => { + var post = new Post({ + title: req.body.post.title, + author: user, + body: req.body.post.body, + votes: rnd, + topLevel: false, + subPosts: [] + }); - post - .save() - .then(post => { - Post.findByIdAndUpdate(req.params.id, { $push: { subPosts: post } }); - - res.redirect(`/posts/${post.id}`); - }) - .catch(e => res.status(500).send(e.stack)); + post + .save() + .then(post => { + Post.findByIdAndUpdate(req.params.id, { + $push: { subPosts: post } + }).then(() => { + res.redirect(`/posts/${req.params.id}`); + }); + }) + .catch(e => res.status(500).send(e.stack)); + }); }); // ---------------------------------------- // Update // ---------------------------------------- -router.put("/:id", (req, res) => { - var postParams = { - title: req.body.post.title, - author: req.body.post.author, - body: req.body.post.body, - votes: req.body.post.votes, - topLevel: req.body.post.topLevel, - subPosts: req.body.post.subPosts - }; +// router.put("/:id", (req, res) => { +// var postParams = { +// title: req.body.post.title, +// author: req.body.post.author, +// body: req.body.post.body, +// votes: req.body.post.votes, +// topLevel: req.body.post.topLevel, +// subPosts: req.body.post.subPosts +// }; - User.findByIdAndUpdate(req.params.id, postParams) - .then(post => { - req.method = "GET"; - res.redirect(`/posts/${post.id}`); - }) - .catch(e => res.status(500).send(e.stack)); -}); +// User.findByIdAndUpdate(req.params.id, postParams) +// .then(post => { +// req.method = "GET"; +// res.redirect(`/posts/${post.id}`); +// }) +// .catch(e => res.status(500).send(e.stack)); +// }); // ---------------------------------------- // Destroy // ---------------------------------------- -router.delete("/:id", (req, res) => { - var currentPost = req.session.currentPost; - Post.findByIdAndRemove(req.params.id) - .then(() => { - req.method = "GET"; - if (currentPost.id === req.params.id) { - res.redirect("/logout"); - } else { - res.redirect("/posts"); - } - }) - .catch(e => res.status(500).send(e.stack)); -}); +// router.delete("/:id", (req, res) => { +// var currentPost = req.session.currentPost; +// Post.findByIdAndRemove(req.params.id) +// .then(() => { +// req.method = "GET"; +// if (currentPost.id === req.params.id) { +// res.redirect("/logout"); +// } else { +// res.redirect("/posts"); +// } +// }) +// .catch(e => res.status(500).send(e.stack)); +// }); module.exports = router; diff --git a/seeders/index.js b/seeders/index.js index b3bd87e..ba71214 100644 --- a/seeders/index.js +++ b/seeders/index.js @@ -59,6 +59,7 @@ const seeds = () => { title: `Title of ${i}`, author: users[authorId], body: `This is a subpost! ${i}`, + votes: 0, topLevel: false, subPosts: [] }); @@ -67,10 +68,10 @@ const seeds = () => { title: `Title of ${i}`, author: users[authorId], body: `This is a sub-subpost! ${i}`, + votes: 0, topLevel: false, subPosts: [] }); - console.log(authorId); subPost.subPosts.push(subSubPost); posts[authorId].subPosts.push(subPost); users[authorId].posts.push(subPost); diff --git a/views/partials/postPartial.handlebars b/views/partials/postPartial.handlebars index 995d5a0..9bc1fc3 100644 --- a/views/partials/postPartial.handlebars +++ b/views/partials/postPartial.handlebars @@ -1,7 +1,7 @@ {{#each subPosts as |subPost|}}
-

{{ subPost.title }} {{ subPost.author }} {{ subPost.votes }}

+

{{ subPost.title }} {{ subPost.author.username }} {{ subPost.votes }}

{{ subPost.body }}

diff --git a/views/posts/index.handlebars b/views/posts/index.handlebars index 4e24b75..0f5e24b 100644 --- a/views/posts/index.handlebars +++ b/views/posts/index.handlebars @@ -11,7 +11,7 @@ {{#each posts as |post| }}
-

{{ post.title }} :By: {{ post.author }} :Votes: {{ post.votes }}

+

{{ post.title }} :By: {{ post.author.username }} :Votes: {{ post.votes }}

{{ post.body }}

diff --git a/views/posts/show.handlebars b/views/posts/show.handlebars index b336924..683c8d6 100644 --- a/views/posts/show.handlebars +++ b/views/posts/show.handlebars @@ -2,7 +2,7 @@