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