3 var _ = require('lodash');
4 var util = require('util');
6 var Automation = require('../../models').Automation;
9 exports.index = function(req, res, next) {
11 var attributes = ['fullname', 'name', 'email'];
12 var per_page = req.query.per_page ? parseInt(req.query.per_page, 10) : 100;
13 var page = req.query.page ? parseInt(req.query.page, 10) : 0;
18 offset: page * per_page
21 _.forIn(req.query, function(value, key) {
27 query.order = util.format('%s %s', req.query.sort_by, req.query.sort_order || 'ASC') || null;
33 attributes.forEach(function(attribute) {
36 $like: '%' + value + '%'
39 query.where.$or.push(tmp);
46 query.where[key].$like = '%' + value + '%';
51 .findAndCountAll(query)
52 .then(function(result) {
54 var total_pages = Math.ceil(result.count / per_page);
55 var next_page = total_pages > (query.offset + 1) ? util.format('%s://%s%s?page=%d', req.protocol, req.headers.host, req.baseUrl, page + 1) : null;
56 var previous_page = page > 0 ? util.format('%s://%s%s?page=%d', req.protocol, req.headers.host, req.baseUrl, page - 1) : null;
58 res.status(200).send({
62 previous_page: previous_page,
63 total_pages: total_pages
67 .catch(function(err) {
68 res.status(500).send({
69 error: 'Something blew up!'
74 // Get a single Automation
75 exports.show = function(req, res) {
77 .findById(req.params.id)
78 .then(function(Automation) {
80 return res.sendStatus(404);
82 return res.send(Automation);
84 .catch(function(err) {
85 return handleError(res, err);
89 // Creates a new Automation in the DB.
90 exports.create = function(req, res, next) {
94 return res.sendStatus(201);
96 .catch(function(err) {
97 return handleError(res, err);
101 // Updates an existing Automation in the DB.
102 exports.update = function(req, res, next) {
107 .findById(req.params.id)
108 .then(function(Automation) {
110 return res.sendStatus(404);
112 var updated = _.merge(Automation, req.body);
116 return res.status(200).send(Automation);
118 .catch(function(err) {
119 // return handleError(res, err);
124 .catch(function(err) {
129 // Deletes a Automation from the DB.
130 exports.destroy = function(req, res) {
132 .findById(req.params.id)
133 .then(function(Automation) {
135 return res.sendStatus(404);
139 return res.sendStatus(204);
141 .catch(function(err) {
142 return handleError(res, err);
145 .catch(function(err) {
146 return handleError(res, err);
150 exports.bulkDestroy = function(req, res) {
156 individualHooks: true
159 return res.sendStatus(204);
161 .catch(function(err) {
162 return handleError(res, err);
166 function handleError(res, err) {
167 return res.status(500).send(err);