Download this file

controllerV0.js    147 lines (135 with data), 4.2 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*made by Miguel Rodrigues @ KBZ miguel.rodrigues@knowledgebiz.pt*/
//controllerV0 - First Version
var database = require('./mysql');
var controllerNotification = require('../backend/notificationManager');
module.exports.registerVapp= function (req, res) {
if(Object.keys(req.body).length != 0){
database.insertVapp(req.body, function(isOK, data){
if(!isOK) {
sendResponse(500, {success:false, data:data},"Register Vapp: "+data, res);
}else {
sendResponse(200, {success: true, data:data},"Register Vapp: Success", res);
}
});
}else{
res.end();
}
};
module.exports.createApp= function (req, res) {
if(Object.keys(req.body).length != 0){
database.insertVappByDeveloperid(req.body, function(isOK, data){
if(!isOK) {
sendResponse(500, {success:false, data:data},"Create Vapp: "+data, res);
}else {
sendResponse(200, {success: true, data:data},"Create Vapp: Success", res);
}
});
}else{
res.end();
}
};
module.exports.createNotifications= function (req, res) {
if(Object.keys(req.body).length != 0){
database.checkIfTokenExists(req.body, function(isOK, reason){
if(isOK) {
controllerNotification.notificationsCheck(req.body, function(isOK, reason){
if(isOK) {
database.insertNotification(req.body, function(isOK, data){
if(!isOK) {
sendResponse(500, {success:false, data:data},"Insert Notification: "+data, res);
}else {
sendResponse(200, {success: true, data:data},"Insert Notification: Success", res);
}
});
}else {
sendResponse(500, {success: false, reason: reason},"Notifications Check: "+reason, res);
}
});
}else {
sendResponse(500, {success: false, reason: reason},"Check Token: "+reason, res);
}
});
}else{
res.end();
}
};
module.exports.createNotificationsRules= function (req, res) {
if(Object.keys(req.body).length != 0){
database.insertNotificationRules(req.body, function(isOK, data){
if(!isOK) {
sendResponse(500, {success:false, data:data},"Create Rules: "+data, res);
}else {
sendResponse(200, {success: true, data:data},"Create Rules: Success", res);
}
});
}else{
res.end();
}
};
module.exports.getNotifications= function (req, res) {
appid = req.params.appid;
database.retrieveNotificationList(appid, function(isOK, rows){
if(!isOK) {
sendResponse(500, {success:false, data:rows},"Get Notifications: "+rows, res);
}else {
sendResponse(200, {success: true, data:rows},"Get Notifications: Success",res);
}
});
};
module.exports.getApps= function (req, res) {
developerid = req.params.developerid;
database.retrieveApp(developerid, function(isOK, rows){
if(!isOK) {
sendResponse(500, {success:false, data:rows},"Get Vapps: "+rows,res);
}else {
sendResponse(200, {success: true, data:rows},"Get Vapps: Success",res);
}
});
};
/*output:
{
success: true/false,
data: <>
}*/
module.exports.getRules= function (req, res) {
appid = req.params.appid;
database.retrieveRulesList(appid, function(isOK, rows){
if(!isOK) {
sendResponse(500, {success:false, data:rows},"Get Rules: "+rows,res);
}else {
sendResponse(200, {success: true, data:rows},"Get Rules: Success",res);
}
});
};
module.exports.deleteVapp= function (req, res) {
appid = req.params.appid;
database.deleteVapp(appid, function(isOK, data){
if(!isOK) {
sendResponse(500, {success:false, data:data},"Delete Vapp: "+data,res);
}else {
sendResponse(200, {success: true, data:data},"Delete Vapp: Success",res);
}
});
};
module.exports.deleteRule= function (req, res) {
rulesid = req.params.rulesid;
database.deleteRule(rulesid, function(isOK, data){
if(!isOK) {
sendResponse(500, {success:false, data:data},"Delete Rule: "+data,res);
}else {
sendResponse(200, {success: true, data:data},"Delete Rule: Success",res);
}
});
};
/*
INPUT:
statusCode: number 200/400/500 etc
JSONbody: the content of response
*/
function sendResponse(statusCode, JSONbody, type, res){
//console.log("Sending response to " + type);
console.log(type);
res.writeHead(statusCode, {'Content-type': 'application/json'});
res.write(JSON.stringify(JSONbody));
res.end();
}