diff --git a/question-service/src/main/kotlin/no/lundesgaard/extremestartup/question/QuestionController.kt b/question-service/src/main/kotlin/no/lundesgaard/extremestartup/question/QuestionController.kt index 443bc81..26d5df0 100644 --- a/question-service/src/main/kotlin/no/lundesgaard/extremestartup/question/QuestionController.kt +++ b/question-service/src/main/kotlin/no/lundesgaard/extremestartup/question/QuestionController.kt @@ -1,16 +1,49 @@ package no.lundesgaard.extremestartup.question +import org.springframework.http.HttpStatus.NOT_FOUND +import org.springframework.web.bind.annotation.DeleteMapping import org.springframework.web.bind.annotation.GetMapping +import org.springframework.web.bind.annotation.PathVariable +import org.springframework.web.bind.annotation.PostMapping +import org.springframework.web.bind.annotation.RequestBody +import org.springframework.web.bind.annotation.ResponseStatus import org.springframework.web.bind.annotation.RestController import java.lang.String.format import kotlin.random.Random.Default.nextInt @RestController class QuestionController { - @GetMapping("/") - fun getQuestionSummary(): String { + private val questions: HashMap> = HashMap() + + @GetMapping("/random") + fun randomQuestion(): String { val r = nextInt(100) val q = if (r < 33) AdditionQuestion() else if (r < 66) SubtractionQuestion() else MultiplicationQuestion() - return format("%s: %s? %s\n", q.id, q, q.correctAnswer()) + questions[q.id] = q + return format("%s\n", q) + } + + @GetMapping("/{id}") + fun question(@PathVariable id: String): String { + val q = questions[id] ?: throw MissingQuestionException(id) + return format("%s\n", q) + } + + @PostMapping("/{id}") + fun checkAnswer(@PathVariable id: String, @RequestBody answer: String): String { + val q = questions[id] ?: throw MissingQuestionException(id) + return format("%s\n", answer == q.correctAnswer().toString()) } -} + + @DeleteMapping("/{id}") + fun delete(@PathVariable id: String):String { + val q = questions.remove(id) + return format("%s\n", q?.id ?: "") + } + + @GetMapping("/") + fun questions(): String = format("%d\n", questions.size) +} + +@ResponseStatus(NOT_FOUND) +class MissingQuestionException(id: String): RuntimeException(format("Unknown question id: %s", id)) diff --git a/question-service/src/main/kotlin/no/lundesgaard/extremestartup/question/questions.kt b/question-service/src/main/kotlin/no/lundesgaard/extremestartup/question/questions.kt index 6b14c23..f31b9a2 100644 --- a/question-service/src/main/kotlin/no/lundesgaard/extremestartup/question/questions.kt +++ b/question-service/src/main/kotlin/no/lundesgaard/extremestartup/question/questions.kt @@ -6,22 +6,24 @@ import kotlin.random.Random.Default.nextInt fun questionId() = UUID.randomUUID().toString().substring(0, 8) abstract class Question(val id: String = questionId()) { + abstract fun asText(): String abstract fun correctAnswer(): A + override fun toString() = "%s: %s".format(id, asText()) } abstract class BinaryMathsQuestion(val n1: Int = nextInt(20), val n2: Int = nextInt(20)): Question() class AdditionQuestion: BinaryMathsQuestion() { - override fun toString() = "what is %d plus %d".format(n1, n2) + override fun asText() = "what is %d plus %d".format(n1, n2) override fun correctAnswer() = n1 + n2 } class SubtractionQuestion: BinaryMathsQuestion() { - override fun toString() = "what is %d minus %d".format(n1, n2) + override fun asText() = "what is %d minus %d".format(n1, n2) override fun correctAnswer() = n1 - n2 } class MultiplicationQuestion: BinaryMathsQuestion() { - override fun toString() = "what is %d multiplied by %d".format(n1, n2) + override fun asText() = "what is %d multiplied by %d".format(n1, n2) override fun correctAnswer() = n1 * n2 } diff --git a/question-service/src/main/resources/application.yml b/question-service/src/main/resources/application.yml index e69de29..10b2c1a 100644 --- a/question-service/src/main/resources/application.yml +++ b/question-service/src/main/resources/application.yml @@ -0,0 +1,4 @@ +spring: + jackson: + serialization: + indent_output: true