package com.pixelized.server.lwa.server.rest.alteration

import com.pixelized.server.lwa.server.Engine
import com.pixelized.server.lwa.utils.extentions.alterationId
import com.pixelized.server.lwa.utils.extentions.exception
import com.pixelized.shared.lwa.protocol.rest.APIResponse
import io.ktor.server.response.respond
import io.ktor.server.routing.RoutingContext

fun Engine.getAlteration(): suspend RoutingContext.() -> Unit {
    return {
        try {
            // get the query parameter
            val alterationId = call.queryParameters.alterationId
            // get the alteration of the given id.
            val alteration = alterationService.alteration(alterationId = alterationId)
                ?: error("Alteration with id:$alterationId not found.")
            // send it back to the user.
            call.respond(
                message = APIResponse.success(
                    data = alteration
                )
            )
        } catch (exception: Exception) {
            call.exception(
                exception = exception,
            )
        }
    }
}