Skip to content

Instantly share code, notes, and snippets.

@ebourmalo
Last active August 29, 2015 14:27
Show Gist options
  • Save ebourmalo/95df7ef2221ecbe95876 to your computer and use it in GitHub Desktop.
Save ebourmalo/95df7ef2221ecbe95876 to your computer and use it in GitHub Desktop.
/**
* [Action] Add an answer to a question
*
* @param req {String} - Identifier of the question to add to
* @param res {Object} - Data of the new answer
*/
function addAnswer(request, response) {
var questionId = request.params.questionId;
var newAnswer = request.body;
async.auto({
getQuestion: function(callback) {
Question.findById(questionId)
.populate('author')
.exec(callback);
},
createAnswer: ['getQuestion', function(callback, results) {
var answer = new Answers({
question: questionId,
description: newAnswer.description,
fileId: newAnswer.fileId,
author: newAnswer.authorId
});
answer.save(function(err, answer, count) {
console.log('routes/answers - Save answer:', err, answer, count);
cb(err, answer);
});
}],
updateQuestion: function(callback) {
question.lastActivity = new Date();
question.save(callback);
},
getAuthorsOtherAnswers: function(callback) {
// Find authors of others answers (called followers)
},
sendEmailsToFollowers: ['getQuestion', 'createAnswer', 'getAuthorsOtherAnswers', function(cb, results) {
// Send emails to authors of others answers of the question
}]
}, function(error, results) {
if (error) {
console.error('Something went wrong', error);
return response.status(400).send('An error occured');
}
response.json(results.createAnswer);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment