Example: Update resource with WSPut

Update a resource with the WSPut attribute.

Example updating a resource with WSPut

In this sample REST function a user resource is updated.

IMPORT com

TYPE profileType RECORD ... END RECORD

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

PUBLIC FUNCTION updateUsers(
    id INTEGER ATTRIBUTES(WSParam),
    thisUser profileType)
  ATTRIBUTES(WSPut,
             WSPath="/users/{id}",
             WSDescription="Update a user profile",
             WSThrows="400:@userError")
  RETURNS STRING
    DEFINE ret STRING
    WHENEVER ERROR CONTINUE
    UPDATE users
        SET name = thisUser.name,
            email = thisUser.email
        WHERE @id  = id
    WHENEVER ERROR STOP
    CASE
    WHEN SQLCA.SQLCODE = 0
       # test for processed rows
       IF SQLCA.SQLERRD[3] = 1 THEN
         LET ret = SFMT("Updated user with ID: %1",id)
       ELSE
         LET ret = SFMT("No user with ID: %1",id)
       END IF
    OTHERWISE
       LET ret=SFMT("Error updating user with ID: %1",id)     
       LET userError.message = SFMT("SQL error:%1 [%2]",SQLCA.SQLCODE, SQLERRMESSAGE)
       CALL com.WebServiceEngine.SetRestError(400,userError)
    END CASE
    RETURN ret
END FUNCTION