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
11 var xml = require('xml2js');
13 var _ = require('lodash');
15 var JscriptyProject = require('../../models').JscriptyProject;
18 function handleError(res, statusCode) {
19 statusCode = statusCode || 500;
20 return function(err) {
21 res.status(statusCode).send(err);
25 function responseWithResult(res, statusCode) {
26 statusCode = statusCode || 200;
27 return function(entity) {
29 res.status(statusCode).json(entity);
34 function handleEntityNotFound(res) {
35 return function(entity) {
37 res.status(404).end();
44 function saveUpdates(updates) {
45 return function(entity) {
46 return entity.updateAttributes(updates)
47 .then(function(updated) {
53 function removeEntity(res) {
54 return function(entity) {
56 return entity.destroy()
58 res.status(204).end();
64 // Gets a list of JscriptyProjects
66 exports.index = function(req, res) {
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;
75 offset: page * per_page
78 _.forIn(req.query, function(value, key) {
84 query.order = util.format('%s %s', req.query.sort_by, req.query.sort_order || 'ASC') || null;
90 attributes.forEach(function(attribute) {
93 $like: '%' + value + '%'
96 query.where.$or.push(tmp);
103 query.where[key].$like = '%' + value + '%';
108 .findAndCountAll(query)
109 .then(function(result) {
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;
115 res.status(200).send({
118 next_page: next_page,
119 previous_page: previous_page,
120 total_pages: total_pages
124 .catch(function(err) {
125 res.status(500).send({
126 error: 'Something blew up!'
130 exports.projectValidation = function(req, res) {
131 console.log(req.body);
138 .then(function(jscripty_projects) {
139 if (!jscripty_projects) {
140 return res.sendStatus(404);
142 return res.send(jscripty_projects);
144 .catch(function(err) {
145 return handleError(res, err);
148 // Gets a single JscriptyProject from the DB
150 exports.show = function(req, res) {
152 .findById(req.params.id)
153 .then(function(jscripty_project) {
154 if (!jscripty_project) {
155 return res.sendStatus(404);
157 return res.send(jscripty_project);
159 .catch(function(err) {
160 return handleError(res, err);
163 // Creates a new JscriptyProject in the DB
164 exports.create = function(req, res) {
167 .then(function(jscripty_project) {
168 return res.status(201).send(jscripty_project);
170 .catch(function(err) {
171 return handleError(res, err);
174 // Updates an existing JscriptyProject in the DB
175 exports.update = function(req, res) {
180 .findById(req.params.id)
181 .then(function(jscripty_project) {
182 if (!jscripty_project) {
183 return res.sendStatus(404);
185 var updated = _.merge(jscripty_project, req.body);
188 return res.status(200).send(jscripty_project);
190 .catch(function(err) {
191 return handleError(res, err);
194 .catch(function(err) {
195 return handleError(res, err);
200 // Deletes a JscriptyProject from the DB
201 exports.destroy = function(req, res) {
208 .then(function(jscripty_project) {
209 if (!jscripty_project) {
210 return res.sendStatus(404);
212 jscripty_project.destroy()
214 return res.sendStatus(204);
216 .catch(function(err) {
217 return handleError(res, err);
220 .catch(function(err) {
221 return handleError(res, err);
225 // Deletes a jscripty_project from the DB.
226 exports.bulkDestroy = function(req, res) {
232 individualHooks: true
235 return res.sendStatus(204);
237 .catch(function(err) {
238 return handleError(res, err);
241 exports.download = function(req, res, next) {
243 .findById(req.params.id)
244 .then(function(jscripty_project) {
245 if (!jscripty_project) {
246 return res.sendStatus(404);
248 console.log(req.query.filename);
250 "Content-Disposition": "attachment; filename=\"" + req.query.filename +
252 "Content-Type": "txt/xml"
254 return res.send(jscripty_project.draft);
256 .catch(function(err) {
257 return handleError(res, err);
261 exports.projectJson = function(req, res, next) {
263 .findById(req.params.id)
264 .then(function(jscripty_project) {
265 if (!jscripty_project) {
266 return res.sendStatus(404);
269 xml.parseString(jscripty_project.production, {
272 }, function(err, result) {
274 return res.status(200).send(result);
278 .catch(function(err) {
279 return handleError(res, err);