59b09cba66c7effa1ca35bd76de56a07dd4ac853
[motion.git] / server / api / fax_application / fax_application.controller.js
1 'use strict';
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({
7     where: req.query
8   }).then(function(fax_applications) {
9     return res.status(200).send(fax_applications);
10   }).catch(function(err) {
11     return handleError(res, err);
12   });
13 };
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);
19     }
20     return res.send(fax_application);
21   }).catch(function(err) {
22     return handleError(res, err);
23   });
24 };
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', {
29     where: {
30       FaxAccountId: req.body.FaxAccountId
31     }
32   }).then(function(max) {
33     // SET PRIORITY
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) {
38       return next(err);
39     });
40     console.log(max);
41   }).catch(function(err) {
42     next(err);
43   });
44   // FaxApplication
45   // .create(req.body)
46   // .then(function(fax_application) {
47   //   return res.status(201).send(fax_application);
48   // })
49   // .catch(function(err) {
50   //   return handleError(res, err);
51   // });
52 };
53 // Updates an existing fax_application in the DB.
54 exports.update = function(req, res) {
55   if (req.body.id) {
56     delete req.body.id;
57   }
58   FaxApplication.findById(req.params.id).then(function(fax_application) {
59     if (!fax_application) {
60       return res.sendStatus(404);
61     }
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);
67     });
68   }).catch(function(err) {
69     return handleError(res, err);
70   });
71 };
72 // Deletes a fax_application from the DB.
73 exports.destroy = function(req, res) {
74   FaxApplication.find({
75     where: {
76       id: req.params.id
77     }
78   }).then(function(fax_application) {
79     if (!fax_application) {
80       return res.sendStatus(404);
81     }
82     fax_application.destroy().then(function() {
83       return res.sendStatus(204);
84     }).catch(function(err) {
85       return handleError(res, err);
86     });
87   }).catch(function(err) {
88     return handleError(res, err);
89   });
90 };
91
92 function handleError(res, err) {
93   return res.status(500).send(err);
94 }