demo-money-project/src/main/kotlin/com/example/demo/rest/ApiController.kt

39 lines
1.0 KiB
Kotlin

package com.example.demo.rest
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.RequestMapping
import org.springframework.web.bind.annotation.RestController
@RestController
@RequestMapping("/people")
class ApiController {
private val people = mutableListOf<Person>()
@GetMapping
fun index(): List<Person> {
return people
}
@PostMapping
fun create(@RequestBody person: Person): Person {
people.add(person)
return person
}
@DeleteMapping("/{id}")
fun delete(@PathVariable id: Long): String {
val person = people.firstOrNull { it.id == id } ?: return "not found"
people.remove(person)
return "deleted"
}
}
data class Person(
var id: Long? = null,
var name: String? = null
)