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