Skip to content

Commit

Permalink
Update quiz collector
Browse files Browse the repository at this point in the history
  • Loading branch information
yostane committed Aug 22, 2024
1 parent 885a429 commit 14f0d7a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ fun Application.module() {
configureTemplating()
configureSerialization()
configureRouting()
configureQuizCollector()
}
Original file line number Diff line number Diff line change
@@ -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<QuestionResponse>) {
val score: Int = responses.count { response -> correctResponses[response.question] == response.answer }
val histogram: Map<String, Int> = responses.groupingBy { it.answer }.eachCount()
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>()
val quizResponses = mutableListOf<QuizResponse>()

fun Application.configureCollector() {
data class CollectResponse(val score: Int)

fun Application.configureQuizCollector() {
routing {
post("/collect") {

val quizResponse = call.receive<QuizResponse>()
quizResponses.add(quizResponse)
call.respond(CollectResponse(quizResponse.score))
}

get("/ui") {
Expand All @@ -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}"
}
}
}
}
}
}
}
Expand Down

0 comments on commit 14f0d7a

Please sign in to comment.