diff --git a/public/javascripts/newexercisevalidation.js b/public/javascripts/newexercisevalidation.js index bd6a315..156fcac 100644 --- a/public/javascripts/newexercisevalidation.js +++ b/public/javascripts/newexercisevalidation.js @@ -16,7 +16,15 @@ $("#newexercise").validate({ } }, dataFilter: function(data) { - return (JSON.parse(data).name); + if($("#name").attr("value") === $("#name").attr("oldvalue") ) { + console.log("Old = New " + JSON.parse(data).name); + return JSON.stringify(true); + //return $.toJSON(true); + } else { + console.log("Old != New"); + return (JSON.parse(data).name); + } + } } }, @@ -46,13 +54,11 @@ $(".musclearray").each(function(){ $(this).rules("add", { required: true, digits: true, - range: [1, 10], + range: [0, 10], messages: { - required: "Muscle Array values must be betwen 1 and 10" + required: "Muscle Array values must be betwen 0 and 10" } }); }); - - }); \ No newline at end of file diff --git a/routes/admin.js b/routes/admin.js index fdedfd8..6491082 100644 --- a/routes/admin.js +++ b/routes/admin.js @@ -124,11 +124,42 @@ app.get('/admin/exercises', loggedIn, isAdmin, function(req, res, next){ }); app.get('/admin/exercises/:_id', loadExercise, function(req, res, next){ - res.render('admin/exercise', {title: req.exercise.name, + res.render('admin/editexercise', {title: req.exercise.name, exercise: req.exercise }); }); + app.post('/admin/exercises/:_id', function(req, res, next){ + console.log("Upsert data " + JSON.stringify(req.body)); + var exercise = req.body; + console.log("Upsert exercise " + JSON.stringify(exercise)); + var docid = req.body._id; + console.log("DOCID " + docid); + delete exercise["_id"]; + console.log("Upsert document " + JSON.stringify(exercise)); + Exercise.findByIdAndUpdate(docid, exercise, {upsert: false}, 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); + } else { + next(err); + } + } + return; + } + res.redirect('/admin/exercises'); + }); + + + + }); + + app.del('/admin/exercises/:_id', loggedIn, isAdmin, loadExercise, function(req, res, next) { req.exercise.remove(function(err) { if (err) { return next(err); } @@ -144,13 +175,9 @@ app.get('/admin/exercises', loggedIn, isAdmin, function(req, res, next){ return next(err); } if (! exercise) { - console.log("Failure" + JSON.stringify({ success : false })); return res.send(JSON.stringify({ "name": "true" })); } - //req.exercise = exercise; - - console.log("Success" + exercise.name); - res.send(JSON.stringify({ "name": "false" })); + res.send(JSON.stringify({ "name": "false" })); }); });