Built motion from commit 5e31ea4.|0.0.32
[motion.git] / server / api / jscripty_project / jscripty_project.controller.js
1 /**
2  * Using Rails-like standard naming convention for endpoints.
3  * GET     /api/jscripty/projects              ->  index
4  * POST    /api/jscripty/projects              ->  create
5  * GET     /api/jscripty/projects/:id          ->  show
6  * PUT     /api/jscripty/projects/:id          ->  update
7  * DELETE  /api/jscripty/projects/:id          ->  destroy
8  */
9
10 'use strict';
11 var xml = require('xml2js');
12
13 var _ = require('lodash');
14
15 var JscriptyProject = require('../../models').JscriptyProject;
16
17
18 function handleError(res, statusCode) {
19   statusCode = statusCode || 500;
20   return function(err) {
21     res.status(statusCode).send(err);
22   };
23 }
24
25 function responseWithResult(res, statusCode) {
26   statusCode = statusCode || 200;
27   return function(entity) {
28     if (entity) {
29       res.status(statusCode).json(entity);
30     }
31   };
32 }
33
34 function handleEntityNotFound(res) {
35   return function(entity) {
36     if (!entity) {
37       res.status(404).end();
38       return null;
39     }
40     return entity;
41   };
42 }
43
44 function saveUpdates(updates) {
45   return function(entity) {
46     return entity.updateAttributes(updates)
47       .then(function(updated) {
48         return updated;
49       });
50   };
51 }
52
53 function removeEntity(res) {
54   return function(entity) {
55     if (entity) {
56       return entity.destroy()
57         .then(function() {
58           res.status(204).end();
59         });
60     }
61   };
62 }
63
64 // Gets a list of JscriptyProjects
65
66 exports.index = function(req, res) {
67
68   var attributes = ['name', 'description'];
69   var per_page = req.query.per_page ? parseInt(req.query.per_page, 10) : 100;
70   var page = req.query.page ? parseInt(req.query.page, 10) : 0;
71
72   var query = {
73     where: {},
74     limit: per_page,
75     offset: page * per_page
76   };
77
78   _.forIn(req.query, function(value, key) {
79     switch (key) {
80       case 'per_page':
81       case 'page':
82         break;
83       case 'sort_by':
84         query.order = util.format('%s %s', req.query.sort_by, req.query.sort_order || 'ASC') || null;
85         break;
86       case 'sort_order':
87         break;
88       case '$':
89         query.where.$or = [];
90         attributes.forEach(function(attribute) {
91           var tmp = {};
92           tmp[attribute] = {
93             $like: '%' + value + '%'
94           };
95
96           query.where.$or.push(tmp);
97         });
98         break;
99       default:
100         query.where[key] = {
101           $like: {}
102         };
103         query.where[key].$like = '%' + value + '%';
104     }
105   });
106
107   JscriptyProject
108     .findAndCountAll(query)
109     .then(function(result) {
110
111       var total_pages = Math.ceil(result.count / per_page);
112       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;
113       var previous_page = page > 0 ? util.format('%s://%s%s?page=%d', req.protocol, req.headers.host, req.baseUrl, page - 1) : null;
114
115       res.status(200).send({
116         count: result.count,
117         rows: result.rows,
118         next_page: next_page,
119         previous_page: previous_page,
120         total_pages: total_pages
121       });
122
123     })
124     .catch(function(err) {
125       res.status(500).send({
126         error: 'Something blew up!'
127       });
128     });
129 };
130 exports.projectValidation = function(req, res) {
131   console.log(req.body);
132   JscriptyProject
133     .findAll({
134       where: {
135         name: req.body.name
136       }
137     })
138     .then(function(jscripty_projects) {
139       if (!jscripty_projects) {
140         return res.sendStatus(404);
141       }
142       return res.send(jscripty_projects);
143     })
144     .catch(function(err) {
145       return handleError(res, err);
146     });
147 };
148 // Gets a single JscriptyProject from the DB
149
150 exports.show = function(req, res) {
151   JscriptyProject
152     .findById(req.params.id)
153     .then(function(jscripty_project) {
154       if (!jscripty_project) {
155         return res.sendStatus(404);
156       }
157       return res.send(jscripty_project);
158     })
159     .catch(function(err) {
160       return handleError(res, err);
161     });
162 };
163 // Creates a new JscriptyProject in the DB
164 exports.create = function(req, res) {
165   JscriptyProject
166     .create(req.body)
167     .then(function(jscripty_project) {
168       return res.status(201).send(jscripty_project);
169     })
170     .catch(function(err) {
171       return handleError(res, err);
172     });
173 };
174 // Updates an existing JscriptyProject in the DB
175 exports.update = function(req, res) {
176   if (req.body.id) {
177     delete req.body.id;
178   }
179   JscriptyProject
180     .findById(req.params.id)
181     .then(function(jscripty_project) {
182       if (!jscripty_project) {
183         return res.sendStatus(404);
184       }
185       var updated = _.merge(jscripty_project, req.body);
186       updated.save()
187         .then(function() {
188           return res.status(200).send(jscripty_project);
189         })
190         .catch(function(err) {
191           return handleError(res, err);
192         });
193     })
194     .catch(function(err) {
195       return handleError(res, err);
196     });
197 };
198
199
200 // Deletes a JscriptyProject from the DB
201 exports.destroy = function(req, res) {
202   JscriptyProject
203     .find({
204       where: {
205         id: req.params.id
206       }
207     })
208     .then(function(jscripty_project) {
209       if (!jscripty_project) {
210         return res.sendStatus(404);
211       }
212       jscripty_project.destroy()
213         .then(function() {
214           return res.sendStatus(204);
215         })
216         .catch(function(err) {
217           return handleError(res, err);
218         });
219     })
220     .catch(function(err) {
221       return handleError(res, err);
222     });
223 };
224
225 // Deletes a jscripty_project from the DB.
226 exports.bulkDestroy = function(req, res) {
227   JscriptyProject
228     .destroy({
229       where: {
230         id: req.query.id
231       },
232       individualHooks: true
233     })
234     .then(function() {
235       return res.sendStatus(204);
236     })
237     .catch(function(err) {
238       return handleError(res, err);
239     });
240 };
241 exports.download = function(req, res, next) {
242   JscriptyProject
243     .findById(req.params.id)
244     .then(function(jscripty_project) {
245       if (!jscripty_project) {
246         return res.sendStatus(404);
247       }
248       console.log(req.query.filename);
249       res.set({
250         "Content-Disposition": "attachment; filename=\"" + req.query.filename +
251           ".xml\"",
252         "Content-Type": "txt/xml"
253       });
254       return res.send(jscripty_project.draft);
255     })
256     .catch(function(err) {
257       return handleError(res, err);
258     });
259 };
260
261 exports.projectJson = function(req, res, next) {
262   JscriptyProject
263     .findById(req.params.id)
264     .then(function(jscripty_project) {
265       if (!jscripty_project) {
266         return res.sendStatus(404);
267       }
268
269       xml.parseString(jscripty_project.production, {
270         normalizeTags: true,
271         explicitArray: false
272       }, function(err, result) {
273         console.log(result);
274         return res.status(200).send(result);
275
276       });
277     })
278     .catch(function(err) {
279       return handleError(res, err);
280     });
281 };