mirror of
https://github.com/bodyrep/DemoApp.git
synced 2025-12-06 06:01:48 +00:00
added rudimentary admin pages, started testing validation code
This commit is contained in:
7
app.js
7
app.js
@@ -7,16 +7,14 @@ var express = require('express');
|
||||
http = require('http');
|
||||
routes = require('./routes');
|
||||
var async = require('async');
|
||||
//var app = module.exports = express.createServer();
|
||||
var app = express();
|
||||
|
||||
var dbURL = 'mongodb://localhost/database';
|
||||
var db = require('mongoose').connect(dbURL);
|
||||
var qs = require('qs');
|
||||
|
||||
|
||||
// Configuration
|
||||
|
||||
|
||||
|
||||
app.configure(function(){
|
||||
app.set('views', __dirname + '/views');
|
||||
app.set('view engine', 'jade');
|
||||
@@ -50,6 +48,7 @@ require('./routes/index')(app);
|
||||
require('./routes/users')(app);
|
||||
require('./routes/session')(app);
|
||||
require('./routes/workouts')(app);
|
||||
require('./routes/admin')(app);
|
||||
|
||||
http.createServer(app).listen(app.get("port"), function(){
|
||||
console.log ("Server listening on port " + app.get("port"));
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
var mongoose = require('mongoose');
|
||||
var ExerciseSchema = require('../schemas/exerise');
|
||||
var ExerciseSchema = require('../schemas/exercise');
|
||||
|
||||
var Exercise = mongoose.model('Exercise', ExerciseSchema);
|
||||
|
||||
|
||||
@@ -13,8 +13,8 @@ var exerciseSchema = new Schema({
|
||||
difficulty: Number,
|
||||
musclearray: [Number],
|
||||
keywords: [String],
|
||||
alternatives: [ids],
|
||||
alternatives: [String],
|
||||
datafields: [Datafield]
|
||||
});
|
||||
|
||||
module.exports = workoutSchema;
|
||||
module.exports = exerciseSchema;
|
||||
File diff suppressed because one or more lines are too long
74
public/javascripts/newexercisevalidation.js
Normal file
74
public/javascripts/newexercisevalidation.js
Normal file
@@ -0,0 +1,74 @@
|
||||
$(document).ready(function() {
|
||||
////Validation
|
||||
var name = new LiveValidation('name');
|
||||
name.add( Validate.Format, { pattern: /^\s*[a-zA-Z0-9,\s]+\s*$/ } );
|
||||
name.add( Validate.Length, { minimum: 3, maximum: 12 , failureMessage: "Please enter a valid name" } );
|
||||
name.add( Validate.Presence );
|
||||
|
||||
var type = new LiveValidation('type');
|
||||
type.add( Validate.Exclusion, { within: ['notselected', 'Select One'], failureMessage: "Please select an entry from the list" } );
|
||||
type.add( Validate.Presence );
|
||||
var difficulty = new LiveValidation('difficulty');
|
||||
difficulty.add( Validate.Numericality, { minimum: 1, maximum: 10, onlyInteger: true, failureMessage: "Must be a number between 1 and 10" });
|
||||
difficulty.add( Validate.Presence );
|
||||
var description = new LiveValidation('description');
|
||||
description.add( Validate.Format, { pattern: /^\s*[a-zA-Z0-9,\s]+\s*$/ } );
|
||||
description.add( Validate.Presence );
|
||||
var musclearray0 = new LiveValidation('musclearray[0]');
|
||||
musclearray0.add( Validate.Numericality, { minimum: 1, maximum: 10, onlyInteger: true, failureMessage: "Must be a number between 1 and 10" });
|
||||
musclearray0.add( Validate.Presence );
|
||||
var musclearray1 = new LiveValidation('musclearray[1]');
|
||||
musclearray1.add( Validate.Numericality, { minimum: 1, maximum: 10, onlyInteger: true, failureMessage: "Must be a number between 1 and 10" });
|
||||
musclearray1.add( Validate.Presence );
|
||||
var musclearray2 = new LiveValidation('musclearray[2]');
|
||||
musclearray2.add( Validate.Numericality, { minimum: 1, maximum: 10, onlyInteger: true, failureMessage: "Must be a number between 1 and 10" });
|
||||
musclearray2.add( Validate.Presence );
|
||||
var musclearray3 = new LiveValidation('musclearray[3]');
|
||||
musclearray3.add( Validate.Numericality, { minimum: 1, maximum: 10, onlyInteger: true, failureMessage: "Must be a number between 1 and 10" });
|
||||
musclearray3.add( Validate.Presence );
|
||||
var musclearray4 = new LiveValidation('musclearray[4]');
|
||||
musclearray4.add( Validate.Numericality, { minimum: 1, maximum: 10, onlyInteger: true, failureMessage: "Must be a number between 1 and 10" });
|
||||
musclearray4.add( Validate.Presence );
|
||||
var musclearray5 = new LiveValidation('musclearray[5]');
|
||||
musclearray5.add( Validate.Numericality, { minimum: 1, maximum: 10, onlyInteger: true, failureMessage: "Must be a number between 1 and 10" });
|
||||
musclearray5.add( Validate.Presence );
|
||||
var musclearray6 = new LiveValidation('musclearray[6]');
|
||||
musclearray6.add( Validate.Numericality, { minimum: 1, maximum: 10, onlyInteger: true, failureMessage: "Must be a number between 1 and 10" });
|
||||
musclearray6.add( Validate.Presence );
|
||||
var musclearray7 = new LiveValidation('musclearray[7]');
|
||||
musclearray7.add( Validate.Numericality, { minimum: 1, maximum: 10, onlyInteger: true, failureMessage: "Must be a number between 1 and 10" });
|
||||
musclearray7.add( Validate.Presence );
|
||||
var musclearray8 = new LiveValidation('musclearray[8]');
|
||||
musclearray8.add( Validate.Numericality, { minimum: 1, maximum: 10, onlyInteger: true, failureMessage: "Must be a number between 1 and 10" });
|
||||
musclearray8.add( Validate.Presence );
|
||||
var musclearray9 = new LiveValidation('musclearray[9]');
|
||||
musclearray9.add( Validate.Numericality, { minimum: 1, maximum: 10, onlyInteger: true, failureMessage: "Must be a number between 1 and 10" });
|
||||
musclearray9.add( Validate.Presence );
|
||||
|
||||
// Pass a function that checks if a number is divisible by one that you pass it in args object
|
||||
// In this case, 5 is passed, so should return true and validation will pass
|
||||
//Validate.Custom( 55, { against: function(value,args){ return !(value % args.divisibleBy) }, args: {divisibleBy: 5} } );
|
||||
|
||||
// $.ajax({
|
||||
// type: 'get',
|
||||
// url: '/admin/exercises/',
|
||||
// data: { name: 'Cheddar'}
|
||||
// });
|
||||
|
||||
|
||||
// $.getJSON('admin/exercise', function(data) {
|
||||
// var items = [];
|
||||
|
||||
// $.each(data, function(key, val) {
|
||||
// items.push('<li id="' + key + '">' + val + '</li>');
|
||||
// });
|
||||
|
||||
// $('<ul/>', {
|
||||
// 'class': 'my-new-list',
|
||||
// html: items.join('')
|
||||
// }).appendTo('body');
|
||||
// });
|
||||
|
||||
|
||||
|
||||
});
|
||||
@@ -63,5 +63,7 @@ $(document).ready(function() {
|
||||
|
||||
$('#newworkout').trigger('sortupdate');
|
||||
|
||||
|
||||
|
||||
});
|
||||
|
||||
|
||||
111
routes/admin.js
111
routes/admin.js
@@ -3,11 +3,14 @@
|
||||
*/
|
||||
|
||||
var async = require('async');
|
||||
var Exercise = require('../data/models/workout');
|
||||
var Exercise = require('../data/models/exercise');
|
||||
var User = require('../data/models/user');
|
||||
var notLoggedIn = require('./middleware/not_logged_in');
|
||||
//var loadWorkout = require('./middleware/load_workout');
|
||||
var loggedIn = require('./middleware/logged_in');
|
||||
//var qs = require('querystring');
|
||||
var loadUser = require('./middleware/load_user');
|
||||
var loadExercise = require('./middleware/load_exercise');
|
||||
var isAdmin = require('./middleware/is_admin');
|
||||
var maxUsersPerPage = 15;
|
||||
var maxExercisesPerPage = 15;
|
||||
|
||||
module.exports = function(app) {
|
||||
@@ -16,11 +19,44 @@ app.get('/admin', loggedIn, isAdmin, function(req, res){
|
||||
res.render('admin/index', { title: 'Admin' });
|
||||
});
|
||||
|
||||
app.get('/admin/users', loggedIn, isAdmin, function(req, res){
|
||||
res.render('index', { title: 'Express' });
|
||||
app.get('/admin/users', loggedIn, isAdmin, function(req, res, next){
|
||||
var page = req.query.page && parseInt(req.query.page, 10) || 0;
|
||||
|
||||
User.count(function(err, count) {
|
||||
if (err) {
|
||||
return next(err);
|
||||
}
|
||||
var lastPage = (page + 1) * maxUsersPerPage >= count;
|
||||
|
||||
User.find({})
|
||||
.sort('name')
|
||||
.skip(page * maxUsersPerPage)
|
||||
.limit(maxUsersPerPage)
|
||||
.exec(function(err, users) {
|
||||
if (err) {
|
||||
return next(err);
|
||||
}
|
||||
res.render('admin/users', {
|
||||
title: 'Users',
|
||||
users: users,
|
||||
page: page,
|
||||
lastPage: lastPage
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
app.get('/admin/exercises', loggedIn, function(req, res, next){
|
||||
app.del('/admin/users/:_id', loadUser,
|
||||
function(req, res, next) {
|
||||
req.user.remove(function(err) {
|
||||
if (err) { return next(err); }
|
||||
res.redirect('/admin/users/');
|
||||
});
|
||||
});
|
||||
|
||||
////////////////////////////////////////////////
|
||||
|
||||
app.get('/admin/exercises', loggedIn, isAdmin, function(req, res, next){
|
||||
var page = req.query.page && parseInt(req.query.page, 10) || 0;
|
||||
async.parallel([
|
||||
|
||||
@@ -45,11 +81,11 @@ app.get('/admin/exercises', loggedIn, function(req, res, next){
|
||||
}
|
||||
|
||||
var count = results[0];
|
||||
var workouts = results[1];
|
||||
var exercises = results[1];
|
||||
|
||||
var lastPage = (page + 1) * maxExercisesPerPage >= count;
|
||||
|
||||
res.render('/admin/exercises/index', {
|
||||
res.render('admin/exercises', {
|
||||
title: 'exercises',
|
||||
exercises: exercises,
|
||||
page: page,
|
||||
@@ -60,53 +96,48 @@ app.get('/admin/exercises', loggedIn, function(req, res, next){
|
||||
);
|
||||
});
|
||||
|
||||
app.get('/admin/exercises/new', loggedIn, function(req, res) {
|
||||
res.render('admin/exercises/new', {title: "New Exercise"});
|
||||
app.get('/admin/exercises/new', loggedIn, isAdmin, function(req, res) {
|
||||
res.render('admin/newexercise', {title: "New Exercise"});
|
||||
});
|
||||
|
||||
|
||||
app.post('/admin/exercises/new', loggedIn, function(req, res, next) {
|
||||
console.log("/nreq.body" + JSON.stringify(req.body));
|
||||
var exerise = req.body;
|
||||
Exercise.create(workout, function(err) {
|
||||
if (err) {
|
||||
if (err.code === 11000) {
|
||||
res.send('Conflict', 409);
|
||||
} else {
|
||||
if (err.name === 'ValidationError') {
|
||||
return res.send(Object.keys(err.errors).map(function(errField) {
|
||||
return err.errors[errField].message;
|
||||
}).join('. '), 406);
|
||||
app.post('/admin/exercises', function(req, res, next) {
|
||||
console.log("/nreq.body" + JSON.stringify(req.body));
|
||||
var exercise = req.body;
|
||||
Exercise.create(exercise, function(err) {
|
||||
if (err) {
|
||||
if (err.code === 11000) {
|
||||
res.send('Conflict', 409);
|
||||
} else {
|
||||
next(err);
|
||||
if (err.name === 'ValidationError') {
|
||||
return res.send(Object.keys(err.errors).map(function(errField) {
|
||||
return err.errors[errField].message;
|
||||
}).join('. '), 406);
|
||||
} else {
|
||||
next(err);
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
return;
|
||||
}
|
||||
res.redirect('/admin/exercises');
|
||||
res.redirect('/admin/exercises');
|
||||
});
|
||||
});
|
||||
|
||||
app.get('/admin/exercises/:_id', loadExercise, function(req, res, next){
|
||||
res.render('admin/exercise', {title: req.exercise.name,
|
||||
exercise: req.exercise
|
||||
});
|
||||
});
|
||||
|
||||
app.del('/admin/exercises/:_id', loggedIn, loadWorkout, function(req, res, next) {
|
||||
req.workout.remove(function(err) {
|
||||
app.del('/admin/exercises/:_id', loggedIn, isAdmin, loadExercise, function(req, res, next) {
|
||||
req.exercise.remove(function(err) {
|
||||
if (err) { return next(err); }
|
||||
res.redirect('/admin/exercises');
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
|
||||
//////////////////////////////////////////
|
||||
|
||||
|
||||
|
||||
|
||||
app.get('/workouts/:_id', loadWorkout, function(req, res, next){
|
||||
res.render('workouts/workout', {title: req.workout.title,
|
||||
workout: req.workout});
|
||||
});
|
||||
|
||||
|
||||
|
||||
});
|
||||
|
||||
};
|
||||
17
routes/middleware/load_exercise.js
Normal file
17
routes/middleware/load_exercise.js
Normal file
@@ -0,0 +1,17 @@
|
||||
var exercise = require('../../data/models/exercise');
|
||||
|
||||
function loadexercise(req, res, next) {
|
||||
exercise.findOne({_id: req.params._id})
|
||||
.exec(function(err, exercise) {
|
||||
if (err) {
|
||||
return next(err);
|
||||
}
|
||||
if (! exercise) {
|
||||
return res.send('Not found', 404);
|
||||
}
|
||||
req.exercise = exercise;
|
||||
next();
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = loadexercise;
|
||||
@@ -11,12 +11,6 @@ function loadworkout(req, res, next) {
|
||||
if (! workout) {
|
||||
return res.send('Not found', 404);
|
||||
}
|
||||
console.log('workout' + JSON.stringify(workout));
|
||||
workout.elements.forEach(function(element, index, array) {
|
||||
// console.log("elementID" + element._id);
|
||||
// console.log("elementID" + JSON.stringify(element.splits));
|
||||
});
|
||||
|
||||
req.workout = workout;
|
||||
next();
|
||||
});
|
||||
|
||||
21
views/admin/exercise.jade
Normal file
21
views/admin/exercise.jade
Normal file
@@ -0,0 +1,21 @@
|
||||
extends ../layout
|
||||
|
||||
block content
|
||||
h1 Bodyrep
|
||||
p Exercise Admin
|
||||
|
||||
p
|
||||
a(href="/users") List Users
|
||||
p
|
||||
a(href="/workouts") List workouts
|
||||
p
|
||||
a(href="/admin") Admin Index
|
||||
p
|
||||
a(href="/admin/users") Admin Users
|
||||
p
|
||||
a(href="/admin/exercises") Admin Exercises
|
||||
p
|
||||
a(href="/admin/exercises/new") Add Exercise
|
||||
hr
|
||||
|
||||
p= exercise
|
||||
37
views/admin/exercises.jade
Normal file
37
views/admin/exercises.jade
Normal file
@@ -0,0 +1,37 @@
|
||||
extends ../layout
|
||||
|
||||
block content
|
||||
h1 Bodyrep
|
||||
p Exercise Admin
|
||||
|
||||
p
|
||||
a(href="/users") List Users
|
||||
p
|
||||
a(href="/workouts") List workouts
|
||||
p
|
||||
a(href="/admin") Admin Index
|
||||
p
|
||||
a(href="/admin/users") Admin Users
|
||||
p
|
||||
a(href="/admin/exercises") Admin Exercises
|
||||
p
|
||||
a(href="/admin/exercises/new") Add Exercise
|
||||
hr
|
||||
|
||||
ul
|
||||
- exercises.forEach(function(exercise) {
|
||||
li
|
||||
a(href="/admin/exercises/" + encodeURIComponent(exercise._id))= exercise.name
|
||||
form(action="/admin/exercises/" + encodeURIComponent(exercise._id), method="POST")
|
||||
input(name="_method", type="hidden", value="DELETE")
|
||||
input(type="submit", value="Delete")
|
||||
- });
|
||||
|
||||
- if (page > 0) {
|
||||
a(href="?page=" + (page - 1)) Previous
|
||||
|
||||
- }
|
||||
|
||||
- if (! lastPage) {
|
||||
a(href="?page=" + (page + 1)) Next
|
||||
- }
|
||||
16
views/admin/index.jade
Normal file
16
views/admin/index.jade
Normal file
@@ -0,0 +1,16 @@
|
||||
extends ../layout
|
||||
|
||||
block content
|
||||
h1 Bodyrep
|
||||
p Admin Index
|
||||
|
||||
p
|
||||
a(href="/users") List Users
|
||||
p
|
||||
a(href="/workouts") List workouts
|
||||
p
|
||||
a(href="/admin") Admin Index
|
||||
p
|
||||
a(href="/admin/users") Admin Users
|
||||
p
|
||||
a(href="/admin/exercises") Admin Exercises
|
||||
44
views/admin/newexercise.jade
Normal file
44
views/admin/newexercise.jade
Normal file
@@ -0,0 +1,44 @@
|
||||
extends ../layout
|
||||
|
||||
block additionalscripts
|
||||
script(src='/javascripts/newexercisevalidation.js')
|
||||
|
||||
block content
|
||||
h1 New Exercise<p></p>
|
||||
|
||||
form(method="POST", action="/admin/exercises")
|
||||
ul
|
||||
li
|
||||
label(for="name") Exercise Name<br />Need to add check to ensure we donbt get duplicate names<br />
|
||||
input#name(name="name")
|
||||
li
|
||||
label(for="type") Exercise Type<br />
|
||||
select#type(name="type")
|
||||
option(value='notselected') Select One
|
||||
option(value='freeweights') Free Weights
|
||||
option(value='0130') Exercise Machines
|
||||
option(value='0200') Bodyweight
|
||||
option(value='0200') Kettlebells
|
||||
option(value='0200') Cardio
|
||||
|
||||
li
|
||||
label(for="type") Exercise Description<br />
|
||||
input#description(name="description", class="needsnumber")
|
||||
li
|
||||
label(for="type") Exercise Difficulty<br />
|
||||
input#difficulty(name="difficulty")
|
||||
i = 0
|
||||
- for (i ; i < 10; ++i) {
|
||||
marray = "musclearray[" + i + "]"
|
||||
li
|
||||
label(for="type") Muscle data #{i}
|
||||
input(name= marray, class= marray, id= marray)
|
||||
- }
|
||||
li
|
||||
label(for="type") Keywords<br />
|
||||
li
|
||||
label(for="type") Alternatives<br />
|
||||
li
|
||||
label(for="type") Datafields<br />
|
||||
li
|
||||
input(type="submit", value="Create")
|
||||
14
views/admin/newexercisevalidation.jade
Normal file
14
views/admin/newexercisevalidation.jade
Normal file
@@ -0,0 +1,14 @@
|
||||
/script(
|
||||
$(document).ready(function() {
|
||||
////Validation
|
||||
var name = new LiveValidation('name');
|
||||
name.add( Validate.Format, { pattern: /^\s*[a-zA-Z0-9,\s]+\s*$/ } );
|
||||
name.add( Validate.Length, { minimum: 3, maximum: 12 , failureMessage: "Please enter a valid name" } );
|
||||
var type = new LiveValidation('type');
|
||||
type.add( Validate.Exclusion, { within: ['notselected', 'Select One'], failureMessage: "Please select an entry from the list" } );
|
||||
var difficulty = new LiveValidation('difficulty');
|
||||
difficulty.add( Validate.Numericality, { minimum: 1, maximum: 10, onlyInteger: true, failureMessage: "Must be a number between 1 and 10" });
|
||||
var description = new LiveValidation('description');
|
||||
description.add( Validate.Format, { pattern: /^\s*[a-zA-Z0-9,\s]+\s*$/ } );
|
||||
});
|
||||
/)
|
||||
36
views/admin/users.jade
Normal file
36
views/admin/users.jade
Normal file
@@ -0,0 +1,36 @@
|
||||
extends ../layout
|
||||
|
||||
block content
|
||||
h1 Bodyrep
|
||||
p User Admin
|
||||
|
||||
p
|
||||
a(href="/users") List Users
|
||||
p
|
||||
a(href="/workouts") List workouts
|
||||
p
|
||||
a(href="/admin") Admin Index
|
||||
p
|
||||
a(href="/admin/users") Admin Users
|
||||
p
|
||||
a(href="/admin/exercises") Admin Exercises
|
||||
|
||||
hr
|
||||
|
||||
ul
|
||||
- users.forEach(function(user) {
|
||||
li
|
||||
a(href="/users/" + encodeURIComponent(user.username))= user.name
|
||||
form(action="/admin/users/" + encodeURIComponent(user._id), method="POST")
|
||||
input(name="_method", type="hidden", value="DELETE")
|
||||
input(type="submit", value="Delete")
|
||||
- });
|
||||
|
||||
- if (page > 0) {
|
||||
a(href="?page=" + (page - 1)) Previous
|
||||
|
||||
- }
|
||||
|
||||
- if (! lastPage) {
|
||||
a(href="?page=" + (page + 1)) Next
|
||||
- }
|
||||
@@ -4,6 +4,7 @@ html
|
||||
title BodyREP - #{title}
|
||||
link(rel='stylesheet', '/stylesheets/style.css')
|
||||
include scripts
|
||||
block additionalscripts
|
||||
body
|
||||
include session/user
|
||||
block content
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
script(src='http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js')
|
||||
script(src='http://ajax.googleapis.com/ajax/libs/jqueryui/1.9.2/jquery-ui.min.js')
|
||||
script(src='http://jzaefferer.github.com/jquery-validation/jquery.validate.js')
|
||||
script(src='/javascripts/scripts.js')
|
||||
//script(src='/javascripts/livevalidation_standalone.compressed.js')
|
||||
//link(rel='stylesheet', href='/stylesheets/smoothness/jquery-ui-1.8.20.custom.css',type='text/css' )
|
||||
link(rel='stylesheet', href='/stylesheets/redmond/jquery-ui-1.10.0.custom.css',type='text/css' )
|
||||
link(rel='stylesheet', href='/stylesheets/redmond/jquery-ui-1.10.0.custom.css',type='text/css' )
|
||||
|
||||
@@ -22,4 +22,4 @@ block content
|
||||
|
||||
- if (session.user)
|
||||
- if (session.user.is_admin === true)
|
||||
a(href="/users/") Goto Admin Page
|
||||
a(href="/admin") Goto Admin Page
|
||||
Reference in New Issue
Block a user