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