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