mirror of
https://github.com/sstent/expressmongotest.git
synced 2026-01-26 00:52:11 +00:00
refactored article to workout
This commit is contained in:
@@ -50,7 +50,7 @@ app.configure('production', function(){
|
||||
require('./routes/index')(app);
|
||||
require('./routes/users')(app);
|
||||
require('./routes/session')(app);
|
||||
require('./routes/articles')(app);
|
||||
require('./routes/workouts')(app);
|
||||
|
||||
http.createServer(app).listen(app.get("port"), function(){
|
||||
console.log ("Server listening on port " + app.get("port"));
|
||||
|
||||
@@ -1,6 +0,0 @@
|
||||
var mongoose = require('mongoose');
|
||||
var ArticleSchema = require('../schemas/article');
|
||||
|
||||
var Article = mongoose.model('Article', ArticleSchema);
|
||||
|
||||
module.exports = Article;
|
||||
6
test/data/models/workout.js
Normal file
6
test/data/models/workout.js
Normal file
@@ -0,0 +1,6 @@
|
||||
var mongoose = require('mongoose');
|
||||
var workoutSchema = require('../schemas/workout');
|
||||
|
||||
var workout = mongoose.model('workout', workoutSchema);
|
||||
|
||||
module.exports = workout;
|
||||
@@ -1,19 +0,0 @@
|
||||
var Schema = require('mongoose').Schema;
|
||||
|
||||
var ArticleSchema = new Schema({
|
||||
title: {
|
||||
type: String
|
||||
},
|
||||
body: String,
|
||||
author: {
|
||||
type: Schema.ObjectId,
|
||||
ref: 'User',
|
||||
required: true
|
||||
},
|
||||
created_at: {
|
||||
type: Date,
|
||||
'default': Date.now
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = ArticleSchema;
|
||||
@@ -7,8 +7,8 @@ var UserSchema = new mongoose.Schema({
|
||||
is_admin: {type: Boolean, 'default': false }
|
||||
});
|
||||
|
||||
UserSchema.methods.recentArticles = function(callback) {
|
||||
return this.model('Article')
|
||||
UserSchema.methods.recentworkouts = function(callback) {
|
||||
return this.model('workout')
|
||||
.find({author: this._id})
|
||||
//.sort('created_at', 1)
|
||||
.limit(5)
|
||||
|
||||
@@ -1,30 +1,19 @@
|
||||
var Schema = require('mongoose').Schema;
|
||||
|
||||
|
||||
var Split = new Schema({
|
||||
reps: { type: String},
|
||||
weight: { type: String},
|
||||
dropset: { type: Boolean }
|
||||
var workoutSchema = new Schema({
|
||||
title: {
|
||||
type: String
|
||||
},
|
||||
body: String,
|
||||
author: {
|
||||
type: Schema.ObjectId,
|
||||
ref: 'User',
|
||||
required: true
|
||||
},
|
||||
created_at: {
|
||||
type: Date,
|
||||
'default': Date.now
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
var Element = new Schema({
|
||||
ExerciseID: { type: Schema.ObjectId, ref: 'exercise'},
|
||||
splits: [Split]
|
||||
});
|
||||
|
||||
|
||||
var ArticleSchema = new Schema({
|
||||
|
||||
userID: { type: Schema.ObjectId, ref: 'User', required: true },
|
||||
workoutDate: { type: Date, 'default': Date.now },
|
||||
workoutTime: { type: Date, 'default': Date.now },
|
||||
privacySetting: { type: Number},
|
||||
Notes: { type: String},
|
||||
templateID: { type: Schema.ObjectId, ref: 'Template'},
|
||||
circuits: [Number],
|
||||
elements: [Element]
|
||||
|
||||
});
|
||||
|
||||
module.exports = ArticleSchema;
|
||||
module.exports = workoutSchema;
|
||||
30
test/data/schemas/workout_new.js
Normal file
30
test/data/schemas/workout_new.js
Normal file
@@ -0,0 +1,30 @@
|
||||
var Schema = require('mongoose').Schema;
|
||||
|
||||
|
||||
var Split = new Schema({
|
||||
reps: { type: String},
|
||||
weight: { type: String},
|
||||
dropset: { type: Boolean }
|
||||
});
|
||||
|
||||
|
||||
var Element = new Schema({
|
||||
ExerciseID: { type: Schema.ObjectId, ref: 'exercise'},
|
||||
splits: [Split]
|
||||
});
|
||||
|
||||
|
||||
var workoutSchema = new Schema({
|
||||
|
||||
userID: { type: Schema.ObjectId, ref: 'User', required: true },
|
||||
workoutDate: { type: Date, 'default': Date.now },
|
||||
workoutTime: { type: Date, 'default': Date.now },
|
||||
privacySetting: { type: Number},
|
||||
Notes: { type: String},
|
||||
templateID: { type: Schema.ObjectId, ref: 'Template'},
|
||||
circuits: [Number],
|
||||
elements: [Element]
|
||||
|
||||
});
|
||||
|
||||
module.exports = workoutSchema;
|
||||
@@ -1,7 +1,9 @@
|
||||
function isAdmin(req, res, next) {
|
||||
if (req.session.user.is_admin === false) {
|
||||
console.log("not an admin - sending to profile");
|
||||
res.redirect('/users/' + req.session.user.username);
|
||||
} else {
|
||||
console.log("Admin detected");
|
||||
next();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,18 +0,0 @@
|
||||
var Article = require('../../data/models/article');
|
||||
|
||||
function loadArticle(req, res, next) {
|
||||
Article.findOne({title: req.params.title})
|
||||
.populate('author')
|
||||
.exec(function(err, article) {
|
||||
if (err) {
|
||||
return next(err);
|
||||
}
|
||||
if (! article) {
|
||||
return res.send('Not found', 404);
|
||||
}
|
||||
req.article = article;
|
||||
next();
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = loadArticle;
|
||||
18
test/routes/middleware/load_workout.js
Normal file
18
test/routes/middleware/load_workout.js
Normal file
@@ -0,0 +1,18 @@
|
||||
var workout = require('../../data/models/workout');
|
||||
|
||||
function loadworkout(req, res, next) {
|
||||
workout.findOne({title: req.params.title})
|
||||
.populate('author')
|
||||
.exec(function(err, workout) {
|
||||
if (err) {
|
||||
return next(err);
|
||||
}
|
||||
if (! workout) {
|
||||
return res.send('Not found', 404);
|
||||
}
|
||||
req.workout = workout;
|
||||
next();
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = loadworkout;
|
||||
@@ -1,8 +1,9 @@
|
||||
function loggedIn(req, res, next) {
|
||||
if (! req.session.user) {
|
||||
//res.send('Forbidden. Please log in first.', 403);
|
||||
console.log("not logged in - redirecting to login");
|
||||
res.redirect('/session/new');
|
||||
} else {
|
||||
console.log("user logged in");
|
||||
next();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,7 +17,6 @@ module.exports = function(app) {
|
||||
}
|
||||
if (user) {
|
||||
req.session.user = user;
|
||||
console.log("req.session.user= " + JSON.stringify(req.session.user));
|
||||
res.redirect('/users/' + req.session.user.username);
|
||||
} else {
|
||||
res.redirect('/session/new');
|
||||
|
||||
@@ -44,7 +44,7 @@ module.exports = function(app) {
|
||||
});
|
||||
|
||||
app.get('/users/:name', loadUser, function(req, res, next){
|
||||
req.user.recentArticles(function(err, articles) {
|
||||
req.user.recentworkouts(function(err, workouts) {
|
||||
if (err) {
|
||||
return next(err);
|
||||
}
|
||||
@@ -52,7 +52,7 @@ module.exports = function(app) {
|
||||
title: 'User profile',
|
||||
user: req.user,
|
||||
requested: req.params.name,
|
||||
recentArticles: articles
|
||||
recentworkouts: workouts
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,31 +1,31 @@
|
||||
/*
|
||||
* Article Routes
|
||||
* workout Routes
|
||||
*/
|
||||
|
||||
var async = require('async');
|
||||
|
||||
var Article = require('../data/models/article');
|
||||
var workout = require('../data/models/workout');
|
||||
var notLoggedIn = require('./middleware/not_logged_in');
|
||||
var loadArticle = require('./middleware/load_article');
|
||||
var loadworkout = require('./middleware/load_workout');
|
||||
var loggedIn = require('./middleware/logged_in');
|
||||
var qs = require('querystring');
|
||||
var maxArticlesPerPage = 5;
|
||||
var maxworkoutsPerPage = 5;
|
||||
|
||||
module.exports = function(app) {
|
||||
|
||||
app.get('/articles', loggedIn, function(req, res, next){
|
||||
app.get('/workouts', loggedIn, function(req, res, next){
|
||||
var page = req.query.page && parseInt(req.query.page, 10) || 0;
|
||||
async.parallel([
|
||||
|
||||
function(next) {
|
||||
Article.count(next);
|
||||
workout.count(next);
|
||||
},
|
||||
|
||||
function(next) {
|
||||
Article.find({})
|
||||
workout.find({})
|
||||
//.sort('title', 1)
|
||||
.skip(page * maxArticlesPerPage)
|
||||
.limit(maxArticlesPerPage)
|
||||
.skip(page * maxworkoutsPerPage)
|
||||
.limit(maxworkoutsPerPage)
|
||||
.exec(next);
|
||||
}
|
||||
],
|
||||
@@ -38,13 +38,13 @@ module.exports = function(app) {
|
||||
}
|
||||
|
||||
var count = results[0];
|
||||
var articles = results[1];
|
||||
var workouts = results[1];
|
||||
|
||||
var lastPage = (page + 1) * maxArticlesPerPage >= count;
|
||||
var lastPage = (page + 1) * maxworkoutsPerPage >= count;
|
||||
|
||||
res.render('articles/index', {
|
||||
title: 'Articles',
|
||||
articles: articles,
|
||||
res.render('workouts/index', {
|
||||
title: 'workouts',
|
||||
workouts: workouts,
|
||||
page: page,
|
||||
lastPage: lastPage
|
||||
});
|
||||
@@ -53,20 +53,20 @@ module.exports = function(app) {
|
||||
);
|
||||
});
|
||||
|
||||
app.get('/articles/new', loggedIn, function(req, res) {
|
||||
res.render('articles/new', {title: "New Article"});
|
||||
app.get('/workouts/new', loggedIn, function(req, res) {
|
||||
res.render('workouts/new', {title: "New workout"});
|
||||
});
|
||||
|
||||
app.get('/articles/:_id', loadArticle, function(req, res, next){
|
||||
res.render('articles/article', {title: req.article.title,
|
||||
article: req.article});
|
||||
app.get('/workouts/:_id', loadworkout, function(req, res, next){
|
||||
res.render('workouts/workout', {title: req.workout.title,
|
||||
workout: req.workout});
|
||||
});
|
||||
|
||||
app.post('/articles', loggedIn, function(req, res, next) {
|
||||
app.post('/workouts', loggedIn, function(req, res, next) {
|
||||
console.log("/nreq.body" + JSON.stringify(req.body));
|
||||
var article = req.body;
|
||||
article.author = req.session.user._id;
|
||||
Article.create(article, function(err) {
|
||||
var workout = req.body;
|
||||
workout.author = req.session.user._id;
|
||||
workout.create(workout, function(err) {
|
||||
if (err) {
|
||||
if (err.code === 11000) {
|
||||
res.send('Conflict', 409);
|
||||
@@ -81,14 +81,14 @@ module.exports = function(app) {
|
||||
}
|
||||
return;
|
||||
}
|
||||
res.redirect('/articles');
|
||||
res.redirect('/workouts');
|
||||
});
|
||||
});
|
||||
|
||||
app.del('/articles/:title', loggedIn, loadArticle, function(req, res, next) {
|
||||
req.article.remove(function(err) {
|
||||
app.del('/workouts/:title', loggedIn, loadworkout, function(req, res, next) {
|
||||
req.workout.remove(function(err) {
|
||||
if (err) { return next(err); }
|
||||
res.redirect('/articles');
|
||||
res.redirect('/workouts');
|
||||
});
|
||||
|
||||
});
|
||||
@@ -1,12 +0,0 @@
|
||||
extends ../layout
|
||||
block content
|
||||
h1= article.title
|
||||
|
||||
div!= article.body
|
||||
|
||||
hr
|
||||
|
||||
p
|
||||
span Author:
|
||||
|
||||
a(href="/users/" + encodeURIComponent(article.author.name))= article.author.name
|
||||
@@ -1,5 +0,0 @@
|
||||
ul
|
||||
- articles.forEach(function(article) {
|
||||
li
|
||||
a(href="/articles/" + encodeURIComponent(article._id))= article.title
|
||||
- });
|
||||
@@ -1,10 +1,10 @@
|
||||
extends ./layout
|
||||
|
||||
block content
|
||||
h1= title
|
||||
h1= #{title}
|
||||
p Welcome to #{title}
|
||||
|
||||
p
|
||||
a(href="/users") List Users
|
||||
p
|
||||
a(href="/articles") List Articles
|
||||
a(href="/workouts") List workouts
|
||||
|
||||
@@ -6,12 +6,12 @@ block content
|
||||
p
|
||||
a(href="/users/new") Create new profile
|
||||
p
|
||||
a(href="/articles/new") Create new article
|
||||
a(href="/workouts/new") Create new workout
|
||||
|
||||
p
|
||||
a(href="/users") List Users
|
||||
p
|
||||
a(href="/articles") List Articles
|
||||
a(href="/workouts") List workouts
|
||||
|
||||
|
||||
ul
|
||||
|
||||
@@ -10,9 +10,12 @@ block content
|
||||
- else
|
||||
h1 Public View
|
||||
|
||||
h2 Recent Articles:
|
||||
h2 Recent workouts:
|
||||
ul
|
||||
- recentArticles.forEach(function(article) {
|
||||
- recentworkouts.forEach(function(workout) {
|
||||
li
|
||||
a(href="/articles/" + encodeURIComponent(article._id))= article.title
|
||||
a(href="/workouts/" + encodeURIComponent(workout._id))= workout.title
|
||||
- });
|
||||
|
||||
- if (session.user.is_admin === true)
|
||||
a(href="/users/") Goto Admin Page
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
extends ../layout
|
||||
|
||||
block content
|
||||
h1 Articles
|
||||
h1 workouts
|
||||
|
||||
p
|
||||
a(href="/articles/new") Create new article
|
||||
a(href="/workouts/new") Create new workout
|
||||
p
|
||||
a(href="/users") List Users
|
||||
p
|
||||
a(href="/articles") List Articles
|
||||
a(href="/workouts") List workouts
|
||||
|
||||
|
||||
include list
|
||||
5
test/views/workouts/list.jade
Normal file
5
test/views/workouts/list.jade
Normal file
@@ -0,0 +1,5 @@
|
||||
ul
|
||||
- workouts.forEach(function(workout) {
|
||||
li
|
||||
a(href="/workouts/" + encodeURIComponent(workout._id))= workout.title
|
||||
- });
|
||||
@@ -1,9 +1,9 @@
|
||||
extends ../layout
|
||||
|
||||
block content
|
||||
h1 New Article
|
||||
h1 New workout
|
||||
|
||||
form(method="POST", action="/articles")
|
||||
form(method="POST", action="/workouts")
|
||||
p
|
||||
label(for="title") UserID<br />
|
||||
input#title(name="User_ID")
|
||||
12
test/views/workouts/workout.jade
Normal file
12
test/views/workouts/workout.jade
Normal file
@@ -0,0 +1,12 @@
|
||||
extends ../layout
|
||||
block content
|
||||
h1= workout.title
|
||||
|
||||
div!= workout.body
|
||||
|
||||
hr
|
||||
|
||||
p
|
||||
span Author:
|
||||
|
||||
a(href="/users/" + encodeURIComponent(workout.author.name))= workout.author.name
|
||||
Reference in New Issue
Block a user