From 7165ed1bfddd5625c7156b544072e12fe41eb2da Mon Sep 17 00:00:00 2001 From: yostane <1958676+yostane@users.noreply.github.com> Date: Sat, 24 Aug 2024 12:26:06 +0200 Subject: [PATCH] update quiz collector --- .../result-explorer/demo01.ipynb | 1313 ++++++++++++++++- .../main/kotlin/example/com/Application.kt | 6 +- .../src/main/kotlin/example/com/Models.kt | 78 +- .../main/kotlin/example/com/QuizCollector.kt | 13 +- 4 files changed, 1349 insertions(+), 61 deletions(-) diff --git a/material/rest-api-ui-ktor-quiz-collector/result-explorer/demo01.ipynb b/material/rest-api-ui-ktor-quiz-collector/result-explorer/demo01.ipynb index 367f42c7..ca9a8e5a 100644 --- a/material/rest-api-ui-ktor-quiz-collector/result-explorer/demo01.ipynb +++ b/material/rest-api-ui-ktor-quiz-collector/result-explorer/demo01.ipynb @@ -3,9 +3,7 @@ { "cell_type": "markdown", "metadata": {}, - "source": [ - "# Quiz collector with kandy" - ] + "source": "# Quiz collector with kandy" }, { "cell_type": "code", @@ -14,13 +12,33 @@ "languageId": "python" }, "ExecuteTime": { - "end_time": "2024-08-23T22:10:39.416441300Z", - "start_time": "2024-08-23T22:10:34.053252400Z" + "end_time": "2024-08-24T10:23:18.959803600Z", + "start_time": "2024-08-24T10:23:13.328613100Z" } }, "source": [ + "// Without specifying %useLatestDescriptors, the version included in the Kotlin Jupyter kernel will be used.\n", + "%useLatestDescriptors \n", "%use kandy\n", - "%use dataframe\n", + "%use dataframe" + ], + "outputs": [], + "execution_count": 90 + }, + { + "metadata": {}, + "cell_type": "markdown", + "source": "## Collecting responses" + }, + { + "metadata": { + "ExecuteTime": { + "end_time": "2024-08-24T10:23:19.179801200Z", + "start_time": "2024-08-24T10:23:18.975805100Z" + } + }, + "cell_type": "code", + "source": [ "val df = DataFrame.read(\"http://localhost:8080/responses\")\n", "df" ], @@ -28,7 +46,7 @@ { "data": { "text/html": [ - " \n", " \n", " " ], - "application/kotlindataframe+json": "{\"$version\":\"2.0.0\",\"metadata\":{\"columns\":[\"What is the primary goal of Kotlin Multiplatform?\",\"How does Kotlin Multiplatform facilitate code sharing between platforms?\",\"Which platforms does Kotlin Multiplatform support?\",\"What is a common use case for Kotlin Multiplatform?\",\"What is a shared code module in Kotlin Multiplatform called?\",\"How does Kotlin Multiplatform handle platform-specific implementations?\",\"What languages can be interoperable with Kotlin Multiplatform?\",\"What tooling supports Kotlin Multiplatform development?\",\"What is the benefit of using Kotlin Multiplatform for mobile development?\",\"How does Kotlin Multiplatform differ from Kotlin Native and Kotlin/JS?\"],\"nrow\":3,\"ncol\":10},\"kotlin_dataframe\":[{\"What is the primary goal of Kotlin Multiplatform?\":\"Muzukashi\",\"How does Kotlin Multiplatform facilitate code sharing between platforms?\":\"By sharing business logic and adapting UI\",\"Which platforms does Kotlin Multiplatform support?\":null,\"What is a common use case for Kotlin Multiplatform?\":null,\"What is a shared code module in Kotlin Multiplatform called?\":null,\"How does Kotlin Multiplatform handle platform-specific implementations?\":null,\"What languages can be interoperable with Kotlin Multiplatform?\":null,\"What tooling supports Kotlin Multiplatform development?\":null,\"What is the benefit of using Kotlin Multiplatform for mobile development?\":null,\"How does Kotlin Multiplatform differ from Kotlin Native and Kotlin/JS?\":null},{\"What is the primary goal of Kotlin Multiplatform?\":\"To share code between multiple platforms\",\"How does Kotlin Multiplatform facilitate code sharing between platforms?\":\"By sharing business logic and adapting UI\",\"Which platforms does Kotlin Multiplatform support?\":\"Android, iOS, and web\",\"What is a common use case for Kotlin Multiplatform?\":\"Developing a cross-platform app\",\"What is a shared code module in Kotlin Multiplatform called?\":\"Shared module\",\"How does Kotlin Multiplatform handle platform-specific implementations?\":\"Through expect and actual declarations\",\"What languages can be interoperable with Kotlin Multiplatform?\":\"Java, JavaScript, Swift\",\"What tooling supports Kotlin Multiplatform development?\":\"IntelliJ IDEA, Android Studio\",\"What is the benefit of using Kotlin Multiplatform for mobile development?\":\"Code reuse and sharing\",\"How does Kotlin Multiplatform differ from Kotlin Native and Kotlin/JS?\":\"Kotlin Multiplatform allows sharing code between different platforms using common modules.\"},{\"What is the primary goal of Kotlin Multiplatform?\":\"Nashi\",\"How does Kotlin Multiplatform facilitate code sharing between platforms?\":\"Don't know\",\"Which platforms does Kotlin Multiplatform support?\":\"Windows Phone\",\"What is a common use case for Kotlin Multiplatform?\":\"No\",\"What is a shared code module in Kotlin Multiplatform called?\":\"Nashi\",\"How does Kotlin Multiplatform handle platform-specific implementations?\":\"Copy paste is the best\",\"What languages can be interoperable with Kotlin Multiplatform?\":\"Zenbu\",\"What tooling supports Kotlin Multiplatform development?\":\"Vim\",\"What is the benefit of using Kotlin Multiplatform for mobile development?\":\"Code reuse and sharing\",\"How does Kotlin Multiplatform differ from Kotlin Native and Kotlin/JS?\":\"Kotlin Multiplatform allows sharing code between different platforms using common modules.\"}]}" + "application/kotlindataframe+json": "{\"$version\":\"2.0.0\",\"metadata\":{\"columns\":[\"What is the primary goal of Kotlin Multiplatform?\",\"How does Kotlin Multiplatform facilitate code sharing between platforms?\",\"Which platforms does Kotlin Multiplatform support?\",\"What is a common use case for Kotlin Multiplatform?\",\"What is a shared code module in Kotlin Multiplatform called?\",\"How does Kotlin Multiplatform handle platform-specific implementations?\",\"What languages can be interoperable with Kotlin Multiplatform?\",\"What tooling supports Kotlin Multiplatform development?\",\"What is the benefit of using Kotlin Multiplatform for mobile development?\",\"How does Kotlin Multiplatform differ from Kotlin Native and Kotlin/JS?\"],\"nrow\":2,\"ncol\":10},\"kotlin_dataframe\":[{\"What is the primary goal of Kotlin Multiplatform?\":\"To share\",\"How does Kotlin Multiplatform facilitate code sharing between platforms?\":\"By sharing business logic and adapting UI\",\"Which platforms does Kotlin Multiplatform support?\":\"Android, iOS, and web\",\"What is a common use case for Kotlin Multiplatform?\":\"Developing a cross-platform app\",\"What is a shared code module in Kotlin Multiplatform called?\":\"Shared module\",\"How does Kotlin Multiplatform handle platform-specific implementations?\":\"actual\",\"What languages can be interoperable with Kotlin Multiplatform?\":\"Java, Swift\",\"What tooling supports Kotlin Multiplatform development?\":\"IntelliJ IDEA\",\"What is the benefit of using Kotlin Multiplatform for mobile development?\":\"and sharing\",\"How does Kotlin Multiplatform differ from Kotlin Native and Kotlin/JS?\":\"None\"},{\"What is the primary goal of Kotlin Multiplatform?\":\"To share code between multiple platforms\",\"How does Kotlin Multiplatform facilitate code sharing between platforms?\":\"By sharing business logic and adapting UI\",\"Which platforms does Kotlin Multiplatform support?\":\"Android, iOS, and web\",\"What is a common use case for Kotlin Multiplatform?\":\"Developing a cross-platform app\",\"What is a shared code module in Kotlin Multiplatform called?\":\"Shared module\",\"How does Kotlin Multiplatform handle platform-specific implementations?\":\"Through expect and actual declarations\",\"What languages can be interoperable with Kotlin Multiplatform?\":\"Java, JavaScript, Swift\",\"What tooling supports Kotlin Multiplatform development?\":\"IntelliJ IDEA, Android Studio\",\"What is the benefit of using Kotlin Multiplatform for mobile development?\":\"Code reuse and sharing\",\"How does Kotlin Multiplatform differ from Kotlin Native and Kotlin/JS?\":\"Kotlin Multiplatform allows sharing code between different platforms using common modules.\"}]}" + }, + "execution_count": 91, + "metadata": {}, + "output_type": "execute_result" + } + ], + "execution_count": 91 + }, + { + "metadata": {}, + "cell_type": "markdown", + "source": "## Correct answers stats" + }, + { + "metadata": { + "ExecuteTime": { + "end_time": "2024-08-24T10:23:19.446801800Z", + "start_time": "2024-08-24T10:23:19.220805400Z" + } + }, + "cell_type": "code", + "source": [ + "val correctDF = DataFrame.read(\"http://localhost:8080/correct-stats\")\n", + "correctDF.schema()" + ], + "outputs": [ + { + "data": { + "text/plain": [ + "question: String\n", + "correct: Int" + ] + }, + "execution_count": 92, + "metadata": {}, + "output_type": "execute_result" + } + ], + "execution_count": 92 + }, + { + "metadata": { + "ExecuteTime": { + "end_time": "2024-08-24T10:23:20.040806Z", + "start_time": "2024-08-24T10:23:19.652806400Z" + } + }, + "cell_type": "code", + "source": [ + "val correctDF2 = correctDF.add(\"tq\") { question.take(3) + Typography.ellipsis.toString() + question.takeLast(25) }\n", + "correctDF2" + ], + "outputs": [ + { + "data": { + "text/html": [ + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
questioncorrecttq
What is the primary goal of Kotlin Mu...1Wha… of Kotlin Multiplatform?
How does Kotlin Multiplatform facilit...2How…haring between platforms?
Which platforms does Kotlin Multiplat...2Whi…in Multiplatform support?
What is a common use case for Kotlin ...2Wha…for Kotlin Multiplatform?
What is a shared code module in Kotli...2Wha…lin Multiplatform called?
How does Kotlin Multiplatform handle ...1How…specific implementations?
What languages can be interoperable w...1Wha…ith Kotlin Multiplatform?
What tooling supports Kotlin Multipla...1Wha…ultiplatform development?
What is the benefit of using Kotlin M...1Wha…m for mobile development?
How does Kotlin Multiplatform differ ...1How…lin Native and Kotlin/JS?
\n", + " \n", + " \n", + " " + ], + "application/kotlindataframe+json": "{\"$version\":\"2.0.0\",\"metadata\":{\"columns\":[\"question\",\"correct\",\"tq\"],\"nrow\":10,\"ncol\":3},\"kotlin_dataframe\":[{\"question\":\"What is the primary goal of Kotlin Multiplatform?\",\"correct\":1,\"tq\":\"Wha\\u2026 of Kotlin Multiplatform?\"},{\"question\":\"How does Kotlin Multiplatform facilitate code sharing between platforms?\",\"correct\":2,\"tq\":\"How\\u2026haring between platforms?\"},{\"question\":\"Which platforms does Kotlin Multiplatform support?\",\"correct\":2,\"tq\":\"Whi\\u2026in Multiplatform support?\"},{\"question\":\"What is a common use case for Kotlin Multiplatform?\",\"correct\":2,\"tq\":\"Wha\\u2026for Kotlin Multiplatform?\"},{\"question\":\"What is a shared code module in Kotlin Multiplatform called?\",\"correct\":2,\"tq\":\"Wha\\u2026lin Multiplatform called?\"},{\"question\":\"How does Kotlin Multiplatform handle platform-specific implementations?\",\"correct\":1,\"tq\":\"How\\u2026specific implementations?\"},{\"question\":\"What languages can be interoperable with Kotlin Multiplatform?\",\"correct\":1,\"tq\":\"Wha\\u2026ith Kotlin Multiplatform?\"},{\"question\":\"What tooling supports Kotlin Multiplatform development?\",\"correct\":1,\"tq\":\"Wha\\u2026ultiplatform development?\"},{\"question\":\"What is the benefit of using Kotlin Multiplatform for mobile development?\",\"correct\":1,\"tq\":\"Wha\\u2026m for mobile development?\"},{\"question\":\"How does Kotlin Multiplatform differ from Kotlin Native and Kotlin/JS?\",\"correct\":1,\"tq\":\"How\\u2026lin Native and Kotlin/JS?\"}]}" + }, + "execution_count": 93, + "metadata": {}, + "output_type": "execute_result" + } + ], + "execution_count": 93 + }, + { + "metadata": { + "ExecuteTime": { + "end_time": "2024-08-24T10:23:20.291806700Z", + "start_time": "2024-08-24T10:23:20.072802700Z" + } + }, + "cell_type": "code", + "source": [ + "correctDF2.plot {\n", + " bars {\n", + " y(tq) {\n", + " axis.name = \"Question\"\n", + " }\n", + " x(correct)\n", + " }\n", + "}" + ], + "outputs": [ + { + "data": { + "text/html": [ + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " 0.0\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " 0.5\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " 1.0\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " 1.5\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " 2.0\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " Wha… of Kotlin Multiplatform?\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " How…haring between platforms?\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " Whi…in Multiplatform support?\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " Wha…for Kotlin Multiplatform?\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " Wha…lin Multiplatform called?\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " How…specific implementations?\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " Wha…ith Kotlin Multiplatform?\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " Wha…ultiplatform development?\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " Wha…m for mobile development?\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " How…lin Native and Kotlin/JS?\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " Question\n", + " \n", + " \n", + " \n", + " \n", + " correct\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "\n", + " " + ], + "application/plot+json": { + "output_type": "lets_plot_spec", + "output": { + "mapping": {}, + "data": { + "correct": [ + 1.0, + 2.0, + 2.0, + 2.0, + 2.0, + 1.0, + 1.0, + 1.0, + 1.0, + 1.0 + ], + "tq": [ + "Wha… of Kotlin Multiplatform?", + "How…haring between platforms?", + "Whi…in Multiplatform support?", + "Wha…for Kotlin Multiplatform?", + "Wha…lin Multiplatform called?", + "How…specific implementations?", + "Wha…ith Kotlin Multiplatform?", + "Wha…ultiplatform development?", + "Wha…m for mobile development?", + "How…lin Native and Kotlin/JS?" + ] + }, + "kind": "plot", + "scales": [ + { + "aesthetic": "y", + "discrete": true, + "name": "Question" + }, + { + "aesthetic": "x", + "limits": [ + null, + null + ] + } + ], + "layers": [ + { + "mapping": { + "y": "tq", + "x": "correct" + }, + "stat": "identity", + "sampling": "none", + "position": "dodge", + "geom": "bar" + } + ] + }, + "apply_color_scheme": true, + "swing_enabled": true + } }, - "execution_count": 6, + "execution_count": 94, "metadata": {}, "output_type": "execute_result" } ], - "execution_count": 6 + "execution_count": 94 }, { "cell_type": "code", @@ -695,8 +1894,8 @@ "languageId": "python" }, "ExecuteTime": { - "end_time": "2024-08-23T21:40:01.838147400Z", - "start_time": "2024-08-23T21:39:58.091494700Z" + "end_time": "2024-08-24T10:23:20.788804700Z", + "start_time": "2024-08-24T10:23:20.506803500Z" } }, "source": [ @@ -707,7 +1906,7 @@ { "data": { "text/html": [ - " \n", " \n", " " ], - "application/kotlindataframe+json": "{\"$version\":\"2.0.0\",\"metadata\":{\"columns\":[\"What is the primary goal of Kotlin Multiplatform?\",\"How does Kotlin Multiplatform facilitate code sharing between platforms?\",\"Which platforms does Kotlin Multiplatform support?\",\"What is a common use case for Kotlin Multiplatform?\",\"What is a shared code module in Kotlin Multiplatform called?\",\"How does Kotlin Multiplatform handle platform-specific implementations?\",\"What languages can be interoperable with Kotlin Multiplatform?\",\"What tooling supports Kotlin Multiplatform development?\",\"What is the benefit of using Kotlin Multiplatform for mobile development?\",\"How does Kotlin Multiplatform differ from Kotlin Native and Kotlin/JS?\"],\"nrow\":1,\"ncol\":10},\"kotlin_dataframe\":[{\"What is the primary goal of Kotlin Multiplatform?\":[\"Muzukashi\",\"To share code between multiple platforms\",\"Nashi\"],\"How does Kotlin Multiplatform facilitate code sharing between platforms?\":[\"By sharing business logic and adapting UI\",\"By sharing business logic and adapting UI\",\"Don't know\"],\"Which platforms does Kotlin Multiplatform support?\":[\"Android, iOS, and web\",\"Windows Phone\"],\"What is a common use case for Kotlin Multiplatform?\":[\"Developing a cross-platform app\",\"No\"],\"What is a shared code module in Kotlin Multiplatform called?\":[\"Shared module\",\"Nashi\"],\"How does Kotlin Multiplatform handle platform-specific implementations?\":[\"Through expect and actual declarations\",\"Copy paste is the best\"],\"What languages can be interoperable with Kotlin Multiplatform?\":[\"Java, JavaScript, Swift\",\"Zenbu\"],\"What tooling supports Kotlin Multiplatform development?\":[\"IntelliJ IDEA, Android Studio\",\"Vim\"],\"What is the benefit of using Kotlin Multiplatform for mobile development?\":[\"Code reuse and sharing\",\"Code reuse and sharing\"],\"How does Kotlin Multiplatform differ from Kotlin Native and Kotlin/JS?\":[\"Kotlin Multiplatform allows sharing code between different platforms using common modules.\",\"Kotlin Multiplatform allows sharing code between different platforms using common modules.\"]}]}" + "application/kotlindataframe+json": "{\"$version\":\"2.0.0\",\"metadata\":{\"columns\":[\"What is the primary goal of Kotlin Multiplatform?\",\"How does Kotlin Multiplatform facilitate code sharing between platforms?\",\"Which platforms does Kotlin Multiplatform support?\",\"What is a common use case for Kotlin Multiplatform?\",\"What is a shared code module in Kotlin Multiplatform called?\",\"How does Kotlin Multiplatform handle platform-specific implementations?\",\"What languages can be interoperable with Kotlin Multiplatform?\",\"What tooling supports Kotlin Multiplatform development?\",\"What is the benefit of using Kotlin Multiplatform for mobile development?\",\"How does Kotlin Multiplatform differ from Kotlin Native and Kotlin/JS?\"],\"nrow\":1,\"ncol\":10},\"kotlin_dataframe\":[{\"What is the primary goal of Kotlin Multiplatform?\":[\"To share\",\"To share code between multiple platforms\"],\"How does Kotlin Multiplatform facilitate code sharing between platforms?\":[\"By sharing business logic and adapting UI\",\"By sharing business logic and adapting UI\"],\"Which platforms does Kotlin Multiplatform support?\":[\"Android, iOS, and web\",\"Android, iOS, and web\"],\"What is a common use case for Kotlin Multiplatform?\":[\"Developing a cross-platform app\",\"Developing a cross-platform app\"],\"What is a shared code module in Kotlin Multiplatform called?\":[\"Shared module\",\"Shared module\"],\"How does Kotlin Multiplatform handle platform-specific implementations?\":[\"actual\",\"Through expect and actual declarations\"],\"What languages can be interoperable with Kotlin Multiplatform?\":[\"Java, Swift\",\"Java, JavaScript, Swift\"],\"What tooling supports Kotlin Multiplatform development?\":[\"IntelliJ IDEA\",\"IntelliJ IDEA, Android Studio\"],\"What is the benefit of using Kotlin Multiplatform for mobile development?\":[\"and sharing\",\"Code reuse and sharing\"],\"How does Kotlin Multiplatform differ from Kotlin Native and Kotlin/JS?\":[\"None\",\"Kotlin Multiplatform allows sharing code between different platforms using common modules.\"]}]}" }, - "execution_count": 4, + "execution_count": 95, "metadata": {}, "output_type": "execute_result" } ], - "execution_count": 4 + "execution_count": 95 } ], "metadata": { 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 46403fff..22b709e7 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 @@ -1,6 +1,9 @@ package example.com -import example.com.plugins.* +import example.com.plugins.configureHTTP +import example.com.plugins.configureRouting +import example.com.plugins.configureSerialization +import example.com.plugins.configureTemplating import io.ktor.server.application.* fun main(args: Array) { @@ -8,6 +11,7 @@ fun main(args: Array) { } fun Application.module() { + quizResponses.addAll(sampleResponses) configureHTTP() configureTemplating() configureSerialization() 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 index 72011fd4..2e0c6d81 100644 --- 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 @@ -27,4 +27,80 @@ data class QuizResponse(val responses: List) { } @Serializable -data class CollectResponse(val score: Int) \ No newline at end of file +data class CollectResponse(val score: Int) + +val quizResponses = mutableListOf() + +// sample data with correct and wrong answers +val sampleResponses = listOf( + QuizResponse( + listOf( + QuestionResponse( + "What is the primary goal of Kotlin Multiplatform?", + "To share" + ), + QuestionResponse( + "How does Kotlin Multiplatform facilitate code sharing between platforms?", + "By sharing business logic and adapting UI" + ), + QuestionResponse("Which platforms does Kotlin Multiplatform support?", "Android, iOS, and web"), + QuestionResponse("What is a common use case for Kotlin Multiplatform?", "Developing a cross-platform app"), + QuestionResponse("What is a shared code module in Kotlin Multiplatform called?", "Shared module"), + QuestionResponse( + "How does Kotlin Multiplatform handle platform-specific implementations?", + "actual" + ), + QuestionResponse( + "What languages can be interoperable with Kotlin Multiplatform?", + "Java, Swift" + ), + QuestionResponse( + "What tooling supports Kotlin Multiplatform development?", + "IntelliJ IDEA" + ), + QuestionResponse( + "What is the benefit of using Kotlin Multiplatform for mobile development?", + "and sharing" + ), + QuestionResponse( + "How does Kotlin Multiplatform differ from Kotlin Native and Kotlin/JS?", + "None" + ) + ) + ), + QuizResponse( + listOf( + QuestionResponse( + "What is the primary goal of Kotlin Multiplatform?", + "To share code between multiple platforms" + ), + QuestionResponse( + "How does Kotlin Multiplatform facilitate code sharing between platforms?", + "By sharing business logic and adapting UI" + ), + QuestionResponse("Which platforms does Kotlin Multiplatform support?", "Android, iOS, and web"), + QuestionResponse("What is a common use case for Kotlin Multiplatform?", "Developing a cross-platform app"), + QuestionResponse("What is a shared code module in Kotlin Multiplatform called?", "Shared module"), + QuestionResponse( + "How does Kotlin Multiplatform handle platform-specific implementations?", + "Through expect and actual declarations" + ), + QuestionResponse( + "What languages can be interoperable with Kotlin Multiplatform?", + "Java, JavaScript, Swift" + ), + QuestionResponse( + "What tooling supports Kotlin Multiplatform development?", + "IntelliJ IDEA, Android Studio" + ), + QuestionResponse( + "What is the benefit of using Kotlin Multiplatform for mobile development?", + "Code reuse and sharing" + ), + QuestionResponse( + "How does Kotlin Multiplatform differ from Kotlin Native and Kotlin/JS?", + "Kotlin Multiplatform allows sharing code between different platforms using common modules." + ) + ) + ), +) \ 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 dcc94923..bbc21ea9 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 @@ -6,8 +6,7 @@ import io.ktor.server.request.* import io.ktor.server.response.* import io.ktor.server.routing.* import kotlinx.html.* - -val quizResponses = mutableListOf() +import kotlinx.serialization.Serializable fun Application.configureQuizCollector() { routing { @@ -26,6 +25,16 @@ fun Application.configureQuizCollector() { }) } + get("/correct-stats") { + @Serializable + data class QuestionStats(val question: String, val correct: Int) + + val result = quizResponses.flatMap { it.responses }.groupBy { it.question } + .mapValues { it.value.count { qr -> correctResponses[qr.question] == qr.answer } } + .map { QuestionStats(it.key, it.value) } + call.respond(result) + } + get("/table2") { call.respond(quizResponses) }