2 var _ = require('lodash');
3 var FaxApplication = require('../../models').FaxApplication;
4 // Get list of fax_applications
5 exports.index = function(req, res) {
6 FaxApplication.findAll({
8 }).then(function(fax_applications) {
9 return res.status(200).send(fax_applications);
10 }).catch(function(err) {
11 return handleError(res, err);
14 // Get a single fax_application
15 exports.show = function(req, res) {
16 FaxApplication.findById(req.params.id).then(function(fax_application) {
17 if (!fax_application) {
18 return res.sendStatus(404);
20 return res.send(fax_application);
21 }).catch(function(err) {
22 return handleError(res, err);
25 // Creates a new fax_application in the DB.
26 exports.create = function(req, res, next) {
27 console.log(req.body);
28 FaxApplication.max('priority', {
30 FaxAccountId: req.body.FaxAccountId
32 }).then(function(max) {
34 req.body.priority = max ? ++max : 1;
35 FaxApplication.create(req.body).then(function(faxApplication) {
36 return res.status(201).send(faxApplication);
37 }).catch(function(err) {
41 }).catch(function(err) {
46 // .then(function(fax_application) {
47 // return res.status(201).send(fax_application);
49 // .catch(function(err) {
50 // return handleError(res, err);
53 // Updates an existing fax_application in the DB.
54 exports.update = function(req, res) {
58 FaxApplication.findById(req.params.id).then(function(fax_application) {
59 if (!fax_application) {
60 return res.sendStatus(404);
62 var updated = _.merge(fax_application, req.body);
63 updated.save().then(function() {
64 return res.status(200).send(fax_application);
65 }).catch(function(err) {
66 return handleError(res, err);
68 }).catch(function(err) {
69 return handleError(res, err);
72 // Deletes a fax_application from the DB.
73 exports.destroy = function(req, res) {
78 }).then(function(fax_application) {
79 if (!fax_application) {
80 return res.sendStatus(404);
82 fax_application.destroy().then(function() {
83 return res.sendStatus(204);
84 }).catch(function(err) {
85 return handleError(res, err);
87 }).catch(function(err) {
88 return handleError(res, err);
92 function handleError(res, err) {
93 return res.status(500).send(err);