Skip to content

Commit

Permalink
question randomized and answer fixess
Browse files Browse the repository at this point in the history
  • Loading branch information
eferhatg committed Apr 22, 2016
1 parent 9c348c6 commit 19d8886
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 41 deletions.
2 changes: 1 addition & 1 deletion controllers/answer.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ AnswerController.prototype.answer = function(request, reply) {
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;
newAnswer.client_id = request.payload.client_id.toString();

self.answerModel.answer(newAnswer, function(answered) {
callback(null,answered);
Expand Down
4 changes: 2 additions & 2 deletions db_schemas/answer.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ module.exports = function(sequelize, DataTypes) {
primaryKey: true,
defaultValue: Sequelize.UUIDV1
},
question_id: DataTypes.STRING,
user_id: DataTypes.STRING,
question_id: Sequelize.UUID,
user_id: Sequelize.UUID,
text: DataTypes.STRING,
option: DataTypes.STRING,
installation_id: DataTypes.STRING,
Expand Down
2 changes: 1 addition & 1 deletion db_schemas/question.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module.exports = function(sequelize, DataTypes) {
},
question_text: DataTypes.STRING,
question_image: DataTypes.STRING,
user_id: DataTypes.STRING,
user_id: Sequelize.UUID,
app: DataTypes.INTEGER, // 0: referandum, 1: kapıştır
option_a: DataTypes.STRING,
option_b: DataTypes.STRING,
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ server.ext('onPreResponse', function(request, reply) {
});

//Sync with database
db.sequelize.sync({ logging: true }).then(
db.sequelize.sync({ logging: (process.env.DBLOG||false) }).then(

function(result) {
//Register the plugins
Expand Down
48 changes: 23 additions & 25 deletions models/answer.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,45 @@ var crypto = require('crypto');

function AnswerModel(db) {
this.answerSchema = db.answer;
this.db = db;
};

AnswerModel.prototype.answer = function(answer, cb) {


var self = this;
var whereQuery = { question_id: answer.question_id, client_id: answer.client_id };

//eğer user_id varsa user_id yoksa installation_id üzerinden
var rawQuery = 'select * from answer where client_id=? and question_id=?::UUID and ';

if (answer.user_id.length > 0) {
whereQuery.user_id = answer.user_id;
rawQuery = rawQuery + 'answer.user_id=?::UUID;'
} else {
whereQuery.installation_id = answer.installation_id;
rawQuery = rawQuery + 'answer.installation_id=?::UUID;'
}

self.db.sequelize.query(rawQuery, { replacements: [answer.client_id, answer.question_id, (answer.user_id.length > 0) ? answer.user_id : answer.installation_id], type: self.db.sequelize.QueryTypes.SELECT, model: self.answerSchema })
.then(function(answerList) {
if (answerList.length == 0) {
self.answerSchema.create(answer).then(function(createdAnswer) {
cb(createdAnswer);
});
} else {

this.answerSchema.findAll({
where: whereQuery
}).then(function(answerList) {

if (answerList.length == 0) {
this.answerSchema.create(answer).then(function(createdAnswer) {
cb(createdAnswer);
});
} else {

var dbAnswer = answerList[0];
var dbAnswer = answerList[0];

if (dbAnswer.option == 's' && answer.option !== 's') {
//update et
dbAnswer.update({
option: answer.option
}).then(function() {
if (dbAnswer.option == 's' && answer.option !== 's') {
//update et
dbAnswer.update({
option: answer.option
}).then(function() {
cb(dbAnswer);
});
} else {
cb(dbAnswer);
});
} else {
cb(dbAnswer);
}
}
}
})

});
};


Expand Down
21 changes: 10 additions & 11 deletions models/question.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var Sequelize = require('sequelize');

function QuestionModel(db) {
this.questionSchema = db.question;
this.sequelize=db.sequelize;
this.sequelize = db.sequelize;
};

QuestionModel.prototype.insertQuestion = function(question, cb) {
Expand All @@ -26,23 +26,22 @@ QuestionModel.prototype.fetchQuestions = function(app, limit, user_id, installat

var rawQuery = "DROP TABLE IF EXISTS tempUserQuestionTable; " +
"CREATE TEMP TABLE tempUserQuestionTable AS " +
"select (row_number() over ()) as rn, * from question where question.app=? and question.is_deleted=FALSE " +
"select * from question where question.app=? and question.is_deleted=FALSE " +
"EXCEPT " +
"select (row_number() over ()) as rn,q.* from answer a inner JOIN question q on a.question_id=q.id ";
"select q.* from answer a inner JOIN question q on a.question_id=q.id ";

rawQuery = rawQuery + ((user_id) ? "where a.user_id=(?::UUID); " : "where a.installation_id=?; ");

rawQuery = rawQuery + "select * from tempUserQuestionTable where rn in (" +
"select round(random() * (select count( * ) from tempUserQuestionTable))::integer as id " +
"from generate_series(1, (select count( * ) from tempUserQuestionTable))" +
rawQuery = rawQuery + "select * from (select (row_number() over ()) as rn,* from tempUserQuestionTable) as tu where tu.rn in (" +
"select DISTINCT round(random() * (select count( * ) from tempUserQuestionTable))::integer as id " +
"from generate_series(1, 100)" +
") limit ?;";


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

0 comments on commit 19d8886

Please sign in to comment.