Function Semi_vend_code_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        :   Semi_vend_code_API

    Description :   API logic for the Semi_vend_code 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 Semi_vend_code[.ID.[<Property>]]
                                    - HTTPMethod can be any valid HTTP method, e.g., GET, POST, PUT, DELETE, etc.
                                Examples:
                                    - Semi_vend_code.POST
                                    - Semi_vend_code.ID.PUT
                                    - Semi_vend_code.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/12/25    djs     Original programmer.

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

#pragma precomp SRP_PreCompiler

$Insert APP_INSERTS
$Insert API_SETUP
$Insert HTTP_INSERTS
$Insert SEMI_VEND_CODE_EQUATES

Declare function Semi_Vend_Code_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 semi_vend_code.HEAD
API semi_vend_code.GET

    GoSub CreateHALCollection

end api


API semi_vend_code.ID.HEAD
API semi_vend_code.ID.GET

    GoSub CreateHALItem

end api


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

CreateHALItem:
    
    ResponseCode = 200
    SemiVendCd   = EndpointSegment
    IF RowExists('SEMI_VEND_CODE', SemiVendCd) then
        JSON = Semi_Vend_Code_Services('ConvertRecordToJSON', SemiVendCd)
        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', ResponseCode, Message)
            end else
                HTTP_Services('SetResponseStatus', ResponseCode)
            end
        end else
            Message = Error_Services('GetMessage')          
            HTTP_Services('SetResponseStatus', 500, 'Error in the ' : CurrentAPI : ' API. Message: ': Message)
        end        
    end else
        ResponseCode    = 406
        ResponseMessage = 'No WO found based on the search criteria provided.'
        HTTP_Services('SetResponseStatus', ResponseCode, ResponseMessage)
    end
    
return


//----------------------------------------------------------------------------------------------------------------------
// CreateHALCollection
//
// Creates a HAL+JSON collection based on OpenInsight data row representations.
//----------------------------------------------------------------------------------------------------------------------
CreateHALCollection:
    
    StatusCode     = 200
    JSONCollection = ''
    Abort          = False$
    SemiVendCodes  = Semi_Vend_Code_Services('GetSemiVendCodes')
    If Error_Services('NoError') then 
        If SemiVendCodes NE '' then
            If SRP_JSON(hJSONCollection, 'New', 'Object') then
                If SRP_Json(hSemiArray, 'New', 'Array') then
                    For each SemiVendCode in SemiVendCodes using @FM
                        SemiJson = Semi_Vend_Code_Services('ConvertRecordToJSON', SemiVendCode)
                        If (SRP_Json(hSemi, 'Parse', SemiJson) EQ '') then
                            hTemp = SRP_Json(hSemi, 'Get', 'SemiVendCode')
                            SRP_Json(hSemiArray, 'Add', hTemp)
                            SRP_Json(hTemp, 'Release')
                            SRP_Json(hSemi, 'Release')
                        end
                    Next SemiVendCode
                end
                SRP_Json(hJSONCollection, 'Set', 'SemiVendCodes', hSemiArray)
                SRP_Json(hSemiArray, 'Release')
                JSONCollection = SRP_JSON(hJSONCollection, 'Stringify', 'Fast')
                SRP_JSON(hJSONCollection, 'Release')
            end
        end else
            Message = 'No Semi Vend Codes found!'
        end
    end else
        Message    = Error_Services('GetMessage')
        StatusCode = 500
    end
    If Error_Services('NoError') then
        HTTP_Services('SetResponseHeaderField', 'Content-Location', FullEndpointURL)
        HTTP_Services('SetResponseBody', JSONCollection, 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

