Built motion from commit 06df96e on branch develop.
[motion.git] / server / api / dashboard / dashboard.controller.js
1 'use strict';
2
3 var _ = require('lodash');
4 var util = require('util');
5 var Dashboard = require('../../models').Dashboard;
6
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;
12
13   var query = {
14     where: {},
15     limit: per_page,
16     offset: page * per_page
17   };
18
19   _.forIn(req.query, function(value, key) {
20     switch (key) {
21       case 'per_page':
22       case 'page':
23         break;
24       case 'sort_by':
25         query.order = util.format('%s %s', req.query.sort_by, req.query.sort_order || 'ASC') || null;
26         break;
27       case 'sort_order':
28         break;
29       case '$':
30         query.where.$or = [];
31         attributes.forEach(function(attribute) {
32           var tmp = {};
33           tmp[attribute] = {
34             $like: '%' + value + '%'
35           };
36
37           query.where.$or.push(tmp);
38         });
39         break;
40       default:
41         query.where[key] = {
42           $like: {}
43         };
44         query.where[key].$like = '%' + value + '%';
45     }
46   });
47
48   Dashboard
49     .findAndCountAll(query)
50     .then(function(result) {
51
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;
55
56       res.status(200).send({
57         count: result.count,
58         rows: result.rows,
59         next_page: next_page,
60         previous_page: previous_page,
61         total_pages: total_pages
62       });
63
64     })
65     .catch(function(err) {
66       res.status(500).send({
67         error: 'Something blew up!'
68       });
69     });
70 };
71
72 // Get a single dashboard
73 exports.all = function(req, res) {
74   Dashboard
75     .findAll()
76     .then(function(dashboards) {
77       if (!dashboards) {
78         return res.sendStatus(404);
79       }
80       return res.status(200).send(dashboards);
81     })
82     .catch(function(err) {
83       return handleError(res, err);
84     });
85 };
86
87 // Get a single dashboard
88 exports.show = function(req, res) {
89   Dashboard
90     .findById(req.params.id)
91     .then(function(dashboard) {
92       if (!dashboard) {
93         return res.sendStatus(404);
94       }
95       return res.send(dashboard);
96     })
97     .catch(function(err) {
98       return handleError(res, err);
99     });
100 };
101
102 // Creates a new dashboard in the DB.
103 exports.create = function(req, res) {
104   Dashboard
105     .create(req.body)
106     .then(function(dashboard) {
107       return res.status(201).send(dashboard);
108     })
109     .catch(function(err) {
110       return handleError(res, err);
111     });
112 };
113
114 // Updates an existing dashboard in the DB.
115 exports.update = function(req, res) {
116   if (req.body.id) {
117     delete req.body.id;
118   }
119   Dashboard
120     .findById(req.params.id)
121     .then(function(dashboard) {
122       if (!dashboard) {
123         return res.sendStatus(404);
124       }
125       var updated = _.merge(dashboard, req.body);
126       updated.save()
127         .then(function() {
128           return res.status(200).send(dashboard);
129         })
130         .catch(function(err) {
131           return handleError(res, err);
132         });
133     })
134     .catch(function(err) {
135       return handleError(res, err);
136     });
137 };
138
139 // Deletes a dashboard from the DB.
140 exports.destroy = function(req, res) {
141   Dashboard
142     .findById(req.params.id)
143     .then(function(dashboard) {
144       if (!dashboard) {
145         return res.sendStatus(404);
146       }
147       dashboard.destroy()
148         .then(function() {
149           return res.sendStatus(204);
150         })
151         .catch(function(err) {
152           return handleError(res, err);
153         });
154     })
155     .catch(function(err) {
156       return handleError(res, err);
157     });
158 };
159
160 exports.bulkDestroy = function(req, res) {
161   Dashboard
162     .destroy({
163       where: {
164         id: req.query.id
165       },
166       individualHooks: true
167     })
168     .then(function() {
169       return res.sendStatus(204);
170     })
171     .catch(function(err) {
172       return handleError(res, err);
173     });
174 };
175
176 function handleError(res, err) {
177   return res.status(500).send(err);
178 }