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
12 var _ = require('lodash');
14 var DeskConfiguration = require('../../models').DeskConfiguration;
15 var DeskField = require('../../models').DeskField;
17 function handleError(res, statusCode) {
18 statusCode = statusCode || 500;
19 return function(err) {
20 res.status(statusCode).send(err);
24 function responseWithResult(res, statusCode) {
25 statusCode = statusCode || 200;
26 return function(entity) {
28 res.status(statusCode).json(entity);
33 function handleEntityNotFound(res) {
34 return function(entity) {
36 res.status(404).end();
43 function saveUpdates(updates) {
44 return function(entity) {
45 return entity.updateAttributes(updates)
46 .then(function(updated) {
52 function removeEntity(res) {
53 return function(entity) {
55 return entity.destroy()
57 res.status(204).end();
63 // Gets a list of DeskConfigurations
64 exports.index = function(req, res) {
65 DeskConfiguration.findAll({
68 .then(responseWithResult(res))
69 .catch(handleError(res));
72 // Gets a single DeskConfiguration from the DB
73 exports.show = function(req, res) {
74 DeskConfiguration.find({
89 .then(handleEntityNotFound(res))
90 .then(responseWithResult(res))
91 .catch(handleError(res));
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));
101 // Updates an existing DeskConfiguration in the DB
102 exports.update = function(req, res) {
106 DeskConfiguration.findById(req.params.id)
107 .then(handleEntityNotFound(res))
108 .then(saveUpdates(req.body))
109 .then(responseWithResult(res))
110 .catch(handleError(res));
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));
121 exports.addConfigurationField = function(req, res, next) {
122 // console.log(req.params);
123 var 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)
131 .then(function(deskField) {
132 switch (req.params.type) {
134 return [deskConfiguration.addSubject(deskField), deskField];
136 return [deskConfiguration.addDescription(deskField), deskField];
138 return [deskConfiguration.addField(deskField), deskField];
141 .spread(function(data, deskField) {
142 return res.status(201).json(deskField);
144 .catch(handleError(res));