diff --git a/controllers/answer.js b/controllers/answer.js index 5501656..35b593b 100644 --- a/controllers/answer.js +++ b/controllers/answer.js @@ -10,20 +10,42 @@ function AnswerController(db) { }; AnswerController.prototype.answer = function(request, reply) { - try { - - -/* -Bu kısımda yapılacaklar: - -1- userId'sini güncelle installation_id'nin -2- answer create -3- question'ın count'unu düzenle - - -*/ - - // this.answerModel.answer(); + try { + + var self = this; + + async.waterfall([ + function(callback) { + + var newAnswer = {}; + newAnswer.installation_id = request.headers['x-voter-installation']; + newAnswer.option = request.payload.option; + newAnswer.question_id = request.payload.question_id; + newAnswer.user_id = request.payload.user_id; + newAnswer.text = request.payload.text; + newAnswer.client_id = request.payload.client_id; + + + self.answerModel.answer(newAnswer, function(answered) { + callback(null,answered); + + }); + + }, + function(user_data, callback) { + //question düzenle + }, + function(user_data, callback) { + //user'i düzenle + } + ], function(err, result) { + if (!result || err) { + reply(Boom.badImplementation(JSON.stringify(err))); + return; + } else { + reply({ data: result }); + } + }); } catch (e) { reply(Boom.badRequest(e.message)); diff --git a/models/question.js b/models/question.js index 69acf34..5c483da 100644 --- a/models/question.js +++ b/models/question.js @@ -33,4 +33,27 @@ QuestionModel.prototype.fetchQuestions = function(app, cb) { }); }; +QuestionModel.prototype.increaseStats = function(answer,cb) { + + + switch(answer.option) { + case 'a': + code block + break; + case 'b': + code block + break; + case 's': + code block + break; + default: + default code block +} + + this.questionSchema.findAll().then(function(questions) { + cb(questions); + }); +}; + + module.exports = QuestionModel; diff --git a/routes/v1/answer.js b/routes/v1/answer.js index 127c681..b034fcb 100644 --- a/routes/v1/answer.js +++ b/routes/v1/answer.js @@ -18,19 +18,16 @@ exports.register = function(server, options, next) { config: { description: 'Answer route', tags: ['api', 'user', 'add'], - notes: ['Answer','cevaplardan' - ], + notes: ['Answer','cevaplardan'], handler: answerController.answer, validate: { payload: Joi.object().keys({ installation_id: Joi.string().required().description('installation_id veya imei'), - option:Joi.string().required().description('installation_id veya imei'), - question_text: Joi.when('app', { is: 0, then: Joi.required() }).description('Soru metni'), - question_image: Joi.when('app', { is: 0, then: Joi.required() }).description('Soru resim linki'), - user_id: Joi.string().required().description('User id'), - app: Joi.number().min(0).max(1).required().description('App referandum için 0, kapistir için 1'), - option_a: Joi.when('app', { is: 1, then: Joi.required() }).description('A şıkkı metni ya da resim linki'), - option_b: Joi.when('app', { is: 1, then: Joi.required() }).description('B şıkkı metni ya da resim linki'), + option:Joi.string().required().description(' soldaki "a" sağdaki "b" skip "s" '), + question_id:Joi.string().required().description('question_id'), + user_id:Joi.string().required().description('user_id'), + text:Joi.string().required().description('answer text or image link'), + client_id:Joi.string().required().description('kapistir için 1 referandum için 0') }), headers: Joi.object({ 'x-voter-client-id': Joi.string().required().description('Her app için farklı olacak.'),