Skip to content

Instantly share code, notes, and snippets.

@cfvonner
Created October 1, 2018 16:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cfvonner/1f9dfd96d43a33a178b03f98c9666fb2 to your computer and use it in GitHub Desktop.
Save cfvonner/1f9dfd96d43a33a178b03f98c9666fb2 to your computer and use it in GitHub Desktop.
FW/1 API Problems
component name="Actor REST Endpoint" accessors="true" output="false" {
// FW/1 will use its dependency injection framework (DI/1) to find and
// inject the components defined below as properties
property fw;
property actorService;
property movieToActorService;
public void function default( rc ){
var q = actorService.getAll();
rc.data = q.reduce( function( prev, row ){
var movieLinks = movieToActorService.getByActorID( row.ActorID );
var movieIDs = valueArray( movieLinks, "MovieID" );
var actor = {
"ActorId" : row.ActorID,
"ActorName" : row.ActorName,
"BirthDate" : row.BirthDate,
"BornInCity" : row.BornInCity,
"MovieIDs" : movieIDs
};
return prev.append( actor );
}, []);
// renderData() is a FW/1 utility function to serialize/render data for
// REST APIs
fw.renderData().data( data ).type( 'json');
}
public void function show( rc ){
var q = actorService.getById( rc.id );
if ( q.recordCount ) {
var movieLinks = movieToActorService.getByActorID( rc.id );
var movieIDs = valueArray( movieLinks, "MovieID" );
var data = {
"ActorId" : q.ActorID,
"ActorName" : q.ActorName,
"BirthDate" : q.BirthDate,
"BornInCity" : q.BornInCity,
"MovieIDs" : movieIDs
};
fw.renderData().data( data ).type( 'json' );
}
else {
fw.renderData().data( '' ).type( 'json' ).statusCode( 404 );
}
}
public void function create( rc ){
try {
var result = actorService.insert( rc.actorname, rc.birthdate, rc.bornincity );
fw.renderData()
.data( '' )
.type( 'json' )
.statusCode( 201 )
.header( "X-INSERTED-ID", result );
}
catch ( any e ) {
fw.renderData().data( '' ).type( 'json' ).statusCode( 400 );
}
}
/*
** Data for put and patch must be sent in the request as x-www-form-urlencoded
** data with "content-type" header set to "application/x-www-form-urlencoded",
** or as json data with "content-type" header set to "application/json"
*/
public void function update( rc ){
var q = actorService.getById( rc.id );
if ( q.recordCount ) {
// if the REST request didn't include all of the actor arguments/fields
// then supply them from the current database record
var _actorname = structKeyExists( rc, "actorname" ) ? rc.actorname : q.actorname;
var _birthdate = structKeyExists( rc, "birthdate") ? rc.birthdate : q.birthdate;
var _bornincity = structKeyExists( rc, "bornincity") ? rc.bornincity : q.bornincity;
try {
actorService.update( rc.id, _actorname, _birthdate, _bornincity );
fw.renderData().data( '' ).type( 'json' ).statusCode( 204 );
}
catch ( any e ) {
fw.renderData().data( '' ).type( 'json' ).statusCode( 400 );
}
}
else {
fw.renderData().data( '' ).type( 'json' ).statusCode( 404 );
}
}
public void function destroy( rc ){
var result = actorService.delete( rc.id );
if ( result > 0 ) {
fw.renderData().data( '' ).type( 'json' ).statusCode( 204 );
}
else {
fw.renderData().data( '' ).type( 'json' ).statusCode( 404 );
}
}
}
component extends="framework.one" output="false" {
variables.dbPath = expandPath( "/database/Derby/" );
this.name = hash( getCurrentTemplatePath() );
this.applicationTimeout = CreateTimeSpan( 1, 0, 0, 0 );
this.sessionManagement = true;
this.sessionTimeout = CreateTimeSpan( 0, 2, 0, 0 );
this.sessioncookie.httponly = true;
this.sessioncookie.timeout = "10";
this.serialization.preserveCaseForStructKey = true;
this.passArrayByReference = true;
this.mappings = { "/sharedModel" : expandPath( "../sharedModel" ) };
this.datasources = {
Movies = {
url = "jdbc:derby:#variables.dbPath#;create=true;MaxPooledStatements=300",
driver = "Apache Derby Embedded"
}
};
this.datasource = 'Movies';
// FW/1 settings
variables.framework = {
action = 'action',
defaultSection = 'main',
defaultItem = 'default',
reloadApplicationOnEveryRequest = true,
generateSES = true,
SESOmitIndex = true,
diEngine = "di1",
diComponent = "framework.ioc",
diLocations = [ "/model", "/controllers", "/sharedModel" ],
diConfig = { },
/*
** This bit here maps standard REST API URL paths to the actual controllers
** and methods within this Framework-One application. It will also parse
** the url and identify the id value being passed in the URL.
** See http://framework-one.github.io/documentation/4.2/developing-applications.html#url-routes
** for more information.
*/
routes = [
{ "$RESOURCES" = "actor,movie,movieToActor" }
],
/*
** The decodeRequestBody setting allows FW/1 to accept JSON data or URL-encoded
** form data for POST, PUT and PATCH actions.
** See http://framework-one.github.io/documentation/4.2/developing-applications.html#controllers-for-rest-apis
** for more information.
*/
decodeRequestBody = true,
/*
** The preflightOptions setting allows FW/1 to respond to OPTIONS requests,
** which many front-end applications will call before doing POST/PUT/PATCH/DELETE
** requests.
*/
preflightOptions = true
};
public void function setupSession() { }
public void function setupRequest() { }
public void function setupView() { }
public void function setupResponse() { }
public string function onMissingView(struct rc = {}) {
return "Error 404 - Page not found.";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment