package de.walamana.views

import de.walamana.models.*
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.configureMarksView() {
    route("/marks") {
        get {
            val data = MarksDao.allMarksForPlan(call.receiveParameters().getOrFail("id").toInt())
            call.respond(data)
        }
        put {
            val changedMarks = call.receive<List<Mark>>()
            for(changedMark in changedMarks) {
                val mark = MarksDao.setMark(
                    changedMark.mid,
                    changedMark.gid,
                    changedMark.value
                )
            }
            call.respond(HttpStatusCode.OK)
        }
        patch {
            // TODO: Access only by admin
            val changedMarks = call.receive<List<Mark>>()
            for(changedMark in changedMarks) {
                val mark = MarksDao.setMark(
                    changedMark.mid,
                    changedMark.gid,
                    changedMark.value
                )
            }
            call.respond(HttpStatusCode.OK)
        }
        delete {
            GottesdiensteDao.deleteGottesdienst(call.receiveParameters().getOrFail("id").toInt())
            call.respond(HttpStatusCode.OK)
        }
    }
}