{"size": 2516, "ext": "js", "max_stars_repo_path": "NoDBRest.js", "max_stars_repo_name": "inf3cti0n95/no-db-rest", "max_stars_repo_stars_event_min_datetime": "2017-06-03T08:22:48.000Z", "max_stars_repo_stars_event_max_datetime": "2017-12-04T04:13:24.000Z", "max_issues_repo_path": "NoDBRest.js", "max_issues_repo_name": "inf3cti0n95/no-db-rest", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "NoDBRest.js", "max_forks_repo_name": "inf3cti0n95/no-db-rest", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 2, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 37.552238806, "max_line_length": 103, "alphanum_fraction": 0.5890302067}
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var NoDBFileOps_1 = require("./NoDBFileOps");
function NoDBRest(filepath) {
    return function (request, response, next) {
        var fileOps = new NoDBFileOps_1.default(filepath);
        switch (request.method) {
            case "GET":
                getRequest(fileOps, request, response);
                break;
            case "POST":
                postRequest(fileOps, request, response);
                break;
            case "DELETE":
                deleteRequest(fileOps, request, response);
                break;
            case "PUT":
                putRequest(fileOps, request, response);
                break;
        }
        next();
    };
}
exports.NoDBRest = NoDBRest;
function getRequest(fileOps, request, response) {
    response.send(JSON.stringify(fileOps.get(request.query)));
    console.log("Data Fetched from " + fileOps.filepath + " and served response");
}
function postRequest(fileOps, request, response) {
    var resp = fileOps.put(request.query);
    if (resp)
        console.log("Data inserted to " + fileOps.filepath + " and served response");
    else
        console.log("Data not inserted to " + fileOps.filepath);
    response.send(JSON.stringify(resp));
}
function putRequest(fileOps, request, response) {
    var query = request.query.search;
    var searchParams = query.split(",");
    var queryObject = new Object();
    searchParams.forEach(function (p) {
        var key = p.split("=")[0];
        var val = p.split("=")[1];
        if (/^\d+$/.test(val) || val === "true" || val === "false")
            val = eval(val);
        queryObject[key] = val;
    });
    var update = request.query.update;
    var updateParams = update.split(",");
    var updateObject = new Object();
    updateParams.forEach(function (p) {
        var key = p.split("=")[0];
        var val = p.split("=")[1];
        if (/^\d+$/.test(val) || val === "true" || val === "false")
            val = eval(val);
        updateObject[key] = val;
    });
    var resp = fileOps.update(queryObject, updateObject);
    response.send(JSON.stringify(resp));
    console.log("Data updated at " + resp + " places in " + fileOps.filepath + " and served response");
}
function deleteRequest(fileOps, request, response) {
    var resp = fileOps.delete(request.query);
    response.send(JSON.stringify(resp));
    console.log("Data deleted at " + resp + " places in " + fileOps.filepath + " and served response");
}
