Skip to content

Instantly share code, notes, and snippets.

@aray12
Last active October 7, 2015 17:47
Show Gist options
  • Save aray12/12806831a92beb0b04bf to your computer and use it in GitHub Desktop.
Save aray12/12806831a92beb0b04bf to your computer and use it in GitHub Desktop.
'use strict';
var _ = require('lodash'),
Promise = require('bluebird'),
models = require('../database'),
pipeline = require('../utils/pipeline'),
utils = require('./utils'),
errors = require('../errors');
var listing = {
/**
* Edit a listing
*
* @param object The body of the HTTP request
* @param options Any options passed including the resourceID
*
* @returns {*}
*/
edit: function edit(object, options) {
var tasks,
findResource,
handlePermissions,
updateResource;
findResource = function findResource(options) {
return models.listing.find({listing_id: options.id})
.then(function(listing) {
if (listing) {
options.resource = listing;
}
return Promise.reject(new errors.NotFoundError);
});
};
handlePermissions = function handlePermissions(options) {
if (options.user.account_id !== options.resource.account_id) {
Promise.reject(new errors.NoPermissionError());
}
return options;
};
updateResource = function updateResource(options) {
return options.resource.updateAttributes(options.data);
};
tasks = [
utils.validate(),
findResource,
handlePermissions,
updateResource
];
return pipeline(tasks, object, options);
},
/**
* Edit a listing
*
* @param object The body of the HTTP request
* @param options Any options passed including the resourceID
*
* @returns {*}
*/
editExtend: function edit(object, options) {
var tasks,
findResource,
handlePermissions,
updateResource;
findResource = function findResource(options) {
return models.listing.find({listing_id: options.id})
.then(function(listing) {
if (listing) {
return _.extend(options, {resource: listing});
}
return Promise.reject(new errors.NotFoundError);
});
};
handlePermissions = function handlePermissions(options) {
if (options.user.account_id !== options.resource.account_id) {
Promise.reject(new errors.NoPermissionError());
}
return options;
};
updateResource = function updateResource(options) {
return options.resource.updateAttributes(options.data);
};
tasks = [
utils.validate(),
findResource,
handlePermissions,
updateResource
];
return pipeline(tasks, object, options);
},
/**
* Edit a listing
*
* @param object The body of the HTTP request
* @param options Any options passed including the resourceID
*
* @returns {*}
*/
editExtendNewObj: function edit(object, options) {
var tasks,
findResource,
handlePermissions,
updateResource;
findResource = function findResource(options) {
return models.listing.find({listing_id: options.id})
.then(function(listing) {
if (listing) {
return _.extend({}, options, {resource: listing});
}
return Promise.reject(new errors.NotFoundError);
});
};
handlePermissions = function handlePermissions(options) {
if (options.user.account_id !== options.resource.account_id) {
Promise.reject(new errors.NoPermissionError());
}
return options;
};
updateResource = function updateResource(options) {
return options.resource.updateAttributes(options.data);
};
tasks = [
utils.validate(),
findResource,
handlePermissions,
updateResource
];
return pipeline(tasks, object, options);
}
};
module.exports = listing;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment