<commit_before>module.exports.addResource = function(options) {

    var app = options.app;
    var prefix = options.urlPrefix || "/api";
    var resourceName = options.resourceName;
    var collection = options.collection;
    var schema = options.collection.schema;
    var paths = Object.keys(schema.paths);

    if (prefix[0] != '/') prefix = '/' + prefix;


    var baseUrl = prefix + '/' + resourceName;


    /**
     * schema: Show eschema for colletion
     */
    app.get(baseUrl + '/schema', function(req, res) {
        res.json(schema);
    });


    /**
     * GET: List all model instances.
     */
    app.get(baseUrl, function(req, res) {
        var cb = function(error, resources) {
            if (!error) res.json(resources); // Write the jsonified resources to the response object
            else res.json(error);
        };

        resources = collection.find(cb);
    });


    /**
     * GET [:ID]: Show model instance.
     */
    app.get(baseUrl + '/:id', function(req, res) {
        collection.find({
            "_id": req.params.id
        })
        // .populate('_location _type')
        .exec(function(error, resource) {
            if (!error) {
                if (resource.length) {
                    res.json(resource[0]);
                } else {
                    res.json({
                        message: resourceName + 'Not Found'
                    });
                }
            } else {
                res.json(error);
            }
            });
    });


    /**
     * POST: Create model.
     */
    app.post(baseUrl, function(req, res) {

        var data = req.body;
        postHash = {};
        paths.forEach(function(key) {
            if (!data[key] || key === '_id') return;
            postHash[key] = data[key];
        });

        collection.create(postHash, function(error, resource) {
            if (!error) res.json(resource);
            else res.json(error);
        });
    });


    /**
     * PUT: Update model.
     */
    app.put(baseUrl + '/:id', function(req, res) {
        var data = req.body;

        var _id = data._id;
        delete data._id;

        collection.update({_id: _id}, data, function(error, resource) {
            console.log(error ? error : resource);
            if (error) return res.send(error);
            else res.json(resource);
        });
    });

    /**
     * DELETE: Remove model.
     */
    app.delete(baseUrl + '/:id', function(req, res) {<commit_msg>LIB: restful helper, use router<commit_after>var express = require('express');

module.exports.addResource = function(options) {

    var app = options.app;
    var prefix = options.urlPrefix || "/api";
    var resourceSlug = options.resourceSlug;
    var collection = options.collection;
    var schema = options.collection.schema;
    var paths = Object.keys(schema.paths);

    if (prefix[0] != '/') prefix = '/' + prefix;


    var baseUrl = '/' + resourceSlug;

    var router = express.Router();
    app.use(prefix, router);

    /**
     * schema: Show eschema for colletion
     */
    router.get(baseUrl + '/schema', function(req, res) {
        res.json(schema);
    });


    /**
     * GET: List all model instances.
     */
    router.get(baseUrl, function(req, res) {
        var cb = function(error, resources) {
            if (!error) res.json(resources); // Write the jsonified resources to the response object
            else res.json(error);
        };

        resources = collection.find(cb);
    });


    /**
     * GET [:ID]: Show model instance.
     */
    router.get(baseUrl + '/:id', function(req, res) {
        collection.find({
            "_id": req.params.id
        })
        // .populate('_location _type')
        .exec(function(error, resource) {
            if (error) res.json(error);
            else {
                if (resource.length) res.json(resource[0]);
                else {
                    res.json({
                        message: resourceSlug + 'Not Found'
                    });
                }
            }
        });
    });


    /**
     * POST: Create model.
     */
    router.post(baseUrl, function(req, res) {

        var data = req.body;
        postHash = {};
        paths.forEach(function(key) {
            if (!data[key] || key === '_id') return;
            postHash[key] = data[key];
        });

        collection.create(postHash, function(error, resource) {
            if (!error) res.json(resource);
            else res.json(error);
        });
    });


    /**
     * PUT: Update model.
     */
    router.put(baseUrl + '/:id', function(req, res) {
        var data = req.body;

        var _id = data._id;
        delete data._id;

        collection.update({_id: _id}, data, function(error, resource) {
            console.log(error ? error : resource);
            if (error) return res.send(error);
            else res.json(resource);
        });
    });

    /**
     * DELETE: Remove model.
     */
    router.delete(baseUrl + '/:id', function(req, res) {
{'fraction_non_alphanumeric': 0.1477656405163853, 'fraction_numerical': 0.0007944389275074479, 'mean_word_length': 1.6505263157894736, 'pattern_counts': {'":': 2, '<': 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': '6366468', 'n_tokens_mistral': 1488, 'n_tokens_neox': 1431, 'n_words': 325}