3 var _ = require('lodash');
4 var Company = require('../../models').Company;
5 var util = require('util');
7 // Get list of companies
8 exports.index = function(req, res) {
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;
17 offset: page * per_page
20 _.forIn(req.query, function(value, key) {
26 query.order = util.format('%s %s', req.query.sort_by, req.query.sort_order || 'ASC') || null;
32 attributes.forEach(function(attribute) {
35 $like: '%' + value + '%'
38 query.where.$or.push(tmp);
45 query.where[key].$like = '%' + value + '%';
50 .findAndCountAll(query)
51 .then(function(result) {
53 var total_pages = Math.ceil(result.count / per_page);
54 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;
55 var previous_page = page > 0 ? util.format('%s://%s%s?page=%d', req.protocol, req.headers.host, req.baseUrl, page - 1) : null;
57 res.status(200).send({
61 previous_page: previous_page,
62 total_pages: total_pages
66 .catch(function(err) {
67 res.status(500).send({
68 error: 'Something blew up!'
73 exports.companyValidation = function(req, res) {
74 console.log(req.body);
81 .then(function(companies) {
83 return res.sendStatus(404);
85 return res.send(companies);
87 .catch(function(err) {
88 return handleError(res, err);
92 // Get a single company
93 exports.show = function(req, res) {
95 .findById(req.params.id)
96 .then(function(company) {
98 return res.sendStatus(404);
100 return res.send(company);
102 .catch(function(err) {
103 return handleError(res, err);
107 // Creates a new company in the DB.
108 exports.create = function(req, res) {
111 .then(function(company) {
112 return res.status(201).send(company);
114 .catch(function(err) {
115 return handleError(res, err);
119 // Updates an existing company in the DB.
120 exports.update = function(req, res) {
130 .then(function(companies) {
132 return res.sendStatus(404);
134 if (companies.length > 0) {
135 return res.status(500).send({
136 message: 'MESSAGE_EXIST_COMPANY'
148 .then(function(company) {
150 return res.sendStatus(404);
152 var updated = _.merge(company, req.body);
155 return res.status(200).send(company);
157 .catch(function(err) {
158 return handleError(res, err);
161 .catch(function(err) {
162 return handleError(res, err);
165 .catch(function(err) {
166 return handleError(res, err);
170 // Deletes a company from the DB.
171 exports.destroy = function(req, res) {
173 .findById(req.params.id)
174 .then(function(company) {
176 return res.sendStatus(404);
180 return res.sendStatus(204);
182 .catch(function(err) {
183 return handleError(res, err);
186 .catch(function(err) {
187 return handleError(res, err);
191 exports.bulkDestroy = function(req, res) {
197 individualHooks: true
200 return res.sendStatus(204);
202 .catch(function(err) {
203 return handleError(res, err);
207 function handleError(res, err) {
208 return res.status(500).send(err);