Function APIRoot_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        :   APIRoot_API

    Description :   API logic for the Apiroot 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.
                        ParentURL               - The URL path preceeding the current endpoint.
                                                  the SelfURL.
                        CurrentAPI              - The name of this stored procedure.

    Parameters  :
        API             [in] -- Web API to process. Format is [APIPattern].[HTTPMethod]:
                                    - APIPattern must follow this structure <Resource>[.ID.[<Property>]]
                                    - HTTPMethod can be any valid HTTP method, e.g., GET, POST, PUT, DELETE, etc.
                                Examples:
                                    - APIROOT.POST
                                    - APIROOT.ID.PUT
                                    - APIROOT.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)
        11/19/18    dmb     Original programmer.
        04/09/19    dmb     [SRPFW-271] Update the APIROOT.GET API to verify that the AuthenticatedAccountID has a value
                            before adding the resetPassword form action.
        04/29/19    dmb     Update the APIROOT.GET API to verify the sub-resource has at least one HTTP method before
                            adding it.
        05/28/19    dmb     [SRPFW-274] Replace all references to AddLinkRelationship with AddLinkRelation.
        07/16/19    dmb     [SRPFW-277] Retrofit APIROOT.GET API to use the HTTP_Resource_Manager_Services module.
        01/23/20    dmb     [SRPFW-296] Add matching HEAD APIs for all GET APIs.
        01/26/20    dmb     [SRPFW-296] Update the APIROOT.GET API so link relations are templated whenever possible.
        02/13/20    dmb     [SRPFW-311] Fix a minor typo in the APIROOT.GET API.

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

#pragma precomp SRP_PreCompiler

$insert APP_INSERTS
$insert API_SETUP
$insert HTTP_INSERTS
$insert HTTP_FRAMEWORK_SETUP_EQUATES

Declare function    Database_Services, HTTP_Resource_Manager_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 APIROOT.HEAD
API APIROOT.GET

    objResource     = HTTP_Resource_Services('GetObject')
    If Error_Services('NoError') then
        HTTP_Resource_Services('AddLinkRelation', objResource, 'self', FullEndpointURL)
        RootResources   = HTTP_Resource_Manager_Services('GetResourceChildren', FullEndpointURL, 'RESOURCE')
        For Each Resource in RootResources using @FM
            Methods = HTTP_Resource_Manager_Services('GetResourceProperty', Resource, 'METHODS')
            If Methods NE '' then
                Name    = HTTP_Resource_Manager_Services('GetResourceProperty', Resource, 'NAME')
                Title   = HTTP_Resource_Manager_Services('GetResourceProperty', Resource, 'TITLE')
                HTTP_Resource_Services('AddLinkRelation', objResource, Name, FullEndpointURL : '/' : Resource[-1, 'B/'], Title, True$)
            end
        Next Resource
    end

    If Error_Services('NoError') then
        AuthenticatedAccountID  = HTTP_Authentication_Services('GetAuthenticatedAccountID')
        If AuthenticatedAccountID NE '' then
            HTTP_Resource_Services('AddFormAction', objResource, 'resetPassword', 'PATCH', FullEndpointURL : '/webaccounts/' : AuthenticatedAccountID : '/password', 'Reset Password', 'value', '' : @VM : True$ : @VM : True$)
        end
    end

    If Error_Services('NoError') then
        Services    = HTTP_Resource_Services('GetSerializedResource', objResource)
        HTTP_Services('SetResponseBody', Services, False$, 'application/hal+json')
    end else
        HTTP_Services('SetResponseError', '', '', 500, Error_Services('GetMessage'), FullEndpointURL)
    end

end api
