2 * Using Rails-like standard naming convention for endpoints.
3 * GET /api/desk/fields -> index
4 * POST /api/desk/fields -> create
5 * GET /api/desk/fields/:id -> show
6 * PUT /api/desk/fields/:id -> update
7 * DELETE /api/desk/fields/:id -> destroy
13 var _ = require('lodash');
15 var DeskField = require('../../models').DeskField;
18 function handleError(res, statusCode) {
19 statusCode = statusCode || 500;
20 return function(err) {
21 res.status(statusCode).send(err);
25 function responseWithResult(res, statusCode) {
26 statusCode = statusCode || 200;
27 return function(entity) {
29 res.status(statusCode).json(entity);
34 function handleEntityNotFound(res) {
35 return function(entity) {
37 res.status(404).end();
44 function saveUpdates(updates) {
45 return function(entity) {
46 return entity.updateAttributes(updates)
47 .then(function(updated) {
53 function removeEntity(res) {
54 return function(entity) {
56 return entity.destroy()
58 res.status(204).end();
64 // Gets a list of DeskFields
65 exports.index = function(req, res) {
67 .then(responseWithResult(res))
68 .catch(handleError(res));
71 // Gets a single DeskField from the DB
72 exports.show = function(req, res) {
73 DeskField.findById(req.params.id)
74 .then(handleEntityNotFound(res))
75 .then(responseWithResult(res))
76 .catch(handleError(res));
79 // Creates a new DeskField in the DB
80 exports.create = function(req, res) {
81 DeskField.create(req.body)
82 .then(responseWithResult(res, 201))
83 .catch(handleError(res));
86 // Updates an existing DeskField in the DB
87 exports.update = function(req, res) {
91 DeskField.findById(req.params.id)
92 .then(handleEntityNotFound(res))
93 .then(saveUpdates(req.body))
94 .then(responseWithResult(res))
95 .catch(handleError(res));
98 // Deletes a DeskField from the DB
99 exports.destroy = function(req, res) {
100 DeskField.findById(req.params.id)
101 .then(handleEntityNotFound(res))
102 .then(removeEntity(res))
103 .catch(handleError(res));