[BAEL-1753] added ToDo application controllers

This commit is contained in:
Syed Mansoor 2018-06-29 09:59:06 +10:00
parent 423ccc6a83
commit d834de3f23
2 changed files with 29 additions and 13 deletions

View File

@ -5,7 +5,7 @@ version '1.0-SNAPSHOT'
buildscript {
ext.kotlin_version = '1.2.40'
ext.kotlin_version = '1.2.41'
ext.ktor_version = '0.9.2'
repositories {

View File

@ -1,26 +1,26 @@
@file:JvmName("APIServer")
import io.ktor.application.call
import io.ktor.application.install
import io.ktor.features.CallLogging
import io.ktor.features.ContentNegotiation
import io.ktor.features.DefaultHeaders
import io.ktor.gson.gson
import io.ktor.http.ContentType
import io.ktor.request.path
import io.ktor.request.receive
import io.ktor.response.respond
import io.ktor.response.respondText
import io.ktor.routing.get
import io.ktor.routing.routing
import io.ktor.routing.*
import io.ktor.server.engine.embeddedServer
import io.ktor.server.netty.Netty
import org.slf4j.event.Level
data class Author(val name: String, val website: String)
data class ToDo(var id: Int, val name: String, val description: String, val completed: Boolean)
fun main(args: Array<String>) {
val toDoList = ArrayList<ToDo>();
val jsonResponse = """{
"id": 1,
"task": "Pay waterbill",
@ -42,15 +42,31 @@ fun main(args: Array<String>) {
setPrettyPrinting()
}
}
routing {
get("/todo") {
call.respondText(jsonResponse, ContentType.Application.Json)
}
get("/author") {
val author = Author("baeldung", "baeldung.com")
call.respond(author)
routing() {
route("/todo") {
post {
var toDo = call.receive<ToDo>();
toDo.id = toDoList.size;
toDoList.add(toDo);
call.respond("Added")
}
delete("/{id}") {
call.respond(toDoList.removeAt(call.parameters["id"]!!.toInt()));
}
get("/{id}") {
call.respond(toDoList[call.parameters["id"]!!.toInt()]);
}
get {
call.respond(toDoList);
}
}
get("/author"){
call.respond(Author("Baeldung","baeldung.com"));
}
}
}.start(wait = true)