3 var _ = require('lodash');
4 var util = require('util');
5 var Dashboard = require('../../models').Dashboard;
7 // Get list of dashboards
8 exports.index = function(req, res) {
9 var attributes = ['name', 'description'];
10 var per_page = req.query.per_page ? parseInt(req.query.per_page, 10) : 100;
11 var page = req.query.page ? parseInt(req.query.page, 10) : 0;
16 offset: page * per_page
19 _.forIn(req.query, function(value, key) {
25 query.order = util.format('%s %s', req.query.sort_by, req.query.sort_order || 'ASC') || null;
31 attributes.forEach(function(attribute) {
34 $like: '%' + value + '%'
37 query.where.$or.push(tmp);
44 query.where[key].$like = '%' + value + '%';
49 .findAndCountAll(query)
50 .then(function(result) {
52 var total_pages = Math.ceil(result.count / per_page);
53 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;
54 var previous_page = page > 0 ? util.format('%s://%s%s?page=%d', req.protocol, req.headers.host, req.baseUrl, page - 1) : null;
56 res.status(200).send({
60 previous_page: previous_page,
61 total_pages: total_pages
65 .catch(function(err) {
66 res.status(500).send({
67 error: 'Something blew up!'
72 // Get a single dashboard
73 exports.all = function(req, res) {
76 .then(function(dashboards) {
78 return res.sendStatus(404);
80 return res.status(200).send(dashboards);
82 .catch(function(err) {
83 return handleError(res, err);
87 // Get a single dashboard
88 exports.show = function(req, res) {
90 .findById(req.params.id)
91 .then(function(dashboard) {
93 return res.sendStatus(404);
95 return res.send(dashboard);
97 .catch(function(err) {
98 return handleError(res, err);
102 // Creates a new dashboard in the DB.
103 exports.create = function(req, res) {
106 .then(function(dashboard) {
107 return res.status(201).send(dashboard);
109 .catch(function(err) {
110 return handleError(res, err);
114 // Updates an existing dashboard in the DB.
115 exports.update = function(req, res) {
120 .findById(req.params.id)
121 .then(function(dashboard) {
123 return res.sendStatus(404);
125 var updated = _.merge(dashboard, req.body);
128 return res.status(200).send(dashboard);
130 .catch(function(err) {
131 return handleError(res, err);
134 .catch(function(err) {
135 return handleError(res, err);
139 // Deletes a dashboard from the DB.
140 exports.destroy = function(req, res) {
142 .findById(req.params.id)
143 .then(function(dashboard) {
145 return res.sendStatus(404);
149 return res.sendStatus(204);
151 .catch(function(err) {
152 return handleError(res, err);
155 .catch(function(err) {
156 return handleError(res, err);
160 exports.bulkDestroy = function(req, res) {
166 individualHooks: true
169 return res.sendStatus(204);
171 .catch(function(err) {
172 return handleError(res, err);
176 function handleError(res, err) {
177 return res.status(500).send(err);