Skip to content

Commit

Permalink
Add more features
Browse files Browse the repository at this point in the history
  • Loading branch information
yostane committed Aug 23, 2024
1 parent 14f0d7a commit 14edd7d
Show file tree
Hide file tree
Showing 13 changed files with 211 additions and 12 deletions.
3 changes: 1 addition & 2 deletions material/rest-api-ui-ktor-quiz-collector/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@

plugins {
alias(libs.plugins.kotlin.jvm)
alias(libs.plugins.ktor)
id("org.jetbrains.kotlin.plugin.serialization") version "2.0.10"
id("org.jetbrains.kotlin.plugin.serialization") version "2.0.20"
}

group = "example.com"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@

[versions]
ktor-version = "3.0.0-beta-2"
kotlin-version = "2.0.10"
kotlin-version = "2.0.20"
logback-version = "1.4.14"
kotlinx-html-version = "0.10.1"

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"version": "1",
"name": "quiz-collector-bruno",
"type": "collection",
"ignore": [
"node_modules",
".git"
],
"presets": {
"requestType": "http",
"requestUrl": "http://localhost:8080"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
meta {
name: /respond 1
type: http
seq: 1
}

post {
url: http://localhost:8080/respond
body: json
auth: none
}

body:json {
{
"responses": [
{
"question": "What is the primary goal of Kotlin Multiplatform?",
"answer": "Muzukashi"
},
{
"question": "How does Kotlin Multiplatform facilitate code sharing between platforms?",
"answer": "By sharing business logic and adapting UI"
}
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
meta {
name: /respond 2
type: http
seq: 2
}

post {
url: http://localhost:8080/respond
body: json
auth: none
}

body:json {
{
"responses": [
{
"question": "What is the primary goal of Kotlin Multiplatform?",
"answer": "To share code between multiple platforms"
},
{
"question": "How does Kotlin Multiplatform facilitate code sharing between platforms?",
"answer": "By sharing business logic and adapting UI"
},
{
"question": "Which platforms does Kotlin Multiplatform support?",
"answer": "Android, iOS, and web"
},
{
"question": "What is a common use case for Kotlin Multiplatform?",
"answer": "Developing a cross-platform app"
},
{
"question": "What is a shared code module in Kotlin Multiplatform called?",
"answer": "Shared module"
},
{
"question": "How does Kotlin Multiplatform handle platform-specific implementations?",
"answer": "Through expect and actual declarations"
},
{
"question": "What languages can be interoperable with Kotlin Multiplatform?",
"answer": "Java, JavaScript, Swift"
},
{
"question": "What tooling supports Kotlin Multiplatform development?",
"answer": "IntelliJ IDEA, Android Studio"
},
{
"question": "What is the benefit of using Kotlin Multiplatform for mobile development?",
"answer": "Code reuse and sharing"
},
{
"question": "How does Kotlin Multiplatform differ from Kotlin Native and Kotlin/JS?",
"answer": "Kotlin Multiplatform allows sharing code between different platforms using common modules."
}
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
meta {
name: /respond 3
type: http
seq: 3
}

post {
url: http://localhost:8080/respond
body: json
auth: none
}

body:json {
{
"responses": [
{
"question": "What is the primary goal of Kotlin Multiplatform?",
"answer": "Nashi"
},
{
"question": "How does Kotlin Multiplatform facilitate code sharing between platforms?",
"answer": "Don't know"
},
{
"question": "Which platforms does Kotlin Multiplatform support?",
"answer": "Windows Phone"
},
{
"question": "What is a common use case for Kotlin Multiplatform?",
"answer": "No"
},
{
"question": "What is a shared code module in Kotlin Multiplatform called?",
"answer": "Nashi"
},
{
"question": "How does Kotlin Multiplatform handle platform-specific implementations?",
"answer": "Copy paste is the best"
},
{
"question": "What languages can be interoperable with Kotlin Multiplatform?",
"answer": "Zenbu"
},
{
"question": "What tooling supports Kotlin Multiplatform development?",
"answer": "Vim"
},
{
"question": "What is the benefit of using Kotlin Multiplatform for mobile development?",
"answer": "Code reuse and sharing"
},
{
"question": "How does Kotlin Multiplatform differ from Kotlin Native and Kotlin/JS?",
"answer": "Kotlin Multiplatform allows sharing code between different platforms using common modules."
}
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
meta {
name: /results
type: http
seq: 4
}

get {
url: http://localhost:8080/results
body: none
auth: none
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
meta {
name: /table
type: http
seq: 5
}

get {
url: http://localhost:8080/table
body: none
auth: none
}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# quiz collector notebooks

```sh
pip install kotlin-jupyter-kernel jupyter
pip show kotlin-jupyter-kernel jupyter
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
kotlin-jupyter-kernel==0.12.0.217
jupyter==1.0.0
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package example.com

import kotlinx.serialization.Serializable


val correctResponses = mapOf(
"What is the primary goal of Kotlin Multiplatform?" to "To share code between multiple platforms",
Expand All @@ -14,8 +16,15 @@ val correctResponses = mapOf(
"How does Kotlin Multiplatform differ from Kotlin Native and Kotlin/JS?" to "Kotlin Multiplatform allows sharing code between different platforms using common modules."
)

@Serializable
data class QuestionResponse(val question: String, val answer: String)

@Serializable
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()
}
fun getScore(): Int {
return responses.count { response -> correctResponses[response.question] == response.answer }
}
}

@Serializable
data class CollectResponse(val score: Int)
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,21 @@ import kotlinx.html.*

val quizResponses = mutableListOf<QuizResponse>()

data class CollectResponse(val score: Int)

fun Application.configureQuizCollector() {
routing {
post("/collect") {
post("/respond") {
val quizResponse = call.receive<QuizResponse>()
quizResponses.add(quizResponse)
call.respond(CollectResponse(quizResponse.score))
call.respond(CollectResponse(quizResponse.getScore()))
}

get("/results") {
call.respond(quizResponses)
}

get("/table") {
call.respond(quizResponses.flatMap { it.responses }.groupBy { it.question }
.mapValues { it.value.map { it.answer } })
}

get("/ui") {
Expand All @@ -33,7 +40,7 @@ fun Application.configureQuizCollector() {
ul {
quizResponses.forEach { quizResponse ->
li {
+"Score: ${quizResponse.score}"
+"Score: ${quizResponse.getScore()}"
}
}
}
Expand Down

0 comments on commit 14edd7d

Please sign in to comment.