<commit_before>var mysql = require("mysql");

function REST_ROUTER(router,connection,md5) {
    var self = this;
    self.handleRoutes(router,connection,md5);
}

// Send a query to the database
function SendQuery(connection,query){
    connection.query(query, function(err,rows){
        if(err){
            res.json({"Status" : "Error"});
            throw err;
        } else {
            res.json({"Status" : "Succes", "Data" : rows});
        }
    });
}

REST_ROUTER.prototype.handleRoutes= function(router,connection,md5) {
    router.get("/",function(req,res){
        res.json({"Message" : "Hello, welcome to our domotica API"});
    });

    router.get("/sensortype/:type", function(req, res){
        // Create a SQL query using mysql.escape for safety reasons.
        var query = `
            SELECT * FROM COMBINED_MEASUREMENTS
            WHERE SENSOR_TYPE_NAME = `+
            mysql.escape(req.params.type);

            connection.query(query, function(err,rows){
                if(err){
                    res.json({"Status" : "Error"});
                    throw err;
                } else {
                    res.json({"Status" : "Succes", "Data" : rows});
                }
            });
    });

    router.get("/sensor/:id", function(req, res){
        // Create a SQL query using mysql.escape for safety reasons.
        var query = `
            SELECT * FROM SENSOR_MEASUREMENTS
            WHERE SENSOR_ID = `+
            mysql.escape(req.params.id);

            connection.query(query, function(err,rows){
                if(err){
                    res.json({"Status" : "Error"});
                    throw err;
                } else {
                    res.json({"Status" : "Succes", "Data" : rows});
                }
            });
    });
    router.post("/sensor/:id", function(req, res){
        // Create a SQL query using mysql.escape for safety reasons.
        var query = `
            INSERT INTO MEASUREMENT (SENSOR_ID, VALUE) VALUES(`+
            mysql.escape(req.params.id) + `,`
            +mysql.escape(req.body.value) + `);`;

            connection.query(query, function(err,rows){
                if(err){
                    res.json({"Status" : "Error", "error":err});
                    throw err;
                } else {
                    res.json({"Status" : "Succes", "Data" : rows});
                }
            });
    });

    router.get("/device/:id", function(req, res){
        // Create a SQL query using mysql.escape for safety reasons.
        var query = `
            SELECT * FROM DEVICE_SENSORS
            WHERE DEVICE_ID = `+
            mysql.escape(req.params.id);

            connection.query(query, function(err,rows){
                if(err){
                    res.json({"Status" : "Error"});
                    throw err;
                } else {
                    res.json({"Status" : "Succes", "Data" : rows});
                }
            });
    });
}

module.exports = REST_ROUTER;<commit_msg>Added check for valid json (but still getting errors<commit_after>var mysql = require("mysql");

function REST_ROUTER(router,connection,md5) {
    var self = this;
    self.handleRoutes(router,connection,md5);
}

// Send a query to the database
function SendQuery(connection,query){
    connection.query(query, function(err,rows){
        if(err){
            res.json({"Status" : "Error"});
            throw err;
        } else {
            res.json({"Status" : "Succes", "Data" : rows});
        }
    });
}

function IsJsonString(str) {
    try {
        JSON.parse(str);
    } catch (e) {
        return false;
    }
    return true;
}


REST_ROUTER.prototype.handleRoutes= function(router,connection,md5) {
    router.get("/",function(req,res){
        res.json({"Message" : "Hello, welcome to our domotica API"});
    });

    router.get("/sensortype/:type", function(req, res){
        // Create a SQL query using mysql.escape for safety reasons.
        var query = `
            SELECT * FROM COMBINED_MEASUREMENTS
            WHERE SENSOR_TYPE_NAME = `+
            mysql.escape(req.params.type);

            connection.query(query, function(err,rows){
                if(err){
                    res.json({"Status" : "Error"});
                    throw err;
                } else {
                    res.json({"Status" : "Succes", "Data" : rows});
                }
            });
    });

    router.get("/sensor/:id", function(req, res){
        // Create a SQL query using mysql.escape for safety reasons.
        var query = `
            SELECT * FROM SENSOR_MEASUREMENTS
            WHERE SENSOR_ID = `+
            mysql.escape(req.params.id);

            connection.query(query, function(err,rows){
                if(err){
                    res.json({"Status" : "Error"});
                    throw err;
                } else {
                    res.json({"Status" : "Succes", "Data" : rows});
                }
            });
    });
    router.post("/sensor/:id", function(req, res){
        // Create a SQL query using mysql.escape for safety reasons.
        if(!IsJsonString(req.body)){
            res.json({"Status" : "Error", "error":"No valid json!"});
        }else{
          var query = `
              INSERT INTO MEASUREMENT (SENSOR_ID, VALUE) VALUES(`+
              mysql.escape(req.params.id) + `,`
              +mysql.escape(req.body.value) + `);`;

              connection.query(query, function(err,rows){
                  if(err){
                      res.json({"Status" : "Error", "error":err});
                      throw err;
                  } else {
                      res.json({"Status" : "Succes", "Data" : rows});
                  }
              });
          }
    });

    router.get("/device/:id", function(req, res){
        // Create a SQL query using mysql.escape for safety reasons.
        var query = `
            SELECT * FROM DEVICE_SENSORS
            WHERE DEVICE_ID = `+
            mysql.escape(req.params.id);

            connection.query(query, function(err,rows){
                if(err){
                    res.json({"Status" : "Error"});
                    throw err;
                } else {
                    res.json({"Status" : "Succes", "Data" : rows});
                }
            });
    });
}

module.exports = REST_ROUTER;
{'fraction_non_alphanumeric': 0.1417981072555205, 'fraction_numerical': 0.0009463722397476341, 'mean_word_length': 1.5353858456617353, 'pattern_counts': {'":': 3, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '335520', 'n_tokens_mistral': 1855, 'n_tokens_neox': 1762, 'n_words': 368}