package de.walamana.views

import de.walamana.models.Gottesdienst
import de.walamana.models.Gottesdienste
import de.walamana.models.GottesdiensteDao
import io.ktor.http.*
import io.ktor.server.application.*
import io.ktor.server.request.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import io.ktor.server.util.*

fun Route.configureGottesdiensteRoutes() {
    route("/gottesdienste") {
        get {
            val data = GottesdiensteDao.allGottesdienste()
            call.respond(data)
        }
        put {
            val data = call.receive<Gottesdienst>()
            val gottesdienst = GottesdiensteDao.createGottesdienst(
                data.name,
                data.date,
                data.attendance,
                data.planId
            )
            call.respond(mapOf("id" to gottesdienst?.id))
        }
        patch {
            // TODO: Access only by admin
            val data = call.receive<Gottesdienst>()
            val changed = GottesdiensteDao.updateGottesdienst(
                data.id,
                data.name,
                data.date,
                data.attendance,
                data.planId
            )
            call.respond(HttpStatusCode.OK)
        }
        delete {
            val id = call.request.queryParameters["id"]?.toInt() ?: TODO("fail")
            GottesdiensteDao.deleteGottesdienst(id)
            call.respond(HttpStatusCode.OK)
        }
    }
}