mirror of
https://github.com/sstent/expressmongotest.git
synced 2026-04-05 12:33:32 +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/index')(app);
|
||||||
require('./routes/users')(app);
|
require('./routes/users')(app);
|
||||||
require('./routes/session')(app);
|
require('./routes/session')(app);
|
||||||
require('./routes/articles')(app);
|
require('./routes/workouts')(app);
|
||||||
|
|
||||||
http.createServer(app).listen(app.get("port"), function(){
|
http.createServer(app).listen(app.get("port"), function(){
|
||||||
console.log ("Server listening on port " + app.get("port"));
|
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 }
|
is_admin: {type: Boolean, 'default': false }
|
||||||
});
|
});
|
||||||
|
|
||||||
UserSchema.methods.recentArticles = function(callback) {
|
UserSchema.methods.recentworkouts = function(callback) {
|
||||||
return this.model('Article')
|
return this.model('workout')
|
||||||
.find({author: this._id})
|
.find({author: this._id})
|
||||||
//.sort('created_at', 1)
|
//.sort('created_at', 1)
|
||||||
.limit(5)
|
.limit(5)
|
||||||
|
|||||||
@@ -1,30 +1,19 @@
|
|||||||
var Schema = require('mongoose').Schema;
|
var Schema = require('mongoose').Schema;
|
||||||
|
|
||||||
|
var workoutSchema = new Schema({
|
||||||
var Split = new Schema({
|
title: {
|
||||||
reps: { type: String},
|
type: String
|
||||||
weight: { type: String},
|
},
|
||||||
dropset: { type: Boolean }
|
body: String,
|
||||||
|
author: {
|
||||||
|
type: Schema.ObjectId,
|
||||||
|
ref: 'User',
|
||||||
|
required: true
|
||||||
|
},
|
||||||
|
created_at: {
|
||||||
|
type: Date,
|
||||||
|
'default': Date.now
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
module.exports = workoutSchema;
|
||||||
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;
|
|
||||||
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) {
|
function isAdmin(req, res, next) {
|
||||||
if (req.session.user.is_admin === false) {
|
if (req.session.user.is_admin === false) {
|
||||||
|
console.log("not an admin - sending to profile");
|
||||||
res.redirect('/users/' + req.session.user.username);
|
res.redirect('/users/' + req.session.user.username);
|
||||||
} else {
|
} else {
|
||||||
|
console.log("Admin detected");
|
||||||
next();
|
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) {
|
function loggedIn(req, res, next) {
|
||||||
if (! req.session.user) {
|
if (! req.session.user) {
|
||||||
//res.send('Forbidden. Please log in first.', 403);
|
console.log("not logged in - redirecting to login");
|
||||||
res.redirect('/session/new');
|
res.redirect('/session/new');
|
||||||
} else {
|
} else {
|
||||||
|
console.log("user logged in");
|
||||||
next();
|
next();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,7 +17,6 @@ module.exports = function(app) {
|
|||||||
}
|
}
|
||||||
if (user) {
|
if (user) {
|
||||||
req.session.user = user;
|
req.session.user = user;
|
||||||
console.log("req.session.user= " + JSON.stringify(req.session.user));
|
|
||||||
res.redirect('/users/' + req.session.user.username);
|
res.redirect('/users/' + req.session.user.username);
|
||||||
} else {
|
} else {
|
||||||
res.redirect('/session/new');
|
res.redirect('/session/new');
|
||||||
|
|||||||
@@ -44,7 +44,7 @@ module.exports = function(app) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
app.get('/users/:name', loadUser, function(req, res, next){
|
app.get('/users/:name', loadUser, function(req, res, next){
|
||||||
req.user.recentArticles(function(err, articles) {
|
req.user.recentworkouts(function(err, workouts) {
|
||||||
if (err) {
|
if (err) {
|
||||||
return next(err);
|
return next(err);
|
||||||
}
|
}
|
||||||
@@ -52,7 +52,7 @@ module.exports = function(app) {
|
|||||||
title: 'User profile',
|
title: 'User profile',
|
||||||
user: req.user,
|
user: req.user,
|
||||||
requested: req.params.name,
|
requested: req.params.name,
|
||||||
recentArticles: articles
|
recentworkouts: workouts
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,31 +1,31 @@
|
|||||||
/*
|
/*
|
||||||
* Article Routes
|
* workout Routes
|
||||||
*/
|
*/
|
||||||
|
|
||||||
var async = require('async');
|
var async = require('async');
|
||||||
|
|
||||||
var Article = require('../data/models/article');
|
var workout = require('../data/models/workout');
|
||||||
var notLoggedIn = require('./middleware/not_logged_in');
|
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 loggedIn = require('./middleware/logged_in');
|
||||||
var qs = require('querystring');
|
var qs = require('querystring');
|
||||||
var maxArticlesPerPage = 5;
|
var maxworkoutsPerPage = 5;
|
||||||
|
|
||||||
module.exports = function(app) {
|
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;
|
var page = req.query.page && parseInt(req.query.page, 10) || 0;
|
||||||
async.parallel([
|
async.parallel([
|
||||||
|
|
||||||
function(next) {
|
function(next) {
|
||||||
Article.count(next);
|
workout.count(next);
|
||||||
},
|
},
|
||||||
|
|
||||||
function(next) {
|
function(next) {
|
||||||
Article.find({})
|
workout.find({})
|
||||||
//.sort('title', 1)
|
//.sort('title', 1)
|
||||||
.skip(page * maxArticlesPerPage)
|
.skip(page * maxworkoutsPerPage)
|
||||||
.limit(maxArticlesPerPage)
|
.limit(maxworkoutsPerPage)
|
||||||
.exec(next);
|
.exec(next);
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
@@ -38,13 +38,13 @@ module.exports = function(app) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var count = results[0];
|
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', {
|
res.render('workouts/index', {
|
||||||
title: 'Articles',
|
title: 'workouts',
|
||||||
articles: articles,
|
workouts: workouts,
|
||||||
page: page,
|
page: page,
|
||||||
lastPage: lastPage
|
lastPage: lastPage
|
||||||
});
|
});
|
||||||
@@ -53,20 +53,20 @@ module.exports = function(app) {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get('/articles/new', loggedIn, function(req, res) {
|
app.get('/workouts/new', loggedIn, function(req, res) {
|
||||||
res.render('articles/new', {title: "New Article"});
|
res.render('workouts/new', {title: "New workout"});
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get('/articles/:_id', loadArticle, function(req, res, next){
|
app.get('/workouts/:_id', loadworkout, function(req, res, next){
|
||||||
res.render('articles/article', {title: req.article.title,
|
res.render('workouts/workout', {title: req.workout.title,
|
||||||
article: req.article});
|
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));
|
console.log("/nreq.body" + JSON.stringify(req.body));
|
||||||
var article = req.body;
|
var workout = req.body;
|
||||||
article.author = req.session.user._id;
|
workout.author = req.session.user._id;
|
||||||
Article.create(article, function(err) {
|
workout.create(workout, function(err) {
|
||||||
if (err) {
|
if (err) {
|
||||||
if (err.code === 11000) {
|
if (err.code === 11000) {
|
||||||
res.send('Conflict', 409);
|
res.send('Conflict', 409);
|
||||||
@@ -81,14 +81,14 @@ module.exports = function(app) {
|
|||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
res.redirect('/articles');
|
res.redirect('/workouts');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
app.del('/articles/:title', loggedIn, loadArticle, function(req, res, next) {
|
app.del('/workouts/:title', loggedIn, loadworkout, function(req, res, next) {
|
||||||
req.article.remove(function(err) {
|
req.workout.remove(function(err) {
|
||||||
if (err) { return next(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
|
extends ./layout
|
||||||
|
|
||||||
block content
|
block content
|
||||||
h1= title
|
h1= #{title}
|
||||||
p Welcome to #{title}
|
p Welcome to #{title}
|
||||||
|
|
||||||
p
|
p
|
||||||
a(href="/users") List Users
|
a(href="/users") List Users
|
||||||
p
|
p
|
||||||
a(href="/articles") List Articles
|
a(href="/workouts") List workouts
|
||||||
|
|||||||
@@ -6,12 +6,12 @@ block content
|
|||||||
p
|
p
|
||||||
a(href="/users/new") Create new profile
|
a(href="/users/new") Create new profile
|
||||||
p
|
p
|
||||||
a(href="/articles/new") Create new article
|
a(href="/workouts/new") Create new workout
|
||||||
|
|
||||||
p
|
p
|
||||||
a(href="/users") List Users
|
a(href="/users") List Users
|
||||||
p
|
p
|
||||||
a(href="/articles") List Articles
|
a(href="/workouts") List workouts
|
||||||
|
|
||||||
|
|
||||||
ul
|
ul
|
||||||
|
|||||||
@@ -10,9 +10,12 @@ block content
|
|||||||
- else
|
- else
|
||||||
h1 Public View
|
h1 Public View
|
||||||
|
|
||||||
h2 Recent Articles:
|
h2 Recent workouts:
|
||||||
ul
|
ul
|
||||||
- recentArticles.forEach(function(article) {
|
- recentworkouts.forEach(function(workout) {
|
||||||
li
|
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
|
extends ../layout
|
||||||
|
|
||||||
block content
|
block content
|
||||||
h1 Articles
|
h1 workouts
|
||||||
|
|
||||||
p
|
p
|
||||||
a(href="/articles/new") Create new article
|
a(href="/workouts/new") Create new workout
|
||||||
p
|
p
|
||||||
a(href="/users") List Users
|
a(href="/users") List Users
|
||||||
p
|
p
|
||||||
a(href="/articles") List Articles
|
a(href="/workouts") List workouts
|
||||||
|
|
||||||
|
|
||||||
include list
|
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
|
extends ../layout
|
||||||
|
|
||||||
block content
|
block content
|
||||||
h1 New Article
|
h1 New workout
|
||||||
|
|
||||||
form(method="POST", action="/articles")
|
form(method="POST", action="/workouts")
|
||||||
p
|
p
|
||||||
label(for="title") UserID<br />
|
label(for="title") UserID<br />
|
||||||
input#title(name="User_ID")
|
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