Built motion from commit 5e31ea4.|0.0.32
[motion.git] / server / api / business_action / business_action.controller.js
1 'use strict';
2
3 var _ = require('lodash');
4 var BusinessAction = require('../../models').BusinessAction;
5
6 // Get list of business_actions
7 exports.index = function(req, res) {
8   BusinessAction
9     .findAll()
10     .then(function(business_actions) {
11       return res.status(200).send(business_actions);
12     })
13     .catch(function(err) {
14       return handleError(res, err);
15     });
16 };
17
18 // Get a single businessAction
19 exports.show = function(req, res) {
20   BusinessAction
21     .findById(req.params.id)
22     .then(function(businessAction) {
23       if (!businessAction) {
24         return res.sendStatus(404);
25       }
26       return res.send(businessAction);
27     })
28     .catch(function(err) {
29       return handleError(res, err);
30     });
31 };
32
33 // Creates a new businessAction in the DB.
34 exports.create = function(req, res) {
35   BusinessAction
36     .create(req.body)
37     .then(function(businessAction) {
38       return res.status(201).send(businessAction);
39     })
40     .catch(function(err) {
41       return handleError(res, err);
42     });
43 };
44
45 // Updates an existing businessAction in the DB.
46 exports.update = function(req, res) {
47   if (req.body.id) {
48     delete req.body.id;
49   }
50   BusinessAction
51     .findById(req.params.id)
52     .then(function(businessAction) {
53       if (!businessAction) {
54         return res.sendStatus(404);
55       }
56       var updated = _.merge(businessAction, req.body);
57       updated.save()
58         .then(function() {
59           return res.status(200).send(businessAction);
60         })
61         .catch(function(err) {
62           return handleError(res, err);
63         });
64     })
65     .catch(function(err) {
66       return handleError(res, err);
67     });
68 };
69
70 // Deletes a businessAction from the DB.
71 exports.destroy = function(req, res) {
72   BusinessAction
73     .findById(req.params.id)
74     .then(function(businessAction) {
75       if (!businessAction) {
76         return res.sendStatus(404);
77       }
78       businessAction
79         .destroy()
80         .then(function() {
81           return res.status(200).send(businessAction);
82         })
83         .catch(function(err) {
84           return handleError(res, err);
85         });
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 }