Skip to content

Commit

Permalink
push integration
Browse files Browse the repository at this point in the history
  • Loading branch information
efg authored and efg committed May 4, 2017
1 parent 237762b commit 858aad8
Show file tree
Hide file tree
Showing 13 changed files with 249 additions and 99 deletions.
4 changes: 3 additions & 1 deletion controllers/kapistir.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ KapistirController.prototype.all = function(request, reply) {
reply(Boom.notFound(e.message));
}
};

KapistirController.prototype.ref = function(request, reply) {
reply.view('ref',null);
};



Expand Down
95 changes: 66 additions & 29 deletions controllers/question.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@

var Boom = require('boom');
var QuestionModel = require('../models/question');
var UserModel = require('../models/user');
var cloudinary = require('cloudinary');
var fs = require('fs');
var config_params = require(__dirname + '/../config/config.json');
var config_params = require(__dirname + '/../config/config.json');
var async = require('async');


function QuestionController(db) {
this.questionModel = new QuestionModel(db);
this.userModel = new UserModel(db);
};

QuestionController.prototype.addQuestion = function(request, reply) {
QuestionController.prototype.addQuestion = function (request, reply) {
try {
var newQuestion = {
"user_id": request.payload.user_id,
Expand All @@ -29,21 +31,56 @@ QuestionController.prototype.addQuestion = function(request, reply) {
newQuestion.question_image = request.payload.question_image;
newQuestion.option_b = 'hayir';
newQuestion.option_a = 'evet';
}
newQuestion.is_private = request.payload.is_private == null ? false : request.payload.is_private;
newQuestion.private_url = request.payload.private_url == null ? "" : request.payload.private_url;

this.questionModel.insertQuestion(newQuestion, function(createdQuestion) {
}
var self = this;
async.waterfall([
function (callback) {
self.questionModel.insertQuestion(newQuestion, function (createdQuestion) {
callback(null, createdQuestion);
});
},
function (createdQuestion, callback) {
if (!request.payload.notify_friend) {
callback(null, createdQuestion);
} else {
var friendsArray = [];
self.userModel.getUserFriends(newQuestion.user_id, newQuestion.app, function (users) {
if (users.length == 0) {
callback(null, createdQuestion);
}
else {
for (var i = 0; i < users.length; i++) {
var mUser = users[i];
if (mUser.reg_id != null || mUser.reg_id != "") {
friendsArray.push(mUser.reg_id);
}
}
self.userModel.findUserById(newQuestion.user_id, function (userinfo) {
createdQuestion.asker_name=userinfo.name;
self.questionModel.notifyFriends(createdQuestion, friendsArray, function (err, info) {
callback(null, createdQuestion);
});
});

}
});
}
}
], function (err, createdQuestion) {
reply({ data: createdQuestion });
});


} catch (e) {
reply(Boom.badRequest(e.message));
}
};

QuestionController.prototype.editQuestion = function(request, reply) {
QuestionController.prototype.editQuestion = function (request, reply) {
try {
var newQuestion = {
var newQuestion = {
"id": request.params.id
};

Expand All @@ -59,7 +96,7 @@ QuestionController.prototype.editQuestion = function(request, reply) {
newQuestion.option_a = 'evet';
}

this.questionModel.editQuestion(newQuestion, function(editedQuestion) {
this.questionModel.editQuestion(newQuestion, function (editedQuestion) {
reply({ data: editedQuestion });
});

Expand All @@ -71,10 +108,10 @@ QuestionController.prototype.editQuestion = function(request, reply) {


// [GET] /tasks/{id}
QuestionController.prototype.all = function(request, reply) {
QuestionController.prototype.all = function (request, reply) {
try {

this.questionModel.showAllQuestions(function(data) {
this.questionModel.showAllQuestions(function (data) {
reply(data);
});

Expand All @@ -83,25 +120,25 @@ QuestionController.prototype.all = function(request, reply) {
}
};

QuestionController.prototype.userQuestions = function(request, reply) {
QuestionController.prototype.userQuestions = function (request, reply) {

this.questionModel.userQuestions(request.params.app, request.query.user_id, request.query.limit,function(data) {
reply({
data: {
"count": data.length,
"rows": data
}
});
this.questionModel.userQuestions(request.params.app, request.query.user_id, request.query.limit, function (data) {
reply({
data: {
"count": data.length,
"rows": data
}
});
});

};

QuestionController.prototype.fetch = function(request, reply) {
QuestionController.prototype.fetch = function (request, reply) {
try {

var uId = (request.query.user_id || request.query.user_id.length > 0) ? request.query.user_id : null;

this.questionModel.fetchQuestions(request.params.app, request.query.limit, uId, request.headers['x-voter-installation'],request.query.debug, function(data) {
this.questionModel.fetchQuestions(request.params.app, request.query.limit, uId, request.headers['x-voter-installation'], request.query.debug, function (data) {
reply({
data: {
"count": data.length,
Expand All @@ -115,16 +152,16 @@ QuestionController.prototype.fetch = function(request, reply) {
}
};

QuestionController.prototype.getOne = function(request, reply) {
QuestionController.prototype.getOne = function (request, reply) {
try {

var uId = request.query.user_id;
var qId = request.params.id;
var app = request.query.app;

this.questionModel.getQuestion(uId, qId, app, function(data) {
this.questionModel.getQuestion(uId, qId, app, function (data) {
reply({
data: {
data: {
"rows": data
}
});
Expand All @@ -135,10 +172,10 @@ QuestionController.prototype.getOne = function(request, reply) {
}
};

QuestionController.prototype.delete = function(request, reply) {
QuestionController.prototype.delete = function (request, reply) {
try {

this.questionModel.deleteQuestion(request.params.id, function(data) {
this.questionModel.deleteQuestion(request.params.id, function (data) {
reply({
data: null
});
Expand All @@ -152,7 +189,7 @@ QuestionController.prototype.delete = function(request, reply) {



QuestionController.prototype.upload = function(request, reply) {
QuestionController.prototype.upload = function (request, reply) {
try {

cloudinary.config(config_params["cloudinary"]);
Expand All @@ -168,15 +205,15 @@ QuestionController.prototype.upload = function(request, reply) {
var path = __dirname + "/../uploads/" + name;
var file = fs.createWriteStream(path);

file.on('error', function(err) {
file.on('error', function (err) {
console.error(err);
});

data.file.pipe(file);

data.file.on('end', function(err) {
data.file.on('end', function (err) {

cloudinary.uploader.upload(path, function(result) {
cloudinary.uploader.upload(path, function (result) {
fs.unlink(path, (err) => {
if (err) throw err;
reply({ data: result.url });
Expand Down
1 change: 1 addition & 0 deletions controllers/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ UserController.prototype.signup = function(request, reply) {
data.imei = request.headers['x-voter-installation'];
data.app = parseInt(request.headers['x-voter-client-id']);
data.last_fb_token = request.payload.token;
data.reg_id=request.payload.reg_id==null?'':request.payload.reg_id;

for (var i = 0; i < res.friends.data.length; i++) {

Expand Down
18 changes: 10 additions & 8 deletions db_schemas/question.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

var Sequelize = require('sequelize');

module.exports = function(sequelize, DataTypes) {
module.exports = function (sequelize, DataTypes) {
var question = sequelize.define("question", {
id: {
type: Sequelize.UUID,
Expand All @@ -22,15 +22,17 @@ module.exports = function(sequelize, DataTypes) {
updated_at: { type: DataTypes.DATE, defaultValue: DataTypes.NOW },
is_deleted: { type: DataTypes.BOOLEAN, defaultValue: false },
abuse_count: { type: DataTypes.INTEGER, defaultValue: 0 },
favorite_count: { type: DataTypes.INTEGER, defaultValue: 0 }
favorite_count: { type: DataTypes.INTEGER, defaultValue: 0 },
is_private: { type: DataTypes.BOOLEAN, defaultValue: false },
private_url: DataTypes.STRING,

}, {
tableName: 'question',
schema: 'public',
freezeTableName: true,
timestamps: false,
hasTrigger: true
});
tableName: 'question',
schema: 'public',
freezeTableName: true,
timestamps: false,
hasTrigger: true
});

return question;
};
3 changes: 2 additions & 1 deletion db_schemas/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ module.exports = function(sequelize, DataTypes) {
is_deleted: { type: DataTypes.BOOLEAN, defaultValue: false },
friends: { type: DataTypes.ARRAY(DataTypes.STRING), defaultValue: null },
app: DataTypes.INTEGER,
last_fb_token: DataTypes.STRING
last_fb_token: DataTypes.STRING,
reg_id: DataTypes.STRING,


}, {
Expand Down
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ var server = new Hapi.Server({ debug: { request: ['info', 'error'], log: ['info'


server.connection({
host: '0.0.0.0',
host: '0.0.0.0',

// host: 'localhost',
//host: 'localhost',
port: process.env.PORT || 8000
});

Expand Down
Loading

0 comments on commit 858aad8

Please sign in to comment.