Function Materials_API(@API)
/***********************************************************************************************************************

    This program is proprietary and is not to be used by or disclosed to others, nor is it to be copied without written
    permission from SRP Computer Solutions, Inc.

    Name        :   Materials_API

    Description :   API logic for the Materials resource.

    Notes       :   All web APIs should include the API_SETUP insert. This will provide several useful variables:

                        HTTPMethod              - The HTTP Method (Verb) submitted by the client (e.g., GET, POST, etc.)
                        APIURL                  - The URL for the API entry point (e.g., api.mysite.com/v1).
                        FullEndpointURL         - The URL submitted by the client, including query params.
                        FullEndpointURLNoQuery  - The URL submitted by the client, excluding query params.
                        EndpointSegment         - The URL endpoint segment.
                        ParentURL               - The URL path preceeding the current endpoint.
                        CurrentAPI              - The name of this stored procedure.

    Parameters  :
        API             [in] -- Web API to process. Format is [APIPattern].[HTTPMethod]:
                                    - APIPattern must follow this structure Materials[.ID.[<Property>]]
                                    - HTTPMethod can be any valid HTTP method, e.g., GET, POST, PUT, DELETE, etc.
                                Examples:
                                    - Materials.POST
                                    - Materials.ID.PUT
                                    - Materials.ID.firstName.GET
        Response       [out] -- Response to be sent back to the Controller (HTTP_MCP) or requesting procedure. Web API
                                services do not rely upon anything being returned in the response. This is what the
                                various services like SetResponseBody and SetResponseStatus services are for. A response
                                value is only helpful if the developers want to use it for debug purposes.

    History     :   (Date, Initials, Notes)
        09/27/22    xxx     Original programmer.

***********************************************************************************************************************/

#pragma precomp SRP_PreCompiler

$insert APP_INSERTS
$insert API_SETUP
$insert HTTP_INSERTS

Declare function RDS_Services, WM_Out_Services, Oi_Wizard_Services, Memberof, Hold_Services
Declare subroutine Hold_Services

GoToAPI else
    // The specific resource endpoint doesn't have a API handler yet.
    HTTP_Services('SetResponseStatus', 204, 'This is a valid endpoint but a web API handler has not yet been created.')
end

Return Response OR ''


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Endpoint Handlers
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


API materials.HEAD
API materials.GET

    Null

end api


API materials.ID.HEAD
API materials.ID.GET
    
    StatusCode = 200
    GoSub CreateHALItem
    
end api


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Internal GoSubs
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


//----------------------------------------------------------------------------------------------------------------------
// CreateHALItem
//
// Creates a HAL+JSON object based on the OpenInsight data row representation of the scan.
//----------------------------------------------------------------------------------------------------------------------
CreateHALItem:
    
    MaterialKey = EndpointSegment
    Swap '-' with '*' in MaterialKey
    Swap '.' with '*' in MaterialKey
    Inbound     = (MaterialKey[1, 1] EQ 'I')
    Outbound    = (MaterialKey[1, 1] EQ 'O')
    If Inbound or Outbound then MaterialKey[1, 1] = ''
    
    Begin Case
        Case Num(MaterialKey)
            If RowExists('RDS', MaterialKey) then
                JSON = RDS_Services('ConvertRecordToJSON', MaterialKey, '', FullEndpointURL)
            end else
                Error_Services('Add', 'RDS record, ':MaterialKey:', does not exist.')
            end
        Case Inbound
            If RowExists('WM_IN', MaterialKey) then
                Error_Services('Add', 'WM_IN records are not supported at this time.')
            end else
                Error_Services('Add', 'WM_IN record, ':MaterialKey:', does not exist.')
            end
        Case Outbound
            If RowExists('WM_OUT', MaterialKey) then
                JSON = WM_Out_Services('ConvertRecordToJSON', MaterialKey, '', FullEndpointURL)
            end else
                Error_Services('Add', 'WM_OUT record, ':MaterialKey:', does not exist.')
            end
        Case Otherwise$
            Error_Services('Add', 'Only RDS and WM_OUT keys are supported at this time.')
            
    End Case

    If Error_Services('NoError') then
        HTTP_Services('SetResponseHeaderField', 'Content-Location', FullEndpointURL)
        HTTP_Services('SetResponseBody', JSON, False$, 'application/hal+json')
        If Assigned(Message) then
            HTTP_Services('SetResponseStatus', StatusCode, Message)
        end else
            HTTP_Services('SetResponseStatus', StatusCode)
        end
    end else
        Message = Error_Services('GetMessage')          
        HTTP_Services('SetResponseStatus', 500, 'Error in the ' : CurrentAPI : ' API. Message: ': Message)
    end

return
