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