Skip to content

Commit

Permalink
unfavorite and closes #2 , closes #3
Browse files Browse the repository at this point in the history
  • Loading branch information
eferhatg committed Jul 31, 2016
1 parent 13ca8f5 commit 7f04743
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 28 deletions.
5 changes: 3 additions & 2 deletions controllers/answer.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,20 +119,21 @@ 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){
delete newAnswer.user_id;
}

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);

});
Expand Down
3 changes: 2 additions & 1 deletion controllers/question.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
Expand Down
44 changes: 36 additions & 8 deletions controllers/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions models/answer.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,15 @@ 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);
});
} else {

var dbAnswer = answerList[0];
dbAnswer.update({
is_favorite : true
is_favorite : !answer.unfavorite
}).then(function() {
cb(dbAnswer);
});
Expand Down
42 changes: 30 additions & 12 deletions models/question.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
});


};

Expand Down Expand Up @@ -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 }
Expand All @@ -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 }
Expand Down
3 changes: 2 additions & 1 deletion routes/v1/answer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.'),
Expand Down
8 changes: 6 additions & 2 deletions routes/v1/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
}
Expand Down

0 comments on commit 7f04743

Please sign in to comment.