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

Merge branch 'REST_video' into 'main'

Implement REST-Controller

See merge request !2
parents 71471c9e 1f553f6b
No related branches found
No related tags found
1 merge request!2Implement REST-Controller
...@@ -25,6 +25,7 @@ dependencies { ...@@ -25,6 +25,7 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'com.fasterxml.jackson.module:jackson-module-kotlin' implementation 'com.fasterxml.jackson.module:jackson-module-kotlin'
implementation 'org.jetbrains.kotlin:kotlin-reflect' implementation 'org.jetbrains.kotlin:kotlin-reflect'
implementation 'com.fasterxml.jackson.dataformat:jackson-dataformat-xml:2.18.1'
developmentOnly 'org.springframework.boot:spring-boot-devtools' developmentOnly 'org.springframework.boot:spring-boot-devtools'
runtimeOnly 'com.h2database:h2' runtimeOnly 'com.h2database:h2'
testImplementation 'org.springframework.boot:spring-boot-starter-test' testImplementation 'org.springframework.boot:spring-boot-starter-test'
......
...@@ -4,17 +4,16 @@ import de.thk.gm.gdw.todolist.models.Task ...@@ -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.TasksService
import de.thk.gm.gdw.todolist.services.UsersService import de.thk.gm.gdw.todolist.services.UsersService
import org.springframework.http.HttpStatus 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.bind.annotation.*
import org.springframework.web.server.ResponseStatusException import org.springframework.web.server.ResponseStatusException
import java.util.* import java.util.*
@Controller @RestController
@RequestMapping("/users/{userId}") @RequestMapping("/users/{userId}",produces = [MediaType.APPLICATION_JSON_VALUE,MediaType.APPLICATION_XML_VALUE])
class TasksController (private val tasksService: TasksService, private val usersService: UsersService) { class TasksRestController (private val tasksService: TasksService, private val usersService: UsersService) {
@PostMapping("/tasks") @PostMapping("/tasks")
@ResponseBody
@ResponseStatus(HttpStatus.CREATED) @ResponseStatus(HttpStatus.CREATED)
fun saveTask(name : String, @PathVariable userId: UUID) { fun saveTask(name : String, @PathVariable userId: UUID) {
val user = usersService.getUserById(userId) val user = usersService.getUserById(userId)
...@@ -31,7 +30,6 @@ class TasksController (private val tasksService: TasksService, private val users ...@@ -31,7 +30,6 @@ class TasksController (private val tasksService: TasksService, private val users
} }
@GetMapping("/tasks") @GetMapping("/tasks")
@ResponseBody
fun getTasks(@PathVariable userId: UUID) : List<Task> { fun getTasks(@PathVariable userId: UUID) : List<Task> {
val user = usersService.getUserById(userId) val user = usersService.getUserById(userId)
if(user != null) { if(user != null) {
...@@ -44,7 +42,6 @@ class TasksController (private val tasksService: TasksService, private val users ...@@ -44,7 +42,6 @@ class TasksController (private val tasksService: TasksService, private val users
} }
@GetMapping("/tasks/{id}") @GetMapping("/tasks/{id}")
@ResponseBody
fun getTaskById(@PathVariable id : UUID, @PathVariable userId: UUID) : Task { fun getTaskById(@PathVariable id : UUID, @PathVariable userId: UUID) : Task {
val user = usersService.getUserById(userId) val user = usersService.getUserById(userId)
if(user != null) { if(user != null) {
......
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
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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment