-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GH-1 question-service with random questions and check answer function…
…ality
- Loading branch information
1 parent
8087351
commit 2e43a8f
Showing
3 changed files
with
46 additions
and
7 deletions.
There are no files selected for viewing
41 changes: 37 additions & 4 deletions
41
...tion-service/src/main/kotlin/no/lundesgaard/extremestartup/question/QuestionController.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String, Question<Int>> = 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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
spring: | ||
jackson: | ||
serialization: | ||
indent_output: true |