2 var _ = require('lodash');
3 var ChatApplication = require('../../models').ChatApplication;
4 // Get list of chat_applications
5 exports.index = function(req, res) {
14 .then(function(chat_applications) {
15 return res.status(200).send(chat_applications);
17 .catch(function(err) {
18 return handleError(res, err);
21 // Get a single chat_application
22 exports.show = function(req, res) {
24 .findById(req.params.id)
25 .then(function(chat_application) {
26 if (!chat_application) {
27 return res.sendStatus(404);
29 return res.send(chat_application);
31 .catch(function(err) {
32 return handleError(res, err);
35 // Creates a new chat_application in the DB.
36 exports.create = function(req, res, next) {
37 ChatApplication.max('priority', {
39 ChatWebsiteId: req.body.ChatWebsiteId
41 }).then(function(max) {
43 req.body.priority = max ? ++max : 1;
46 .then(function(mailApplication) {
47 return res.status(201).send(mailApplication);
49 .catch(function(err) {
52 }).catch(function(err) {
56 // Updates an existing chat_application in the DB.
57 exports.update = function(req, res) {
62 .findById(req.params.id)
63 .then(function(chat_application) {
64 if (!chat_application) {
65 return res.sendStatus(404);
67 var updated = _.merge(chat_application, req.body);
71 return res.status(200).send(chat_application);
73 .catch(function(err) {
74 return handleError(res, err);
77 .catch(function(err) {
78 return handleError(res, err);
81 // Deletes a chat_application from the DB.
82 exports.destroy = function(req, res) {
84 .findById(req.params.id)
85 .then(function(chat_application) {
86 if (!chat_application) {
87 return res.sendStatus(404);
92 return res.sendStatus(204);
94 .catch(function(err) {
95 return handleError(res, err);
98 .catch(function(err) {
99 return handleError(res, err);
103 function handleError(res, err) {
104 return res.status(500).send(err);