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