Function Scrubber_pm_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        :   Scrubber_pm_API

    Description :   API logic for the Scrubber_pm 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 Scrubber_pm[.ID.[<Property>]]
                                    - HTTPMethod can be any valid HTTP method, e.g., GET, POST, PUT, DELETE, etc.
                                Examples:
                                    - Scrubber_pm.POST
                                    - Scrubber_pm.ID.PUT
                                    - Scrubber_pm.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)
        06/05/24    xxx     Original programmer.

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

#pragma precomp SRP_PreCompiler

Declare Subroutine Pm_Services, Http_Services, Logging_Services
Declare function Error_Services, Logging_Services, Environment_Services, Datetime	

$insert APP_INSERTS
$insert API_SETUP
$insert HTTP_INSERTS

LogPath     = Environment_Services('GetApplicationRootPath') : '\LogFiles\Scrubbers\API'
LogDate     = Oconv(Date(), 'D4/')
LogTime     = Oconv(Time(), 'MTS')
LogFileName = LogDate[7, 4] : '-' : LogDate[1, 2] : '-' : LogDate[4, 2] : ' Tool Log.csv'
Headers     = 'Logging DTM' : @FM : 'ScrubberID' : @FM : 'Notes' : @FM : 'Message'
objLog      = Logging_Services('NewLog', LogPath, LogFileName, CRLF$, ',', Headers, '', False$, False$)
LoggingDTM  = LogDate : ' ' : LogTime   ; // Logging DTM

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 scrubber_pm.POST
    Body        = HTTP_Services('GetHTTPPostString')
	If Body NE '' then
		// The POST string will have been encoded so use percent (URL) decoding.
		PMJson        = HTTP_Services('DecodePercentString', Body)
		ParseResponse = SRP_JSON(objJson, 'PARSE', PMJson)
		If (ParseResponse EQ '') then
			EquipmentID = SRP_JSON(objJson, 'GetValue', 'EquipmentId')
			User        = SRP_JSON(objJson, 'GetValue', 'User')
		end else
			HTTP_Services('SetResponseStatus', 400, 'Unable to parse the JSON data from the request.')
		end
	end else
		HTTP_Services('SetResponseStatus', 400, 'JSON object is missing in the body of the request.')
	end

end api


API scrubber_pm.HEAD
API scrubber_pm.GET

    HTTP_Resource_Services('LoremIpsum')

end api


API scrubber_pm.ID.HEAD
API scrubber_pm.ID.GET

    ScrubberID = EndpointSegment
    IF RowExists('TOOL', ScrubberID) then
    	Pm_Services('CompleteScrubberPM', ScrubberID)
    	If Error_Services('HasError') then
    		ErrMsg     = Error_Services('GetMessage')
    		HTTP_Services('SetResponseStatus', 500, ErrMsg)
    		LogData    = ''
			LogData<1> = OConv(Datetime(), 'DT2/^H')
			LogData<2> = ScrubberID
			LogData<3> = 'Error'
			LogData<4> = ErrMsg
			Logging_Services('AppendLog', objLog, LogData, @RM, @FM)   
    	end else
    		LogData    = ''
			LogData<1> = OConv(Datetime(), 'DT2/^H')
			LogData<2> = ScrubberID
			LogData<3> = 'Success'
			LogData<4> = 'Scrubber PM Completion was successful'
			Logging_Services('AppendLog', objLog, LogData, @RM, @FM)    
    	end
    end else
    	ErrMsg     = 'Scrubber does not exist!'
        HTTP_Services('SetResponseStatus', 500, ErrMsg)
        LogData    = ''
		LogData<1> = OConv(Datetime(), 'DT2/^H')
		LogData<2> = ScrubberID
		LogData<3> = 'Error'
		LogData<4> = ErrMsg
		Logging_Services('AppendLog', objLog, LogData, @RM, @FM)  
    end
end api
