diff --git a/material/rest-api-ui-ktor-quiz-collector/src/main/kotlin/example/com/Application.kt b/material/rest-api-ui-ktor-quiz-collector/src/main/kotlin/example/com/Application.kt index 859f5a04..46403fff 100644 --- a/material/rest-api-ui-ktor-quiz-collector/src/main/kotlin/example/com/Application.kt +++ b/material/rest-api-ui-ktor-quiz-collector/src/main/kotlin/example/com/Application.kt @@ -12,4 +12,5 @@ fun Application.module() { configureTemplating() configureSerialization() configureRouting() + configureQuizCollector() } diff --git a/material/rest-api-ui-ktor-quiz-collector/src/main/kotlin/example/com/Models.kt b/material/rest-api-ui-ktor-quiz-collector/src/main/kotlin/example/com/Models.kt new file mode 100644 index 00000000..aca8c3be --- /dev/null +++ b/material/rest-api-ui-ktor-quiz-collector/src/main/kotlin/example/com/Models.kt @@ -0,0 +1,21 @@ +package example.com + + +val correctResponses = mapOf( + "What is the primary goal of Kotlin Multiplatform?" to "To share code between multiple platforms", + "How does Kotlin Multiplatform facilitate code sharing between platforms?" to "By sharing business logic and adapting UI", + "Which platforms does Kotlin Multiplatform support?" to "Android, iOS, and web", + "What is a common use case for Kotlin Multiplatform?" to "Developing a cross-platform app", + "What is a shared code module in Kotlin Multiplatform called?" to "Shared module", + "How does Kotlin Multiplatform handle platform-specific implementations?" to "Through expect and actual declarations", + "What languages can be interoperable with Kotlin Multiplatform?" to "Java, JavaScript, Swift", + "What tooling supports Kotlin Multiplatform development?" to "IntelliJ IDEA, Android Studio", + "What is the benefit of using Kotlin Multiplatform for mobile development?" to "Code reuse and sharing", + "How does Kotlin Multiplatform differ from Kotlin Native and Kotlin/JS?" to "Kotlin Multiplatform allows sharing code between different platforms using common modules." +) + +data class QuestionResponse(val question: String, val answer: String) +data class QuizResponse(val responses: List) { + val score: Int = responses.count { response -> correctResponses[response.question] == response.answer } + val histogram: Map = responses.groupingBy { it.answer }.eachCount() +} \ No newline at end of file diff --git a/material/rest-api-ui-ktor-quiz-collector/src/main/kotlin/example/com/QuizCollector.kt b/material/rest-api-ui-ktor-quiz-collector/src/main/kotlin/example/com/QuizCollector.kt index 444d98fa..34c13c9e 100644 --- a/material/rest-api-ui-ktor-quiz-collector/src/main/kotlin/example/com/QuizCollector.kt +++ b/material/rest-api-ui-ktor-quiz-collector/src/main/kotlin/example/com/QuizCollector.kt @@ -2,17 +2,21 @@ package example.com import io.ktor.server.application.* import io.ktor.server.html.* +import io.ktor.server.request.* +import io.ktor.server.response.* import io.ktor.server.routing.* -import kotlinx.html.body -import kotlinx.html.h1 -import kotlinx.html.title +import kotlinx.html.* -val replies = mutableListOf() +val quizResponses = mutableListOf() -fun Application.configureCollector() { +data class CollectResponse(val score: Int) + +fun Application.configureQuizCollector() { routing { post("/collect") { - + val quizResponse = call.receive() + quizResponses.add(quizResponse) + call.respond(CollectResponse(quizResponse.score)) } get("/ui") { @@ -22,7 +26,18 @@ fun Application.configureCollector() { } body { h1 { +"Quiz Collector" } - + if (quizResponses.isEmpty()) { + +"No responses yet." + } else { + +"Responses:" + ul { + quizResponses.forEach { quizResponse -> + li { + +"Score: ${quizResponse.score}" + } + } + } + } } } }