Example: Delete a resource with WSDelete

Delete a resource with the WSDelete attribute.

Example deleting a resource with WSDelete

In this sample REST function a user resource is deleted. In the function's id parameter the attribute WSParam specifies the user to delete.

WSThrows is set to handle errors. In the TRY/CATCH block, the sqlca record is checked after the execution of the SQL query. The SQLERRMESSAGE is set to the message field of the userError variable, and a call to SetRestError() returns the message defined in WSThrows for the error.

IMPORT com

PUBLIC DEFINE userError RECORD ATTRIBUTE(WSError = "User error")
  message STRING
END RECORD

PUBLIC FUNCTION deleteUsers( id INTEGER ATTRIBUTES(WSParam) )
  ATTRIBUTES(WSDelete,
             WSPath = "/users/{id}",
             WSDescription = "Delete a user profile",
             WSThrows = "400:@userError")
  RETURNS STRING
    DEFINE ret STRING
    TRY
      DELETE FROM users WHERE @id = id
      IF sqlca.sqlerrd[3] = 1 THEN # sqlerrd[3] = processed rows
         LET ret = SFMT("Deleted user with ID: %1",id)
      ELSE
         LET ret = SFMT("No user with ID: %1",id)
      END IF
    CATCH
      LET ret = SFMT("Error deleting user with ID: %1",id)     
      LET userError.message = SFMT("SQL error:%1 [%2]",
                                    sqlca.sqlcode, SQLERRMESSAGE)
      CALL com.WebServiceEngine.SetRestError(400,userError)
    END TRY
    RETURN ret
END FUNCTION