diff --git a/controllers/answer.js b/controllers/answer.js index 059a570..5943f43 100644 --- a/controllers/answer.js +++ b/controllers/answer.js @@ -119,6 +119,7 @@ AnswerController.prototype.setFavorite = function(request, reply) { newAnswer.question_id = request.payload.question_id; newAnswer.user_id = request.payload.user_id; newAnswer.text = ''; + newAnswer.unfavorite=request.payload.unfavorite; newAnswer.client_id = request.payload.client_id.toString(); if(!request.payload.user_id||request.payload.user_id.length==0){ @@ -126,13 +127,13 @@ AnswerController.prototype.setFavorite = function(request, reply) { } self.answerModel.setFavorite(newAnswer, function(answered) { - callback(null,answered); + callback(null,newAnswer); }); }, function(answer, callback) { - self.questionModel.increaseAbuse(answer, function(answered) { + self.questionModel.increaseFavorite(answer, function(answered) { callback(null,answer); }); diff --git a/controllers/question.js b/controllers/question.js index 1db77f4..81d062b 100644 --- a/controllers/question.js +++ b/controllers/question.js @@ -5,6 +5,7 @@ var QuestionModel = require('../models/question'); var cloudinary = require('cloudinary'); var fs = require('fs'); var config_params = require(__dirname + '/../config/config.json'); +var async = require('async'); function QuestionController(db) { @@ -56,7 +57,7 @@ QuestionController.prototype.all = function(request, reply) { QuestionController.prototype.userQuestions = function(request, reply) { - this.questionModel.userquestions(request.params.app, request.query.user_id, request.query.limit,function(data) { + this.questionModel.userQuestions(request.params.app, request.query.user_id, request.query.limit,function(data) { reply({ data: { "count": data.length, diff --git a/controllers/user.js b/controllers/user.js index fc3e0d4..054d5b2 100644 --- a/controllers/user.js +++ b/controllers/user.js @@ -6,7 +6,7 @@ var QuestionModel = require('../models/question'); var FB = require('fb'); var cloudinary = require('cloudinary'); var async = require('async'); -var config_params = require(__dirname + '/../config/config.json'); +var config_params = require(__dirname + '/../config/config.json'); function UserController(db) { @@ -72,14 +72,42 @@ UserController.prototype.signup = function(request, reply) { UserController.prototype.fetchUserQuestions = function(request, reply) { try { +var self=this; + var data = {}; + async.waterfall([ + function(callback) { + + self.questionModel.userQuestions(request.query.user_id,request.query.app, request.query.limit, function(q) { + data.questions = { + "count": q.length, + "rows": q + }; + callback(null, data); + + }); + + + }, + function(data, callback) { + + self.questionModel.userFavorites( request.query.user_id,request.query.app, request.query.limit, function(f) { + data.favorites = { + "count": f.length, + "rows": f + }; + callback(null, data); - this.questionModel.userQuestions(request.query.user_id,request.query.app,request.query.limit, function(data) { - reply({ - data: { - "count": data.length, - "rows": data - } - }); + }); + + + } + ], function(err, result) { + if (!result || err) { + reply(Boom.badImplementation(JSON.stringify(err))); + return; + } else { + reply({data:result}); + } }); } catch (e) { diff --git a/models/answer.js b/models/answer.js index f6e3dff..ce57a0e 100644 --- a/models/answer.js +++ b/models/answer.js @@ -100,7 +100,7 @@ AnswerModel.prototype.setFavorite = function(answer, cb) { self.db.sequelize.query(rawQuery, { replacements: [answer.client_id, answer.question_id, answer.user_id ? answer.user_id : answer.installation_id], type: self.db.sequelize.QueryTypes.SELECT, model: self.answerSchema }) .then(function(answerList) { if (answerList.length == 0) { - answer.is_favorite = true; + answer.is_favorite = !answer.unfavorite; self.answerSchema.create(answer).then(function(createdAnswer) { cb(createdAnswer); }); @@ -108,7 +108,7 @@ AnswerModel.prototype.setFavorite = function(answer, cb) { var dbAnswer = answerList[0]; dbAnswer.update({ - is_favorite : true + is_favorite : !answer.unfavorite }).then(function() { cb(dbAnswer); }); diff --git a/models/question.js b/models/question.js index 5bc5dc4..9f678d2 100644 --- a/models/question.js +++ b/models/question.js @@ -36,6 +36,21 @@ QuestionModel.prototype.userQuestions = function(user_id, app, limit, cb) { }); }; +QuestionModel.prototype.userFavorites = function(user_id, app, limit, cb) { + + var rawQuery = 'select * from question where id in(SELECT question_id from answer where user_id=? and answer.is_favorite=true)' + + 'and question.is_deleted=FALSE and question.app=? limit ?;' + this.sequelize.query(rawQuery, { + replacements: [user_id, app, limit], + type: this.sequelize.QueryTypes.SELECT, + // model: this.questionSchema + }) + .then(function(questions) { + cb(questions); + }); + +}; + QuestionModel.prototype.fetchQuestions = function(app, limit, user_id, installation_id, debug, cb) { var debugMode = debug ? debug : 0; @@ -87,18 +102,18 @@ QuestionModel.prototype.fetchQuestions = function(app, limit, user_id, installat QuestionModel.prototype.getAllKapistirForWeb = function(cb) { - + var rawQuery = 'select tu.*,u.profile_img as asker_profile_img,u.facebook_id, u.name as asker_name from question as tu inner JOIN "user" as u on u.id=tu.user_id where tu.app=1 ORDER BY created_at desc'; - this.sequelize.query(rawQuery, { - //replacements: [app, (user_id) ? user_id : installation_id, limit], - type: this.sequelize.QueryTypes.SELECT, - // model: this.questionSchema - }) - .then(function(questions) { - cb(questions); - }); - + this.sequelize.query(rawQuery, { + //replacements: [app, (user_id) ? user_id : installation_id, limit], + type: this.sequelize.QueryTypes.SELECT, + // model: this.questionSchema + }) + .then(function(questions) { + cb(questions); + }); + }; @@ -135,7 +150,7 @@ QuestionModel.prototype.increaseStats = function(answer, cb) { QuestionModel.prototype.increaseAbuse = function(answer, cb) { - var incrementField = { abuse_count: Sequelize.literal('abuse_count+1') } + var incrementField = { abuse_count: Sequelize.literal('abuse_count+1') } this.questionSchema.update(incrementField, { where: { id: answer.question_id } @@ -146,8 +161,11 @@ QuestionModel.prototype.increaseAbuse = function(answer, cb) { }; QuestionModel.prototype.increaseFavorite = function(answer, cb) { + var incrementField = { favorite_count: Sequelize.literal('favorite_count+1') } + if (answer.unfavorite===true) { + incrementField = { favorite_count: Sequelize.literal('favorite_count-1') } + } - var incrementField = { favorite_count: Sequelize.literal('favorite_count+1') } this.questionSchema.update(incrementField, { where: { id: answer.question_id } diff --git a/routes/v1/answer.js b/routes/v1/answer.js index 8b9091f..e776242 100644 --- a/routes/v1/answer.js +++ b/routes/v1/answer.js @@ -126,7 +126,8 @@ exports.register = function(server, options, next) { payload: Joi.object().keys({ question_id: Joi.string().required().description('question_id'), user_id: Joi.string().required().allow('').description('user_id'), - client_id: Joi.number().required().description('kapistir için 1 referandum için 0') + client_id: Joi.number().required().description('kapistir için 1 referandum için 0'), + unfavorite:Joi.boolean().required().description('favori için false olmalı, unfavorite için true olmalı') }), headers: Joi.object({ 'x-voter-client-id': Joi.string().required().description('Her app için farklı olacak.'), diff --git a/routes/v1/user.js b/routes/v1/user.js index ad28a73..56bf427 100644 --- a/routes/v1/user.js +++ b/routes/v1/user.js @@ -88,8 +88,12 @@ exports.register = function(server, options, next) { message: "success", timestamp: Date.now(), data: { - "count": 17, - "rows": 'question array' + "questions": { + "count": 17, + "rows": 'question array'}, + "favorites": { + "count": 17, + "rows": 'favorites array'}, } }).label('Result') }