<commit_before>"use strict";

var log = require("../../../shared/logger.js").get("server"),
    models = require("../../models.js"),
    config = require("../../../shared/config");

/**
 * derive the modelPath from given requestPath
 * @param {!String} reqPath
 * @return {String}
 */
function getModelPath(reqPath) {

    //if we want to use a path
    //var modelPath = reqPath.substr(config.paths.services);
    //return modelPath;
    var splits = reqPath.split("/");
    return splits[splits.length-1];
}

/**
 * returns a model-class or modelInstance if ID was set
 * returns req.getData() if not model was found
 * use forceInstance to get an instance even when ID is not set (null)
 * @param {!Object} req
 * @param {!String} modelPath
 * @param {String} id
 * @param {Boolean} forceInstance
 * @return {Object}
 */
function getModel(req, modelPath, id, forceInstance) {

    //loadModel
    var ModelClass = models.getModel(modelPath);

    //if we can not find a model, we attach the object
    if(ModelClass === null) {
        return req.getData();
    }

    //attach the instance
    if(id === undefined) {
        if(forceInstance !== undefined && forceInstance === true) {
            return new ModelClass();
        }
        return ModelClass;
    }

    return new ModelClass(id);
}

/**
 * load Model-middleware
 * append the model to the request-object
 * @param {!Object} req
 * @param {!Object} res
 * @param {!Function} next
 */
function loadModel(req, res, next) {

    var modelPath = getModelPath(req.getPath()),
        ids = req.getIds(),
        id = null,
        method = req.getMethod();

    //assign ID for modelName
    if(ids[modelPath] !== undefined) {
        id = ids[modelPath];
    }

    if(method === "create") {
        if(id === null) {
            log.debug("Creating new Instance of " + modelPath);
            //force new instance
            req.setModel(getModel(req, modelPath, null, true));
        }
        else {
            next(new Error("(alamid) Cannot load model for path '" + req.getRawPath() + "': 'create' does not accept IDs"));
            return;
        }
    }
    else if(method === "read") {
        if(id === null) {
            //if we have no ID, we probably want to return a collection
            log.debug("Returning the class of " + modelPath + " to be used for collection retrieval");
            req.setModel(getModel(req, modelPath));
        }
        else {
            log.debug("Creating Instance of " + modelPath + " with ID " + id);
            req.setModel(getModel(req, modelPath, id));
        }
    }
    else if(method === "update" || method === "delete") {
        if(id === null) {
            next(new Error("(alamid) Cannot load model for path '" + req.getRawPath() + "', '" + method + "' : Missing IDs"));
            return;
        }
        //return Model-Instance for given id
        log.debug("Creating Instance of " + modelPath + " with ID " + id);
        req.setModel(getModel(req, modelPath, id));
    }
    else {
        next(new Error("(alamid) Unsupported method:  " + method));
    }

    next();
}

module.exports = loadModel;<commit_msg>- small note for the next working day :)<commit_after>"use strict";

var log = require("../../../shared/logger.js").get("server"),
    models = require("../../models.js"),
    config = require("../../../shared/config");

/**
 * derive the modelPath from given requestPath
 * @param {!String} reqPath
 * @return {String}
 */
function getModelPath(reqPath) {

    //if we want to use a path
    //var modelPath = reqPath.substr(config.paths.services);
    //return modelPath;
    var splits = reqPath.split("/");
    return splits[splits.length-1];
}

/**
 * returns a model-class or modelInstance if ID was set
 * returns req.getData() if not model was found
 * use forceInstance to get an instance even when ID is not set (null)
 * @param {!Object} req
 * @param {!String} modelPath
 * @param {String} id
 * @param {Boolean} forceInstance
 * @return {Object}
 */
function getModel(req, modelPath, id, forceInstance) {

    //loadModel
    var ModelClass = models.getModel(modelPath);

    //if we can not find a model, we attach the object
    if(ModelClass === null) {
        //this could also be done directly in the request-class
        //maybe nice than my solution here..
        return req.getData();
    }

    //attach the instance
    if(id === undefined) {
        if(forceInstance !== undefined && forceInstance === true) {
            return new ModelClass();
        }
        return ModelClass;
    }

    return new ModelClass(id);
}

/**
 * load Model-middleware
 * append the model to the request-object
 * @param {!Object} req
 * @param {!Object} res
 * @param {!Function} next
 */
function loadModel(req, res, next) {

    var modelPath = getModelPath(req.getPath()),
        ids = req.getIds(),
        id = null,
        method = req.getMethod();

    //assign ID for modelName
    if(ids[modelPath] !== undefined) {
        id = ids[modelPath];
    }

    if(method === "create") {
        if(id === null) {
            log.debug("Creating new Instance of " + modelPath);
            //force new instance
            req.setModel(getModel(req, modelPath, null, true));
        }
        else {
            next(new Error("(alamid) Cannot load model for path '" + req.getRawPath() + "': 'create' does not accept IDs"));
            return;
        }
    }
    else if(method === "read") {
        if(id === null) {
            //if we have no ID, we probably want to return a collection
            log.debug("Returning the class of " + modelPath + " to be used for collection retrieval");
            req.setModel(getModel(req, modelPath));
        }
        else {
            log.debug("Creating Instance of " + modelPath + " with ID " + id);
            req.setModel(getModel(req, modelPath, id));
        }
    }
    else if(method === "update" || method === "delete") {
        if(id === null) {
            next(new Error("(alamid) Cannot load model for path '" + req.getRawPath() + "', '" + method + "' : Missing IDs"));
            return;
        }
        //return Model-Instance for given id
        log.debug("Creating Instance of " + modelPath + " with ID " + id);
        req.setModel(getModel(req, modelPath, id));
    }
    else {
        next(new Error("(alamid) Unsupported method:  " + method));
    }

    next();
}

module.exports = loadModel;
{'fraction_non_alphanumeric': 0.14596273291925466, 'fraction_numerical': 0.00031055900621118014, 'mean_word_length': 2.2963152507676563, 'pattern_counts': {'":': 0, '<': 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': '4543457', 'n_tokens_mistral': 1897, 'n_tokens_neox': 1858, 'n_words': 578}