var _ = require('underscore'),
    express = require('express'),
    router = express.Router();

/**
 * Create user with parameter
 * Param: req, res, user and callback function
 */
exports.create = function(req, res, user, cb, Entity) {
    var persistingUser = new Entity(user);
    persistingUser.save(function(err) {
        cb(req, res, persistingUser, err);
    });
};

/**
 * Find users with parameter
 * Param: req, res, searchOptions and callback function
 */
exports.find = function(req, res, searchOptions, cb, Entity) {
    var promise = Entity.find(searchOptions).exec();

    promise.then(function(users){
        console.log("find "+  JSON.stringify(searchOptions) + users);
        cb(req, res, users);
    });
};

/**
 * Update user with parameter
 * Param: req, res, id, updatingUser and callback function
 */
exports.update = function(req, res, id, updatingUser, cb, Entity) {
    Entity.findById(id, function(err, user) {
        if (!user)
            return res.json({"message" : "could not find document with id " + id});
        else {
            console.log("updating " + JSON.stringify(updatingUser) + " to " + JSON.stringify(user));
            user = _.extend(user, updatingUser);
            user.save(function(err) {
                cb(req, res, user, err);
            });
        }
    });
};

/**
 * Delete user by id as parameter
 * Param: req, res, id and callback function
 */
exports.delete = function(req, res, id, cb, Entity) {
    Entity.findById(id, function(err, user) {
        if (!user)
            return res.json({"message" : "could not find document with id " + id});
        else {
            user.remove(function(err) {
                cb(req, res, user, err);
            });
        }
    });
};
@@ -100,28 +100,30 @@
 *%0A * Create 
-user
+entity
  with parame
@@ -146,20 +146,22 @@
 q, res, 
-user
+entity
  and cal
@@ -215,20 +215,22 @@
 q, res, 
-user
+entity
 , cb, En
@@ -255,20 +255,22 @@
 rsisting
-User
+Entity
  = new E
@@ -275,20 +275,22 @@
  Entity(
-user
+entity
 );%0A    p
@@ -298,20 +298,22 @@
 rsisting
-User
+Entity
 .save(fu
@@ -357,20 +357,22 @@
 rsisting
-User
+Entity
 , err);%0A
@@ -395,20 +395,23 @@
  * Find 
-user
+entitie
 s with p
@@ -622,20 +622,23 @@
 unction(
-user
+entitie
 s)%7B%0A    
@@ -695,20 +695,23 @@
 ions) + 
-user
+entitie
 s);%0A    
@@ -727,20 +727,23 @@
 q, res, 
-user
+entitie
 s);%0A    
@@ -764,20 +764,22 @@
  Update 
-user
+entity
  with pa
@@ -818,20 +818,22 @@
 updating
-User
+Entity
  and cal
@@ -899,20 +899,22 @@
 updating
-User
+Entity
 , cb, En
@@ -951,36 +951,38 @@
 , function(err, 
-user
+entity
 ) %7B%0A        if (
@@ -974,36 +974,38 @@
  %7B%0A        if (!
-user
+entity
 )%0A            re
@@ -1150,20 +1150,22 @@
 updating
-User
+Entity
 ) + %22 to
@@ -1184,20 +1184,22 @@
 ringify(
-user
+entity
 ));%0A    
@@ -1206,20 +1206,22 @@
         
-user
+entity
  = _.ext
@@ -1224,20 +1224,22 @@
 .extend(
-user
+entity
 , updati
@@ -1244,12 +1244,14 @@
 ting
-User
+Entity
 );%0A 
@@ -1261,20 +1261,22 @@
         
-user
+entity
 .save(fu
@@ -1310,36 +1310,38 @@
    cb(req, res, 
-user
+entity
 , err);%0A        
@@ -1384,20 +1384,22 @@
  Delete 
-user
+entity
  by id a
@@ -1551,20 +1551,22 @@
 on(err, 
-user
+entity
 ) %7B%0A    
@@ -1574,20 +1574,22 @@
    if (!
-user
+entity
 )%0A      
@@ -1693,20 +1693,22 @@
         
-user
+entity
 .remove(
@@ -1756,12 +1756,14 @@
 es, 
-user
+entity
 , er

refactor names