diff --git a/build.gradle b/build.gradle index bcd387689b26da8bab5ae4614d925914835a7e1d..e1503af5269cc9f0e3a8fa3bdc544155673d26d7 100644 --- a/build.gradle +++ b/build.gradle @@ -25,6 +25,7 @@ dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'com.fasterxml.jackson.module:jackson-module-kotlin' implementation 'org.jetbrains.kotlin:kotlin-reflect' + implementation 'com.fasterxml.jackson.dataformat:jackson-dataformat-xml:2.18.1' developmentOnly 'org.springframework.boot:spring-boot-devtools' runtimeOnly 'com.h2database:h2' testImplementation 'org.springframework.boot:spring-boot-starter-test' diff --git a/src/main/kotlin/de/thk/gm/gdw/todolist/controllers/TasksController.kt b/src/main/kotlin/de/thk/gm/gdw/todolist/controllers/TasksRestController.kt similarity index 90% rename from src/main/kotlin/de/thk/gm/gdw/todolist/controllers/TasksController.kt rename to src/main/kotlin/de/thk/gm/gdw/todolist/controllers/TasksRestController.kt index 4361a5da4344e8f1c4643da770ec3df7c8c7108d..5c4b7362e602d95d1e409bbe54977e6b4cb8109a 100644 --- a/src/main/kotlin/de/thk/gm/gdw/todolist/controllers/TasksController.kt +++ b/src/main/kotlin/de/thk/gm/gdw/todolist/controllers/TasksRestController.kt @@ -4,17 +4,16 @@ import de.thk.gm.gdw.todolist.models.Task import de.thk.gm.gdw.todolist.services.TasksService import de.thk.gm.gdw.todolist.services.UsersService import org.springframework.http.HttpStatus -import org.springframework.stereotype.Controller +import org.springframework.http.MediaType import org.springframework.web.bind.annotation.* import org.springframework.web.server.ResponseStatusException import java.util.* -@Controller -@RequestMapping("/users/{userId}") -class TasksController (private val tasksService: TasksService, private val usersService: UsersService) { +@RestController +@RequestMapping("/users/{userId}",produces = [MediaType.APPLICATION_JSON_VALUE,MediaType.APPLICATION_XML_VALUE]) +class TasksRestController (private val tasksService: TasksService, private val usersService: UsersService) { @PostMapping("/tasks") - @ResponseBody @ResponseStatus(HttpStatus.CREATED) fun saveTask(name : String, @PathVariable userId: UUID) { val user = usersService.getUserById(userId) @@ -31,7 +30,6 @@ class TasksController (private val tasksService: TasksService, private val users } @GetMapping("/tasks") - @ResponseBody fun getTasks(@PathVariable userId: UUID) : List<Task> { val user = usersService.getUserById(userId) if(user != null) { @@ -44,7 +42,6 @@ class TasksController (private val tasksService: TasksService, private val users } @GetMapping("/tasks/{id}") - @ResponseBody fun getTaskById(@PathVariable id : UUID, @PathVariable userId: UUID) : Task { val user = usersService.getUserById(userId) if(user != null) { diff --git a/src/main/kotlin/de/thk/gm/gdw/todolist/controllers/UsersController.kt b/src/main/kotlin/de/thk/gm/gdw/todolist/controllers/UsersController.kt deleted file mode 100644 index 5631df981808d8f8339bf482b7f099e63f2ec3cf..0000000000000000000000000000000000000000 --- a/src/main/kotlin/de/thk/gm/gdw/todolist/controllers/UsersController.kt +++ /dev/null @@ -1,28 +0,0 @@ -package de.thk.gm.gdw.todolist.controllers - -import de.thk.gm.gdw.todolist.models.User -import de.thk.gm.gdw.todolist.services.UsersService -import org.springframework.stereotype.Controller -import org.springframework.web.bind.annotation.GetMapping -import org.springframework.web.bind.annotation.PostMapping -import org.springframework.web.bind.annotation.RequestBody -import org.springframework.web.bind.annotation.ResponseBody - -@Controller -class UsersController (private val usersService: UsersService) { - - @PostMapping("/users") - @ResponseBody - fun saveUser(email : String): User { - var user = User() - user.email = email - usersService.saveUser(user) - return user - } - - @GetMapping("/users") - @ResponseBody - fun getUsers(): List<User> { - return usersService.getAllUsers() - } -} \ No newline at end of file diff --git a/src/main/kotlin/de/thk/gm/gdw/todolist/controllers/UsersRestController.kt b/src/main/kotlin/de/thk/gm/gdw/todolist/controllers/UsersRestController.kt new file mode 100644 index 0000000000000000000000000000000000000000..0cb3c8886b62334d945b5c4325891e1f139df409 --- /dev/null +++ b/src/main/kotlin/de/thk/gm/gdw/todolist/controllers/UsersRestController.kt @@ -0,0 +1,60 @@ +package de.thk.gm.gdw.todolist.controllers + +import de.thk.gm.gdw.todolist.models.User +import de.thk.gm.gdw.todolist.services.UsersService +import org.springframework.http.HttpStatus +import org.springframework.http.MediaType +import org.springframework.web.bind.annotation.* +import org.springframework.web.server.ResponseStatusException +import java.util.* + +@RestController +@RequestMapping(produces = [MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE]) +class UsersRestController (private val usersService: UsersService) { + + @PostMapping("/users") + @ResponseStatus(HttpStatus.CREATED) + fun saveUser(email : String): User { + var user = User() + user.email = email + usersService.saveUser(user) + return user + } + + @GetMapping("/users") + fun getUsers(): List<User> { + return usersService.getAllUsers() + } + + @GetMapping("/users/{id}") + fun getUser(@PathVariable("id") id: UUID): User { + val user = usersService.getUserById(id) + if (user != null) { + return user + } else { + throw ResponseStatusException(HttpStatus.NOT_FOUND) + } + } + + @PutMapping("/users/{id}") + @ResponseStatus(HttpStatus.NO_CONTENT) + fun updateUser(email: String, @PathVariable id: UUID) { + var user = usersService.getUserById(id) + if (user != null) { + user.email = email + usersService.saveUser(user) + } else { + throw ResponseStatusException(HttpStatus.NOT_FOUND) + } + } + + @DeleteMapping("/users/{id}") + fun deleteUser(@PathVariable("id") id: UUID) { + var user = usersService.getUserById(id) + if (user != null) { + usersService.deleteUser(user) + } else { + throw ResponseStatusException(HttpStatus.NOT_FOUND) + } + } +} \ No newline at end of file