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