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