Source Code for TutorialPublish

1.     Copy the source code below and replace the code in TutorialPublish with it.

2.     Compile the server module.

 

* -----------------------------------------------------------------------------------------------------

* Disclaimer:

*

* These examples are provided in an "AS IS" basis to demonstrate the use of the LANSA REST API.

* Use of this examples in an end user application with or without modification is the responsibility of the user.

* No warranty or indemnification of any kind is implied.

*

* ===============================

* Server routines in this module:

* ===============================

* Name    : GetContact

* Path    : /Contact/{ContactId}

* Action  : Fetch table xContacts using supplied parameter {ContactId}

* Response: Object {"FirstName":"<xContactFirstname>","LastName":"<xContactLastname>"}

* ==========================================================================================

* Name    : PutContact

* Path    : /Contact/{ContactId}

* Action  : Fetch table xContacts using supplied parameter {ContactId}. Update names in table using supplied JSON.

* Response: None

* ==========================================================================================

* Name    : DeleteContact

* Path    : /Contact/{ContactId}

* Action  : Delete xContacts using supplied parameter {ContactId}.

* Response: None

* ==========================================================================================

* Name    : GetContacts

* Path    : /Contacts

* Action  : Select first 20 records (value of #SetLimit) from table xContacts

* Response: Array of Objects {"FirstName":"<xContactFirstname>","LastName":"<xContactLastname>"} from database table xContacts.

* ==========================================================================================

* Name    : PutContacts

* Path    : /Contacts

* Action  : Select first 20 records (value of #SetLimit) from table xContacts

* Response: None

* ==========================================================================================

*

Begin_Com Role(*EXTENDS #PRIM_SRVM)

 

* Fields from xContacts table used in Get operations

Group_By Name(#ContactFields) Fields(#xContactIdentification #xContactFirstName #xContactLastName)

 

* Fields from xContacts table used in Put operations

Group_By Name(#UpdateContact) Fields(#xContactFirstName #xContactLastName)

 

* Variables used across server routines

Define Field(#ReturnCode) Reffld(#IO$STS)

Define Field(#Message) Type(*CHAR) Length(132)

Define_Com Class(#STD_INT) Name(#HttpStatus)

Define_Com Class(#STD_STRNG) Name(#Resource)

 

* =====================================================

* Get a contact with Key supplied in the Path Parameter

* =====================================================

* =====================================================

Srvroutine Name(GetContact) Response(*HTTP #context)

 

Endroutine

 

* =========================================================

* Update a Contact with Key supplied in the Path Parameters

* =========================================================

Srvroutine Name(PutContact) Response(*HTTP #Context)

 

Endroutine

 

* ==========================================================

* Delete a contact with Key supplied in the Path Parameters

* ==========================================================

Srvroutine Name(DeleteContact) Response(*HTTP #Context)

 

Endroutine

 

* ==================================================

* Access xContacts table got get a list of contacts

* ==================================================

Srvroutine Name(GetContacts) Response(*HTTP #context)

 

Endroutine

 

* ===========================================

* Update multiple Contacts in table xContacts

* ===========================================

Srvroutine Name(PutContacts) Response(*HTTP #Context)

 

Endroutine

 

* ========================================================================================

* Manufacture a bad response. This generic routine is called to return a meaningful error.

* ========================================================================================

 

Mthroutine Name(MakeBadRequestResponse)

 

* Reference to the HttpContext

Define_Map For(*INPUT) Class(#PRIM_SRVM.HttpContext) Name(#Context) Pass(*BY_REFERENCE)

* Resource Name

Define_Map For(*INPUT) Class(#STD_STRNG) Name(#ResourceName)

* The path used when the error occured

Define_Map For(*INPUT) Class(#STD_STRNG) Name(#Path)

* The HTTP status we want to return

Define_Map For(*INPUT) Class(#STD_INT) Name(#Status)

* An additional optional message.

Define_Map For(*INPUT) Class(#STD_STRNG) Name(#AdditionalMessage) Mandatory("")

 

*

Define_Com Class(#PRIM_IOC.StringWriter) Name(#StringWriter)

Define_Com Class(#PRIM_JSON.Writer) Name(#Writer) Textwriter(#StringWriter)

 

#Writer.TextWriter <= #StringWriter

 

* Top level object

#Writer.BeginObject

* The error object containing one or more messages

#Writer.BeginObject( "error" )

* The array of error messages

#Writer.BeginArray( "messages" )

* Write out the basic error details. Mention the resource and path.

#Writer.WriteString( ("Invalid request accessing resource: '&1' using Path: '&2'.").Substitute( #ResourceName, #Path ) )

* If we want to send an additional message, write it out before the end of the array

If (#AdditionalMessage <> "")

#Writer.WriteString( #AdditionalMessage )

Endif

 

#Writer.EndArray

#Writer.EndObject

#Writer.EndObject

 

* Write out the response

#Context.Response.HttpStatus := #Status

#Context.Response.ContentType := "application/json"

#Context.Response.ContentString := #StringWriter.Text

 

Endroutine

*

End_Com