3824ca19f3541c43184c0599370fe8cf0f47ed81
[motion.git] / server / api / desk_configuration / desk_configuration.controller.js
1 /**
2  * Using Rails-like standard naming convention for endpoints.
3  * GET     /api/desk/configurations              ->  index
4  * POST    /api/desk/configurations              ->  create
5  * GET     /api/desk/configurations/:id          ->  show
6  * PUT     /api/desk/configurations/:id          ->  update
7  * DELETE  /api/desk/configurations/:id          ->  destroy
8  */
9
10 'use strict';
11
12 var _ = require('lodash');
13
14 var DeskConfiguration = require('../../models').DeskConfiguration;
15 var DeskField = require('../../models').DeskField;
16
17 function handleError(res, statusCode) {
18   statusCode = statusCode || 500;
19   return function(err) {
20     res.status(statusCode).send(err);
21   };
22 }
23
24 function responseWithResult(res, statusCode) {
25   statusCode = statusCode || 200;
26   return function(entity) {
27     if (entity) {
28       res.status(statusCode).json(entity);
29     }
30   };
31 }
32
33 function handleEntityNotFound(res) {
34   return function(entity) {
35     if (!entity) {
36       res.status(404).end();
37       return null;
38     }
39     return entity;
40   };
41 }
42
43 function saveUpdates(updates) {
44   return function(entity) {
45     return entity.updateAttributes(updates)
46       .then(function(updated) {
47         return updated;
48       });
49   };
50 }
51
52 function removeEntity(res) {
53   return function(entity) {
54     if (entity) {
55       return entity.destroy()
56         .then(function() {
57           res.status(204).end();
58         });
59     }
60   };
61 }
62
63 // Gets a list of DeskConfigurations
64 exports.index = function(req, res) {
65   DeskConfiguration.findAll({
66       where: req.query
67     })
68     .then(responseWithResult(res))
69     .catch(handleError(res));
70 }
71
72 // Gets a single DeskConfiguration from the DB
73 exports.show = function(req, res) {
74   DeskConfiguration.find({
75       where: {
76         id: req.params.id
77       },
78       include: [{
79         model: DeskField,
80         as: 'Subject'
81       }, {
82         model: DeskField,
83         as: 'Description'
84       }, {
85         model: DeskField,
86         as: 'Field'
87       }]
88     })
89     .then(handleEntityNotFound(res))
90     .then(responseWithResult(res))
91     .catch(handleError(res));
92 }
93
94 // Creates a new DeskConfiguration in the DB
95 exports.create = function(req, res) {
96   DeskConfiguration.create(req.body)
97     .then(responseWithResult(res, 201))
98     .catch(handleError(res));
99 }
100
101 // Updates an existing DeskConfiguration in the DB
102 exports.update = function(req, res) {
103   if (req.body.id) {
104     delete req.body.id;
105   }
106   DeskConfiguration.findById(req.params.id)
107     .then(handleEntityNotFound(res))
108     .then(saveUpdates(req.body))
109     .then(responseWithResult(res))
110     .catch(handleError(res));
111 }
112
113 // Deletes a DeskConfiguration from the DB
114 exports.destroy = function(req, res) {
115   DeskConfiguration.findById(req.params.id)
116     .then(handleEntityNotFound(res))
117     .then(removeEntity(res))
118     .catch(handleError(res));
119 }
120
121 exports.addConfigurationField = function(req, res, next) {
122   // console.log(req.params);
123   var deskConfiguration;
124   DeskConfiguration
125     .findById(req.params.id)
126     .then(handleEntityNotFound(res))
127     .then(function(desk_configuration) {
128       deskConfiguration = desk_configuration;
129       return DeskField.create(req.body)
130     })
131     .then(function(deskField) {
132       switch (req.params.type) {
133         case 'subject':
134           return [deskConfiguration.addSubject(deskField), deskField];
135         case 'description':
136           return [deskConfiguration.addDescription(deskField), deskField];
137         case 'field':
138           return [deskConfiguration.addField(deskField), deskField];
139       }
140     })
141     .spread(function(data, deskField) {
142       return res.status(201).json(deskField);
143     })
144     .catch(handleError(res));
145 };