3 var _ = require('lodash');
4 var path = require('path');
5 var config = require('../../config/environment');
6 var util = require('util');
8 var SquareProject = require('../../models').SquareProject;
10 // Get list of square_projects
11 exports.index = function(req, res) {
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;
20 offset: page * per_page
23 _.forIn(req.query, function(value, key) {
29 query.order = util.format('%s %s', req.query.sort_by, req.query.sort_order || 'ASC') || null;
35 attributes.forEach(function(attribute) {
38 $like: '%' + value + '%'
41 query.where.$or.push(tmp);
48 query.where[key].$like = '%' + value + '%';
53 .findAndCountAll(query)
54 .then(function(result) {
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;
60 res.status(200).send({
64 previous_page: previous_page,
65 total_pages: total_pages
69 .catch(function(err) {
70 res.status(500).send({
71 error: 'Something blew up!'
76 exports.projectValidation = function(req, res) {
77 console.log(req.body);
84 .then(function(square_projects) {
85 if (!square_projects) {
86 return res.sendStatus(404);
88 return res.send(square_projects);
90 .catch(function(err) {
91 return handleError(res, err);
95 // Get a single square_project
96 exports.show = function(req, res) {
98 .findById(req.params.id)
99 .then(function(square_project) {
100 if (!square_project) {
101 return res.sendStatus(404);
103 return res.send(square_project);
105 .catch(function(err) {
106 return handleError(res, err);
110 // Creates a new square_project in the DB.
111 exports.create = function(req, res) {
114 .then(function(square_project) {
115 return res.status(201).send(square_project);
117 .catch(function(err) {
118 return handleError(res, err);
122 // Updates an existing square_project in the DB.
123 exports.update = function(req, res) {
128 .findById(req.params.id)
129 .then(function(square_project) {
130 if (!square_project) {
131 return res.sendStatus(404);
133 var updated = _.merge(square_project, req.body);
136 return res.status(200).send(square_project);
138 .catch(function(err) {
139 return handleError(res, err);
142 .catch(function(err) {
143 return handleError(res, err);
147 // Deletes a square_project from the DB.
148 exports.destroy = function(req, res) {
155 .then(function(square_project) {
156 if (!square_project) {
157 return res.sendStatus(404);
159 square_project.destroy()
161 return res.sendStatus(204);
163 .catch(function(err) {
164 return handleError(res, err);
167 .catch(function(err) {
168 return handleError(res, err);
172 // Deletes a square_project from the DB.
173 exports.bulkDestroy = function(req, res) {
179 individualHooks: true
182 return res.sendStatus(204);
184 .catch(function(err) {
185 return handleError(res, err);
189 exports.download = function(req, res, next) {
191 .findById(req.params.id)
192 .then(function(square_project) {
193 if (!square_project) {
194 return res.sendStatus(404);
196 console.log(req.query.filename);
198 "Content-Disposition": "attachment; filename=\"" + req.query.filename +
200 "Content-Type": "txt/xml"
202 return res.send(square_project.preproduction);
204 .catch(function(err) {
205 return handleError(res, err);
209 function handleError(res, err) {
210 return res.status(500).send(err);