mirror of
https://github.com/bodyrep/DemoApp.git
synced 2026-02-04 19:42:00 +00:00
fixed validation for exercise creation (inlcuding unique exercise name), also added ability to edit exercises
This commit is contained in:
@@ -16,7 +16,15 @@ $("#newexercise").validate({
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
dataFilter: function(data) {
|
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", {
|
$(this).rules("add", {
|
||||||
required: true,
|
required: true,
|
||||||
digits: true,
|
digits: true,
|
||||||
range: [1, 10],
|
range: [0, 10],
|
||||||
messages: {
|
messages: {
|
||||||
required: "Muscle Array values must be betwen 1 and 10"
|
required: "Muscle Array values must be betwen 0 and 10"
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
});
|
});
|
||||||
@@ -124,11 +124,42 @@ app.get('/admin/exercises', loggedIn, isAdmin, function(req, res, next){
|
|||||||
});
|
});
|
||||||
|
|
||||||
app.get('/admin/exercises/:_id', loadExercise, 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
|
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) {
|
app.del('/admin/exercises/:_id', loggedIn, isAdmin, loadExercise, function(req, res, next) {
|
||||||
req.exercise.remove(function(err) {
|
req.exercise.remove(function(err) {
|
||||||
if (err) { return next(err); }
|
if (err) { return next(err); }
|
||||||
@@ -144,13 +175,9 @@ app.get('/admin/exercises', loggedIn, isAdmin, function(req, res, next){
|
|||||||
return next(err);
|
return next(err);
|
||||||
}
|
}
|
||||||
if (! exercise) {
|
if (! exercise) {
|
||||||
console.log("Failure" + JSON.stringify({ success : false }));
|
|
||||||
return res.send(JSON.stringify({ "name": "true" }));
|
return res.send(JSON.stringify({ "name": "true" }));
|
||||||
}
|
}
|
||||||
//req.exercise = exercise;
|
res.send(JSON.stringify({ "name": "false" }));
|
||||||
|
|
||||||
console.log("Success" + exercise.name);
|
|
||||||
res.send(JSON.stringify({ "name": "false" }));
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user