Skip to content
Snippets Groups Projects
Commit d2895a3f authored by Hoai Viet Nguyen's avatar Hoai Viet Nguyen
Browse files

add simple controllers

parent 022c3449
No related branches found
No related tags found
No related merge requests found
package de.thk.gm.gdw.todolist
import java.net.URI
import java.net.http.HttpClient
import java.net.http.HttpRequest
import java.net.http.HttpResponse
fun main (args: Array<String>) {
val client = HttpClient.newBuilder().build();
val request = HttpRequest.newBuilder().GET().header("Test","test").uri(URI.create("https://postman-echo.com/get")).build()
val response = client.send(request, HttpResponse.BodyHandlers.ofString())
println(response.body())
}
\ No newline at end of file
### GET request to example server
GET https://postman-echo.com/get
Test: test
Host: test.de
###
\ No newline at end of file
package de.thk.gm.gdw.todolist.controllers
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.ResponseBody
@Controller
class GreetingsController {
@GetMapping("/greetings")
@ResponseBody
fun greetings(name: String): String {
return "Hallo $name"
}
}
\ No newline at end of file
package de.thk.gm.gdw.todolist.controllers
import de.thk.gm.gdw.todolist.models.Task
import org.springframework.http.HttpStatus
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.*
import java.util.*
import kotlin.collections.ArrayList
@Controller
class TasksController {
private var tasks : ArrayList<Task> = ArrayList()
@PostMapping("/tasks")
@ResponseStatus(HttpStatus.CREATED)
fun createTask(name: String) {
var task = Task()
task.name = name
tasks.add(task)
}
@GetMapping("/tasks")
@ResponseBody
fun getTasks() : String {
return tasks.joinToString(",")
}
@GetMapping("/tasks/{id}")
@ResponseBody
fun getTaskById(@PathVariable("id") id: UUID) : String {
val task = tasks.find { it.id == id }
return task.toString()
}
@PutMapping("/tasks/{id}")
@ResponseStatus(HttpStatus.NO_CONTENT)
fun updateTask(@PathVariable("id") id: UUID, name: String) {
var task = tasks.find { it.id == id }
task!!.name = name
}
@DeleteMapping("/tasks/{id}")
@ResponseStatus(HttpStatus.NO_CONTENT)
fun deleteTask(@PathVariable("id") id: UUID) {
var task = tasks.find { it.id == id }
tasks.remove(task)
}
}
\ No newline at end of file
package de.thk.gm.gdw.todolist.models
import java.util.*
class Task {
val id : UUID = UUID.randomUUID()
var name : String = ""
override fun toString(): String {
return "[$id, $name]"
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment