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