2 * Using Rails-like standard naming convention for endpoints.
3 * GET /api/freshdesk/configurations -> index
4 * POST /api/freshdesk/configurations -> create
5 * GET /api/freshdesk/configurations/:id -> show
6 * PUT /api/freshdesk/configurations/:id -> update
7 * DELETE /api/freshdesk/configurations/:id -> destroy
13 var _ = require('lodash');
15 var FreshdeskConfiguration = require('../../models').FreshdeskConfiguration;
16 var FreshdeskField = require('../../models').FreshdeskField;
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 FreshdeskConfigurations
65 exports.index = function(req, res) {
66 FreshdeskConfiguration.findAll({
69 .then(responseWithResult(res))
70 .catch(handleError(res));
73 // Gets a single FreshdeskConfiguration from the DB
74 exports.show = function(req, res) {
75 FreshdeskConfiguration.find({
80 model: FreshdeskField,
83 model: FreshdeskField,
86 model: FreshdeskField,
90 .then(handleEntityNotFound(res))
91 .then(responseWithResult(res))
92 .catch(handleError(res));
95 // Creates a new FreshdeskConfiguration in the DB
96 exports.create = function(req, res) {
97 FreshdeskConfiguration.create(req.body)
98 .then(responseWithResult(res, 201))
99 .catch(handleError(res));
102 // Updates an existing FreshdeskConfiguration in the DB
103 exports.update = function(req, res) {
107 FreshdeskConfiguration.findById(req.params.id)
108 .then(handleEntityNotFound(res))
109 .then(saveUpdates(req.body))
110 .then(responseWithResult(res))
111 .catch(handleError(res));
114 // Deletes a FreshdeskConfiguration from the DB
115 exports.destroy = function(req, res) {
116 FreshdeskConfiguration.findById(req.params.id)
117 .then(handleEntityNotFound(res))
118 .then(removeEntity(res))
119 .catch(handleError(res));
122 exports.addConfigurationField = function(req, res, next) {
123 // console.log(req.params);
124 var freshdeskConfiguration;
125 FreshdeskConfiguration
126 .findById(req.params.id)
127 .then(handleEntityNotFound(res))
128 .then(function(salesforce_configuration) {
129 freshdeskConfiguration = salesforce_configuration;
130 return FreshdeskField.create(req.body)
132 .then(function(freshdeskField) {
133 switch (req.params.type) {
135 return [freshdeskConfiguration.addSubject(freshdeskField), freshdeskField];
137 return [freshdeskConfiguration.addDescription(freshdeskField), freshdeskField];
139 return [freshdeskConfiguration.addField(freshdeskField), freshdeskField];
142 .spread(function(data, freshdeskField) {
143 return res.status(201).json(freshdeskField);
145 .catch(handleError(res));