/**
 * Very simple implementation of Hapi hanlders that do REST operations on
 * Mongoose entitites.
 *
 * At some point, I should make a npm module of this.
 */

var Boom = require('boom');

function create(Model) {
    return function (request, reply) {
        var model = new Model(request.payload);
        return model.save(function(err) {
            if (err) {
                return reply(Boom.expectationFailed('Error persisting entity', err));
            }
            return reply(model);
        });
    }
}

function read(Model) {
    return function (request, reply) {
        var params = _.merge(request.query, request.params);
        return Model.findOne(params).exec(function (err, model) {
            if (err) {
                return reply(Boom.expectationFailed('Error reading entity', err));
            }
            if (!model) {
                return reply(Boom.notFound('Error reading entity', err));
            }
            return reply(model);
        });
    }
}

function update(Model) {
    var reader = read(Model);
    return function (request, reply) {
        return reader(request, function (model) {
            if (model.isBoom) { // handles error reading
                return eply(model)
            }

            model.set(request.payload);
            return model.save(function(err) {
                if (err) {
                    return reply(Boom.expectationFailed('Error persisting entity changes', err));
                }
                return reply(model);
            });
        });
    }
}

function destroy(Model) {
    var reader = read(Model);
    return function (request, reply) {
        return reader(request, function (model) {
            if (model.isBoom) { // handles error reading
                return reply(model)
            }

            return model.remove(function(err) {
                if (err) {
                    return reply(Boom.expectationFailed('Error destroying entity', err));
                }

                return reply(model);
            });
        });
    }
}

function list(Model) {
    return function (request, reply) {
        return Model.find({}, function (err, models) {
            if (err) {
                return reply(Boom.expectationFailed('Error reading entities', err));
            }

            return reply(models);
        });
    }
}

module.exports = function (options) {
    return {
        create: create(options.model),
        read: read(options.model),
        update: update(options.model),
        destroy: destroy(options.model),
        delete: destroy(options.model),
        list: list(options.model)
    };
};

@@ -504,33 +504,34 @@
        %7D);%0A    %7D
+;
 %0A
-
 %7D%0A%0Afunction read
@@ -982,32 +982,33 @@
        %7D);%0A    %7D
+;
 %0A%7D%0A%0Afunction upd
@@ -1219,16 +1219,17 @@
  return 
+r
 eply(mod
@@ -1223,32 +1223,33 @@
 urn reply(model)
+;
 %0A            %7D%0A%0A
@@ -1539,32 +1539,33 @@
        %7D);%0A    %7D
+;
 %0A%7D%0A%0Afunction des
@@ -1785,24 +1785,25 @@
 reply(model)
+;
 %0A           
@@ -2056,24 +2056,25 @@
    %7D);%0A    %7D
+;
 %0A%7D%0A%0Afunction
@@ -2351,21 +2351,22 @@
     %7D);%0A
-
     %7D
+;
 %0A%7D%0A%0Amodu

Fix minor things