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