Switch to side-by-side view

--- a/controllers/mysql.js
+++ b/controllers/mysql.js
@@ -151,7 +151,7 @@
 							}else{
 								var sqlQuery2 = "INSERT INTO ruleslist ( description, parameter, conditionValue, controlValue, threshold, notifyType, emailTo, notificationType, hostname, port, path, method, token) VALUES ('"+eachBody.description+"','"+eachBody.parameter+"','"+eachBody.conditionValue+"','"+eachBody.controlValue+"','"+eachBody.threshold+"','"+eachBody.notifyType+"','"+destinations+"','"+eachBody.notificationType+"','"+eachBody.hostname+"','"+eachBody.port+"','"+eachBody.path+"','"+eachBody.method+"','"+res[0].token+"')";
 								handlerDB(sqlQuery2, function(err, result){
-									if (err){ 
+									if (err){
 										callback(false,err);
 									}else {
 										response.push({description:eachBody.description, reason:"Success"});
@@ -582,30 +582,95 @@
 	data: JSON Body
 Output: true/false with message
 */
-module.exports.updateRule= function(data, callback){
-	var destinations ="";
-	
-	for(i = 0; i < data.emailTo.length; i++){
-		if(data.emailTo[i] == null && data.emailTo.length == 1){
-			destinations = null;
-		}else{
-			if(data.emailTo[i] != null){
-				destinations += data.emailTo[i];
-				if(i < data.emailTo.length - 1 && data.emailTo.length != 1){
-					destinations += ", "
+module.exports.updateRule= function(body, callback){
+	var response = [];
+	if(body.hasOwnProperty('token')){
+		var sqlQuery = "UPDATE ruleslist SET description = '"+body.description+"', parameter = '"+body.parameter+"', conditionValue = '"+body.conditionValue+"', controlValue = '"+body.controlValue+"', threshold = '"+body.threshold+"', notifyType = '"+body.notifyType+"', emailTo = '"+body.emailTo+"', notificationType = '"+body.notificationType+"', hostname = '"+body.hostname+"', port = '"+body.port+"', path = '"+body.path+"', method = '"+body.method+"'where rulesID = '"+body.rulesid+"'";
+		handlerDB(sqlQuery, function(err, result){
+			if (err){
+				callback(false,err);
+			}else {
+				callback(true);
+			}
+		});
+	}else{
+		console.log("body: " + JSON.stringify(body));
+		body.forEach(function(eachBody, bodyIndex){
+			console.log("eachBody: " + eachBody);
+			var destinations ="";
+			for(i = 0; i < eachBody.emailTo.length; i++){
+				if(eachBody.emailTo[i] == null && eachBody.emailTo.length == 1){
+					destinations = null;
+				}else{
+					if(eachBody.emailTo[i] != null){
+						destinations += eachBody.emailTo[i];
+						if(i < eachBody.emailTo.length - 1 && eachBody.emailTo.length != 1){
+							destinations += ","
+						}
+					}
 				}
 			}
-		}
+			var sql = "SELECT * FROM ruleslist where rulesID = '"+eachBody.rulesid+"'";
+			handlerDB(sql, function(err, res){
+				if (err){
+					callback(false,err);
+				}else{
+					if(res.length == 0){
+						response.push({description:eachBody.description, reason: "rulesID incorrect"});
+						if(bodyIndex == body.length - 1){
+							callback(true, response);
+						}
+					}else{
+						switch(eachBody.conditionValue){
+							case ">":
+							case ">=":
+							if(eachBody.threshold > 100){
+								response.push({description:eachBody.description, reason:"Fail! Threshold have to be Lower then 100"});
+								if(bodyIndex == body.length - 1){
+									callback(true, response);
+								}
+							}else{
+								var sqlQuery2 = "UPDATE ruleslist SET description = '"+eachBody.description+"', parameter = '"+eachBody.parameter+"', conditionValue = '"+eachBody.conditionValue+"', controlValue = '"+eachBody.controlValue+"', threshold = '"+eachBody.threshold+"', notifyType = '"+eachBody.notifyType+"', emailTo = '"+eachBody.emailTo+"', notificationType = '"+eachBody.notificationType+"', hostname = '"+eachBody.hostname+"', port = '"+eachBody.port+"', path = '"+eachBody.path+"', method = '"+eachBody.method+"'where rulesID = '"+eachBody.rulesid+"'";
+								handlerDB(sqlQuery2, function(err, result){
+									if (err){ 
+										callback(false,err);
+									}else {
+										response.push({description:eachBody.description, reason:"Success"});
+										if(bodyIndex == body.length - 1){
+											callback(true, response);
+										}
+									}
+								});
+							}
+							break;
+							case "<":
+							case "<=":
+							if(body.data.threshold < 100){
+								response.push({description:eachBody.description, reason:"Fail! Threshold have to be Higher then 100"});
+								if(bodyIndex == body.length - 1){
+									callback(true, response);
+								}
+							}else{
+								var sqlQuery2 = "UPDATE ruleslist SET description = '"+eachBody.description+"', parameter = '"+eachBody.parameter+"', conditionValue = '"+eachBody.conditionValue+"', controlValue = '"+eachBody.controlValue+"', threshold = '"+eachBody.threshold+"', notifyType = '"+eachBody.notifyType+"', emailTo = '"+eachBody.emailTo+"', notificationType = '"+eachBody.notificationType+"', hostname = '"+eachBody.hostname+"', port = '"+eachBody.port+"', path = '"+eachBody.path+"', method = '"+eachBody.method+"'where rulesID = '"+eachBody.rulesid+"'";
+								handlerDB(sqlQuery2, function(err, result){
+									if (err){ 
+										callback(false,err);
+									}else {
+										response.push({description:eachBody.description, reason:"Success"});
+										if(bodyIndex == body.length - 1){
+											callback(true, response);
+										}
+									}
+								});
+							}
+							break;
+							default:
+						}
+					}
+				}
+			})
+		})
 	}
-	var sqlQuery = "UPDATE ruleslist SET description = '"+data.description+"', parameter = '"+data.parameter+"', conditionValue = '"+data.conditionValue+"', controlValue = '"+data.controlValue+"', threshold = '"+data.threshold+"', notifyType = '"+data.notifyType+"', emailTo = '"+destinations+"', notificationType = '"+data.notificationType+"', hostname = '"+data.hostname+"', port = '"+data.port+"', path = '"+data.path+"', method = '"+data.method+"'where rulesID = '"+data.rulesid+"'";
-	handlerDB(sqlQuery, function(err, result){
-		if (err){
-			callback(false,err);
-		}
-		else{
-			callback(true, result);
-		}
-	})
 }
 
 /*