Skip to content

Refactor api controller #1528

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 3, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions app/templates/server/api/user(auth)/user.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,6 @@ function handleError(res, statusCode) {
};
}

function respondWith(res, statusCode) {
statusCode = statusCode || 200;
return function() {
res.status(statusCode).end();
};
}

/**
* Get list of users
* restriction: 'admin'
Expand Down
49 changes: 24 additions & 25 deletions endpoint/templates/basename.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,10 @@
'use strict';<% if (filters.models) { %>

import _ from 'lodash';<% if (filters.mongooseModels) { %>
var <%= classedName %> = require('./<%= basename %>.model');<% } if (filters.sequelizeModels) { %>
var sqldb = require('<%= relativeRequire(config.get('registerModelsFile')) %>');
var <%= classedName %> = sqldb.<%= classedName %>;<% } %>
import <%= classedName %> from './<%= basename %>.model';<% } if (filters.sequelizeModels) { %>
import {<%= classedName %>} from '<%= relativeRequire(config.get('registerModelsFile')) %>';<% } %>

function handleError(res, statusCode) {
statusCode = statusCode || 500;
return function(err) {
res.status(statusCode).send(err);
};
}

function responseWithResult(res, statusCode) {
function respondWithResult(res, statusCode) {
statusCode = statusCode || 200;
return function(entity) {
if (entity) {
Expand All @@ -30,16 +22,6 @@ function responseWithResult(res, statusCode) {
};
}

function handleEntityNotFound(res) {
return function(entity) {
if (!entity) {
res.status(404).end();
return null;
}
return entity;
};
}

function saveUpdates(updates) {
return function(entity) {
<% if (filters.mongooseModels) { %>var updated = _.merge(entity, updates);
Expand All @@ -62,14 +44,31 @@ function removeEntity(res) {
});
}
};
}

function handleEntityNotFound(res) {
return function(entity) {
if (!entity) {
res.status(404).end();
return null;
}
return entity;
};
}

function handleError(res, statusCode) {
statusCode = statusCode || 500;
return function(err) {
res.status(statusCode).send(err);
};
}<% } %>

// Gets a list of <%= classedName %>s
export function index(req, res) {<% if (!filters.models) { %>
res.json([]);<% } else { %>
<% if (filters.mongooseModels) { %><%= classedName %>.findAsync()<% }
if (filters.sequelizeModels) { %><%= classedName %>.findAll()<% } %>
.then(responseWithResult(res))
.then(respondWithResult(res))
.catch(handleError(res));<% } %>
}<% if (filters.models) { %>

Expand All @@ -82,15 +81,15 @@ export function show(req, res) {
}
})<% } %>
.then(handleEntityNotFound(res))
.then(responseWithResult(res))
.then(respondWithResult(res))
.catch(handleError(res));
}

// Creates a new <%= classedName %> in the DB
export function create(req, res) {
<% if (filters.mongooseModels) { %><%= classedName %>.createAsync(req.body)<% }
if (filters.sequelizeModels) { %><%= classedName %>.create(req.body)<% } %>
.then(responseWithResult(res, 201))
.then(respondWithResult(res, 201))
.catch(handleError(res));
}

Expand All @@ -107,7 +106,7 @@ export function update(req, res) {
})<% } %>
.then(handleEntityNotFound(res))
.then(saveUpdates(req.body))
.then(responseWithResult(res))
.then(respondWithResult(res))
.catch(handleError(res));
}

Expand Down