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