Implement Delete API

1.     In the TutorialPublish server module, select the DeleteContact server routine:

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

2.     Replace it with the code below.

3.     Compile the server module.

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

Define_Com Class(#PRIM_JSON.Object) Name(#RequestData) Reference(*DYNAMIC)
Define_Com Class(#PRIM_IOC.StringWriter) Name(#StringWriter)
Define_Com Class(#PRIM_JSON.Writer) Name(#Writer) Textwriter(#StringWriter)
Define_Com Class(#PRIM_JSON.Document) Name(#Document)

#Resource := "Delete /Contact/{ContactID}"

#HttpStatus := 500

* Get the {ContactId} parameter which is the Key to the xContacts table.
If (#Context.Request.PathParameters.TryParseAsInt32( "ContactId", #xContactIdentification ))

* Attempt to delete the Contact
Delete From_File(xContacts) With_Key(#xContactIdentification)

* If successfull, write out an informational message
If_Status Is(*OKAY)

#Writer.TextWriter <= #StringWriter

#Writer.WriteString( ("Contact " + #xContactIdentification.AsString + " Deleted.") )

#Context.Response.HttpStatus := 200
#Context.Response.ContentType := "application/json"
#Context.Response.ContentString := #StringWriter.Text

Return

Endif

Endif

* When not found, send a 404 not found response.
#Com_Owner.MakeBadRequestResponse Context(#Context) Path(#Context.Request.Path) Resourcename("Invalid or no Contact ID. ") Status(#HttpStatus)

Endroutine