package com.pixelized.server.lwa.server.rest.map

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

fun Engine.getMap(): suspend RoutingContext.() -> Unit {
    return {
        try {
            // get the query parameter
            val mapId = call.queryParameters.mapId
            // fetch the map
            val map = mapService.map(id = mapId)
            // respond to the request
            call.respond(
                message = APIResponse.success(
                    data = map,
                ),
            )
        } catch (exception: Exception) {
            call.exception(
                exception = exception
            )
        }
    }
}