<commit_before>module.exports = function(collectionName){

    var db = require('./db.js');
    var async = require('async');
    var ObjectID = require('mongodb').ObjectID;

    var getObjectCollection = function() {
        return db.getDb().collection(collectionName);
    };

    var insertObjects = function(objects, userId, callback) {

        if (objects instanceof Array) {
            objects.forEach(function(object) {
                object.user = userId;
            });
        } else {
            objects.user = userId;
        }

        getObjectCollection().insert(objects, callback);
    };

    var updateObject = function(objectId, object, userId, callback) {
        object.user = userId;
        getObjectCollection().update({_id: objectId}, { $set: object }, { upsert : true}, callback);
    }

    var isObjectOwnedByUser = function(objectId, userId, callback) {
        var cursor = getObjectCollection().find({_id: objectId, user:userId});
        cursor.count(function(err, count) {
            callback(err, count > 0);
        });
    }

    var findObjectsByUser = function(user, callback) {
        var cursor = getObjectCollection().find({user: user});
        cursor.toArray(function(err, docs) {
            callback(err, docs);
        });
    };

    var findObjectsById = function(id, callback) {
        var cursor = getObjectCollection().find({_id: id});
        cursor.nextObject(function(err, object) {
            callback(err, object);
        });
    };

    var deleteObjectsById = function(id, callback) {
        getObjectCollection().remove({_id: id}, function(err, count) {
            callback(err, count);
        });
    };

    var deleteObjectsByUser = function(user, callback) {
        getObjectCollection().remove({user: user}, function(err, count) {
            callback(err, count);
        });
    };

    return {
        use: function (req, res, next) {
            if (req.isAuthenticated()) {
                next();
            } else {
                res.json(401, {message: 'not logged in'});
            }
        },
        get: function (req, res) {
            // respond with entire collection associated with user

            findObjectsByUser(req.user._id, function(err, objects) {
                if (err) {
                    res.json(500, "error: database error");
                } else {
                    res.json(objects);
                }
            });
        },
        put: function (req, res) {
            // replace entire collection of objects associated with user
            async.series([
                function(callback) {
                    deleteObjectsByUser(req.user._id, callback);
                }, function(callback) {
                    insertObjects(req.body, req.user._id, callback);
                }], function(err) {
                if (err) {
                    res.json(500, {message: "could not store objects"})
                } else {
                    res.json({});
                }
            });


        },
        post: function (req, res) {
            // create a new object and associated it with user
            insertObjects(req.body, req.user._id, function(err, object) {
                if (err) {
                    res.json(500, {message: "could not store object"});
                } else {
                    res.json(200, object);
                }
            });
        },
        delete : function (req, res) {
            // delete entire collection associated with user
            deleteObjectsByUser(req.user._id, function(err, objects) {
                if (err) {
                    res.json(500, "error: database error");
                } else {
                    res.json({});
                }
            });
        },
        ":id": {
            get: function (req, res) {
                findObjectsById(req.params.id, function(err, object) {
                    if (err) {
                        res.json(500, {message: "could not get object"});
                    } else {
                        if (object == null) {
                            res.json(404, { message: "object not found"});
                        } else {
                            if (req.user._id.equals(object.user)) {
                                res.json(object);
                            } else {
                                res.json(401, { message: "permission denied"});
                            }
                        }
                    }
                });
            },
            put: function (req, res) {
                // Replace " + req.params.id + ", or if it doesn't exist, create it.
                findObjectsById(req.params.id, function(err, object) {
                    if (err) {
                        res.json(500, {message: "could not get object"});
                    } else {
                        if (object === null || req.user._id.equals(object.user)) {
                            updateObject(req.params.id, req.body, req.user._id, function(err) {
                                if (err) {
                                    res.json(500, {message: "could not update object"});
                                } else {
                                    res.json({});
                                }
                            });
                        } else {
                            res.json(401, { message: "permission denied"});
                        }
                    }
                });

            },
            delete: {
                handler: function (req, res) {
                    // Delete " + req.params.id
                    findObjectsById(req.params.id, function(err, object) {
                        if (err) {
                            res.json(500, { message: "could not get object"} );
                        } else {
                            if (object === null) {
                                res.json(404, { message: "object not found"});
                            } else if (req.user._id.equals(object.user)) {
                                deleteObjectsById(object._id, function(err) {
                                    if (err) {
                                        res.json(500, { message: "could not delete object"} );
                                    } else {
                                        res.json({});
                                    }
                                });
                            } else {
                                res.json(401, { message: "permission denied"});
                            }
                        }
                    });
                }
            }<commit_msg>refactoring remove deleteObjectsById and deleteObjectsByUser functions add default query limit of 10<commit_after>var DEFAULT_LIMIT = 10;

module.exports = function(collectionName){

    var db = require('./db.js');
    var async = require('async');
    var ObjectID = require('mongodb').ObjectID;

    var getObjectCollection = function() {
        return db.getDb().collection(collectionName);
    };

    var insertObjects = function(objects, userId, callback) {

        if (objects instanceof Array) {
            objects.forEach(function(object) {
                object.user = userId;
            });
        } else {
            objects.user = userId;
        }

        getObjectCollection().insert(objects, callback);
    };

    var updateObject = function(objectId, object, userId, callback) {
        object.user = userId;
        getObjectCollection().update({_id: objectId}, { $set: object }, { upsert : true}, callback);
    }

    var isObjectOwnedByUser = function(objectId, userId, callback) {
        var cursor = getObjectCollection().find({_id: objectId, user:userId});
        cursor.count(function(err, count) {
            callback(err, count > 0);
        });
    }

    var findObjects = function(query, callback) {
        var cursor = getObjectCollection().find(query, { limit: DEFAULT_LIMIT});
        cursor.toArray(function(err, docs) {
            callback(err, docs);
        });
    };

    var getObjectById = function(id, callback) {
        var cursor = getObjectCollection().find({_id: id});
        cursor.nextObject(function(err, object) {
            callback(err, object);
        });
    };

    var deleteObjects = function(query, callback) {
        getObjectCollection().remove(query, function(err, count) {
            callback(err, count);
        });
    };

    return {
        use: function (req, res, next) {
            if (req.isAuthenticated()) {
                next();
            } else {
                res.json(401, {message: 'not logged in'});
            }
        },
        get: function (req, res) {
            // respond with entire collection associated with user

            var query = { user: req.user._id };
            findObjects(query, function(err, objects) {
                if (err) {
                    res.json(500, "error: database error");
                } else {
                    res.json(objects);
                }
            });
        },
        put: function (req, res) {
            // replace entire collection of objects associated with user
            async.series([
                function(callback) {
                    var query = {user: req.user._id};
                    deleteObjects(query, callback);
                }, function(callback) {
                    insertObjects(req.body, req.user._id, callback);
                }], function(err) {
                if (err) {
                    res.json(500, {message: "could not store objects"})
                } else {
                    res.json({});
                }
            });


        },
        post: function (req, res) {
            // create a new object and associated it with user
            insertObjects(req.body, req.user._id, function(err, object) {
                if (err) {
                    res.json(500, {message: "could not store object"});
                } else {
                    res.json(200, object);
                }
            });
        },
        delete : function (req, res) {
            // delete entire collection associated with user
            var query = {user: req.user._id};
            deleteObjects(query, function(err, objects) {
                if (err) {
                    res.json(500, "error: database error");
                } else {
                    res.json({});
                }
            });
        },
        ":id": {
            get: function (req, res) {
                getObjectById(req.params.id, function(err, object) {
                    if (err) {
                        res.json(500, {message: "could not get object"});
                    } else {
                        if (object == null) {
                            res.json(404, { message: "object not found"});
                        } else {
                            if (req.user._id.equals(object.user)) {
                                res.json(object);
                            } else {
                                res.json(401, { message: "permission denied"});
                            }
                        }
                    }
                });
            },
            put: function (req, res) {
                // Replace " + req.params.id + ", or if it doesn't exist, create it.
                getObjectById(req.params.id, function(err, object) {
                    if (err) {
                        res.json(500, {message: "could not get object"});
                    } else {
                        if (object === null || req.user._id.equals(object.user)) {
                            updateObject(req.params.id, req.body, req.user._id, function(err) {
                                if (err) {
                                    res.json(500, {message: "could not update object"});
                                } else {
                                    res.json({});
                                }
                            });
                        } else {
                            res.json(401, { message: "permission denied"});
                        }
                    }
                });

            },
            delete: {
                handler: function (req, res) {
                    // Delete " + req.params.id
                    getObjectById(req.params.id, function(err, object) {
                        if (err) {
                            res.json(500, { message: "could not get object"} );
                        } else {
                            if (object === null) {
                                res.json(404, { message: "object not found"});
                            } else if (req.user._id.equals(object.user)) {
                                var query = {_id: object._id};
                                deleteObjects(query, function(err) {
                                    if (err) {
                                        res.json(500, { message: "could not delete object"} );
                                    } else {
                                        res.json({});
                                    }
                                });
                            } else {
                                res.json(401, { message: "permission denied"});
                            }
                        }
                    });
                }
            }
{'fraction_non_alphanumeric': 0.11395211350872007, 'fraction_numerical': 0.007537688442211055, 'mean_word_length': 1.0613861386138614, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 19, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2932810', 'n_tokens_mistral': 3359, 'n_tokens_neox': 3102, 'n_words': 726}