Built motion from commit 5e31ea4.|0.0.32
[motion.git] / server / api / square_odbc / square_odbc.controller.js
1 'use strict';
2
3 var _ = require('lodash');
4 var SquareOdbc = require('../../models').SquareOdbc;
5 var util = require('util');
6
7 // Get list of square_odbcs
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   SquareOdbc
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 exports.odbcValidation = function(req, res) {
73   console.log(req.body);
74   SquareOdbc
75     .findAll({
76       where: {
77         name: req.body.name
78       }
79     })
80     .then(function(square_odbcs) {
81       if (!square_odbcs) {
82         return res.sendStatus(404);
83       }
84       return res.send(square_odbcs);
85     })
86     .catch(function(err) {
87       return handleError(res, err);
88     });
89 };
90
91 // Get a single square_odbc
92 exports.show = function(req, res) {
93   SquareOdbc
94     .findById(req.params.id)
95     .then(function(square_odbc) {
96       if (!square_odbc) {
97         return res.sendStatus(404);
98       }
99       return res.send(square_odbc);
100     })
101     .catch(function(err) {
102       return handleError(res, err);
103     });
104 };
105
106 // Creates a new square_odbc in the DB.
107 exports.create = function(req, res) {
108   SquareOdbc
109     .create(req.body)
110     .then(function(square_odbc) {
111       return res.status(201).send(square_odbc);
112     })
113     .catch(function(err) {
114       return handleError(res, err);
115     });
116 };
117
118 // Updates an existing square_odbc in the DB.
119 exports.update = function(req, res) {
120   SquareOdbc
121     .findAll({
122       where: {
123         name: req.body.name,
124         id: {
125           $ne: req.body.id
126         }
127       }
128     })
129     .then(function(square_odbcs) {
130       if (!square_odbcs) {
131         return res.sendStatus(404);
132       }
133       if (square_odbcs.length > 0) {
134         return res.status(500).send({
135           message: 'MESSAGE_EXIST_ODBC'
136         })
137       }
138       if (req.body.id) {
139         delete req.body.id;
140       }
141       SquareOdbc
142         .findById(req.params.id)
143         .then(function(square_odbc) {
144           if (!square_odbc) {
145             return res.sendStatus(404);
146           }
147           var updated = _.merge(square_odbc, req.body);
148           updated.save()
149             .then(function() {
150               return res.status(200).send(square_odbc);
151             })
152             .catch(function(err) {
153               return handleError(res, err);
154             });
155         })
156         .catch(function(err) {
157           return handleError(res, err);
158         });
159     })
160     .catch(function(err) {
161       return handleError(res, err);
162     });
163 };
164
165 // Deletes a square_odbc from the DB.
166 exports.destroy = function(req, res) {
167   SquareOdbc
168     .find({
169       where: {
170         id: req.params.id
171       }
172     })
173     .then(function(square_odbc) {
174       if (!square_odbc) {
175         return res.sendStatus(404);
176       }
177       square_odbc.destroy()
178         .then(function() {
179           return res.sendStatus(204);
180         })
181         .catch(function(err) {
182           return handleError(res, err);
183         });
184     })
185     .catch(function(err) {
186       return handleError(res, err);
187     });
188 };
189
190 // Deletes a square_project from the DB.
191 exports.bulkDestroy = function(req, res) {
192   SquareOdbc
193     .destroy({
194       where: {
195         id: req.query.id
196       },
197       individualHooks: true
198     })
199     .then(function() {
200       return res.sendStatus(204);
201     })
202     .catch(function(err) {
203       return handleError(res, err);
204     });
205 };
206
207 function handleError(res, err) {
208   return res.status(500).send(err);
209 }