Skip to content

Instantly share code, notes, and snippets.

@evagoras
Last active October 5, 2015 18:24
Show Gist options
  • Save evagoras/e773fa3133d7f18636db to your computer and use it in GitHub Desktop.
Save evagoras/e773fa3133d7f18636db to your computer and use it in GitHub Desktop.
ColdBox - REST - Issue with list() method getting called every time
<cfscript>
// Allow unique URL or combination of URLs, we recommend both enabled
setUniqueURLS(false);
// Auto reload configuration, true in dev makes sense to reload the routes on every request
//setAutoReload(false);
// Sets automatic route extension detection and places the extension in the rc.format variable
setExtensionDetection(true);
// The valid extensions this interceptor will detect
// setValidExtensions('xml,json,jsont,rss,html,htm');
setValidExtensions('json');
// If enabled, the interceptor will throw a 406 exception that an invalid format was detected or just ignore it
setThrowOnInvalidExtension(true);
// Base URL
if( len(getSetting('AppMapping') ) lte 1){
setBaseURL("http://#cgi.HTTP_HOST#/index.cfm");
}
else{
setBaseURL("http://#cgi.HTTP_HOST#/#getSetting('AppMapping')#/index.cfm");
}
addRoute(
pattern = '/tasks/:taskcode-numeric',
handler = 'Task',
action = {
GET = 'view',
PUT = 'update',
DELETE = 'remove'
}
);
addRoute(
pattern = '/tasks',
handler = 'Task',
action = {
POST="create",
GET="list"
}
);
// Your Application Routes
addRoute(pattern=":handler/:action?");
/** Developers can modify the CGI.PATH_INFO value in advance of the SES
interceptor to do all sorts of manipulations in advance of route
detection. If provided, this function will be called by the SES
interceptor instead of referencing the value CGI.PATH_INFO.
This is a great place to perform custom manipulations to fix systemic
URL issues your Web site may have or simplify routes for i18n sites.
@Event The ColdBox RequestContext Object
**/
function PathInfoProvider(Event){
/* Example:
var URI = CGI.PATH_INFO;
if (URI eq "api/foo/bar")
{
Event.setProxyRequest(true);
return "some/other/value/for/your/routes";
}
*/
return CGI.PATH_INFO;
}
</cfscript>
component {
property name="TaskService" inject="TaskService";
// OPTIONAL HANDLER PROPERTIES
this.prehandler_only = "";
this.prehandler_except = "";
this.posthandler_only = "";
this.posthandler_except = "";
this.aroundHandler_only = "";
this.aroundHandler_except = "";
// REST Allowed HTTP Methods Ex: this.allowedMethods = {delete='POST,DELETE',index='GET'}
this.allowedMethods = {
list = "GET",
view = "GET",
update = "POST",
create = "PUT",
remove = "DELETE"
};
// IMPLICIT FUNCTIONS: Uncomment to use
// function preHandler( event, rc, prc, action, eventArguments ){
// }
// function postHandler( event, rc, prc, action, eventArguments ){
// }
// function aroundHandler( event, rc, prc, targetAction, eventArguments ){
// // executed targeted action
// arguments.targetAction( event );
// }
// function onMissingAction( event, rc, prc, missingAction, eventArguments ){
// }
// function onError( event, rc, prc, faultAction, exception, eventArguments ){
// }
function list ( event, rc, prc )
{
param name="rc.skip" default=0;
param name="rc.take" default=-1;
param name="rc.usercode" default="";
param name="rc.status" default="";
param name="rc.groupcode" default="";
param name="rc.officecode" default="";
prc.tasks = TaskService.list(
skip = rc.skip,
take = rc.take,
cUserCode = rc.usercode,
cStatus = rc.status,
cGroupCode = rc.groupcode,
cOfficeCode = rc.officecode
);
if( arrayLen(prc.tasks) ) {
event.renderData( type="JSON", data=prc.tasks );
} else {
event.renderData( type="JSON", data={}, statusCode=404, statusMessage="Tasks not found");
}
}
function view ( event, rc, prc )
{
param name="rc.taskcode" default=-1;
// view a single Task
prc.tasks = TaskService.get( rc.taskcode );
if( arrayLen(prc.tasks) ) {
event.renderData( type="JSON", data=prc.tasks[1] );
} else {
event.renderData( type="JSON", data={}, statusCode=404, statusMessage="Tasks not found");
}
}
function update (event, rc, prc )
{
}
function create (event, rc, prc ) {
}
function remove ( event, rc, prc ) {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment