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