depurating picto expression update

parent 34168770
Showing with 30 additions and 42 deletions
......@@ -218,49 +218,37 @@ module.exports = {
change_exp: function (req, res) {
var params = req.params.all();
if (params.lang && params.text) {
PictoExp.findOne({ id: params.picto, lang: params.lang }).then(function (pictoExpression) {
if (!pictoExpression) {
Picto.findOne({ id: params.picto }).then(function (picto) {
if (!picto) {
res.badRequest();
throw new Error('Picto not found');
}
PictoExp.create({
lang: params.lang,
text: params.text,
picto: picto.id
}).then(function (newPictoExpression) {
if (newPictoExpression) {
res.ok(newPictoExpression);
} else {
res.serverError();
}
})
.catch(function () {
res.serverError();
});
})
.catch(function () {
res.serverError();
});
} else {
pictoExpression.text = params.text;
pictoExpression.save(function (error) {
if (error) {
res.serverError();
} else {
res.ok(pictoExpression);
}
if (!params.lang || !params.text)
return res.badRequest();
PictoExp.findOne({ id: params.picto, lang: params.lang })
.then(function (pictoExpression) {
if (!pictoExpression) {
Picto.findOne({ id: params.picto }).then(function (picto) {
if (!picto)
return res.badRequest('Error: Picto not found');
PictoExp.create({
lang: params.lang,
text: params.text,
picto: picto.id
}).then(function (newPictoExpression) {
if (!newPictoExpression)
throw new Error("Could not create picto expression");
return res.ok(newPictoExpression);
});
}
})
.catch(function () {
res.serverError();
});
} else {
res.badRequest();
}
});
} else {
pictoExpression.text = params.text;
pictoExpression.save(function (error) {
if (error)
throw new Error("Could not save expression");
return res.ok(pictoExpression);
});
}
})
.catch(function (err) {
return res.serverError("Error from server: " + err);
});
},
/**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment