-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathTodoRouter.kt
79 lines (66 loc) · 2.43 KB
/
TodoRouter.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package todoapp.web
import mu.KLogging
import org.springframework.web.reactive.function.server.*
import todoapp.application.TodoCleanup
import todoapp.application.TodoFind
import todoapp.application.TodoModification
import todoapp.application.TodoRegistry
import todoapp.domain.TodoId
import todoapp.web.command.WriteTodoCommand
import todoapp.web.validation.WriteTodoCommandValidator
import todoapp.web.validation.process
import java.net.URI
/**
* 할 일 관리 라우터
*
* @author [email protected]
*/
class TodoRouter(
val find: TodoFind,
val registry: TodoRegistry,
val modification: TodoModification,
val cleanup: TodoCleanup
): RouterFunction<ServerResponse> {
private val validator = WriteTodoCommandValidator()
private val delegate = coRouter {
"/api/todos".nest {
GET("") {
ok().bodyValueAndAwait(find.all())
}
GET("/{id}") { request ->
val id = TodoId(request.pathVariable("id"))
ok().bodyValueAndAwait(find.byId(id))
}
POST("") { request ->
val command = request.awaitBody<WriteTodoCommand>().apply {
validator.process(target = this)
}
registry.register(command.text).let {
created(URI.create("/api/todos/$it")).bodyValueAndAwait(it)
}
}
PUT("/{id}") { request ->
val id = TodoId(request.pathVariable("id"))
val command = request.awaitBody<WriteTodoCommand>().apply {
validator.process(target = this)
}
modification.modify(id, command.text, command.completed)
ok().bodyValueAndAwait(find.byId(id))
}
// TODO 할 일을 정리하는 HandlerFunction을 작성하세요
// 요청 : DELETE /{id}
// 응답 : Unit
// 귀뜸: TodoCleanup 인터페이스가 제공하는 clear 메서드를 이용해보세요
DELETE("/{id}") { request ->
cleanup.clear(TodoId(request.pathVariable("id")))
ok().buildAndAwait()
}
POST("/clear-completed") {
cleanup.clearAllCompleted()
ok().buildAndAwait()
}
}
}
override fun route(request: ServerRequest) = delegate.route(request)
companion object : KLogging()
}