Built motion from commit 5e31ea4.|0.0.32
[motion.git] / server / api / report_tree / report_tree.controller.js
1 'use strict';
2
3 var _ = require('lodash');
4 var util = require('util');
5 var ReportTree = require('../../models').ReportTree;
6
7 // Get the reports trees
8 exports.index = function(req, res) {
9   ReportTree
10     .findAll()
11     .then(function(trees) {
12       if (!trees) {
13         return res.sendStatus(404);
14       }
15       return res.status(200).send(trees);
16     })
17     .catch(function(err) {
18       return handleError(res, err);
19     });
20 };
21
22 // Updates an existing report in the DB.
23 exports.update = function(req, res) {
24   ReportTree
25     .findById(req.params.id)
26     .then(function(tree) {
27       if (!tree) {
28         return res.sendStatus(404);
29       }
30       var updated = _.merge(tree, req.body);
31       updated.save()
32         .then(function() {
33           return res.status(200).send(tree);
34         })
35         .catch(function(err) {
36           return handleError(res, err);
37         });
38     })
39     .catch(function(err) {
40       return handleError(res, err);
41     });
42 };
43
44 function handleError(res, err) {
45   return res.status(500).send(err);
46 }