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