LernApp/src/main/kotlin/com/oliver/kidio/backend/plugins/Routing.kt

84 lines
2.5 KiB
Kotlin
Raw Normal View History

package com.oliver.kidio.backend
import io.ktor.server.application.*
import io.ktor.server.request.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
// Dummy-Speicher für die Test-Daten
private var mockRemainingSeconds = 1800
private val mockTasks = mutableListOf(
Task(
id = "t1",
question = "Was ist 12 + 15?",
options = listOf("25", "27", "30", "22"),
rewardMinutes = 10
),
Task(
id = "t2",
question = "Was ist 8 x 7?",
options = listOf("54", "56", "64", "48"),
rewardMinutes = 15
)
)
fun Application.configureRouting() {
routing {
// Health-Check
get("/api/v1/health") {
call.respond(mapOf("status" to "OK", "message" to "Kidio Backend läuft!"))
}
// 1. Alle verfügbaren Aufgaben abrufen
get("/api/v1/tasks") {
call.respond(mockTasks)
}
// 2. Antwort auf eine Aufgabe überprüfen
post("/api/v1/tasks/verify") {
val request = call.receive<TaskVerifyRequest>()
val isCorrect = when (request.taskId) {
"t1" -> request.selectedAnswer == "27"
"t2" -> request.selectedAnswer == "56"
else -> false
}
if (isCorrect) {
val reward = 10
mockRemainingSeconds += (reward * 60)
call.respond(
TaskVerifyResponse(
isCorrect = true,
earnedMinutes = reward,
message = "Super gemacht! Du hast $reward Minuten gewonnen."
)
)
} else {
call.respond(
TaskVerifyResponse(
isCorrect = false,
earnedMinutes = 0,
message = "Schade, das war leider falsch. Versuche es nochmal!"
)
)
}
}
// 3. Bildschirmzeit synchronisieren
post("/api/v1/screentime/sync") {
val request = call.receive<SyncRequest>()
mockRemainingSeconds -= request.usedSecondsSinceLastSync
if (mockRemainingSeconds < 0) mockRemainingSeconds = 0
call.respond(
SyncResponse(
remainingSeconds = mockRemainingSeconds,
isBlocked = mockRemainingSeconds <= 0
)
)
}
}
}