@@ -2,22 +2,33 @@ package com.hexagontk.todo.backend.rest
2
2
3
3
import com.hexagonkt.converters.convert
4
4
import com.hexagonkt.core.media.APPLICATION_JSON
5
+ import com.hexagonkt.core.media.TEXT_PLAIN
5
6
import com.hexagonkt.http.model.ContentType
6
7
import com.hexagonkt.http.handlers.HttpContext
8
+ import com.hexagonkt.http.handlers.HttpController
9
+ import com.hexagonkt.http.handlers.HttpHandler
7
10
import com.hexagonkt.http.handlers.path
11
+ import com.hexagonkt.http.model.HttpMethod.*
12
+ import com.hexagonkt.http.server.callbacks.CorsCallback
13
+ import com.hexagonkt.http.server.callbacks.UrlCallback
8
14
import com.hexagonkt.serialization.jackson.json.Json
9
15
import com.hexagonkt.serialization.parseMap
10
16
import com.hexagonkt.serialization.serialize
11
17
import com.hexagontk.todo.backend.domain.model.Task
12
18
import com.hexagontk.todo.backend.domain.TaskStore
13
19
import java.util.UUID
14
20
15
- class Router (private val store : TaskStore ) {
16
- private val tasksHandler = path(" /tasks" ) {
21
+ class Router (private val store : TaskStore ) : HttpController {
22
+
23
+ private val json = ContentType (APPLICATION_JSON )
24
+ private val text = ContentType (TEXT_PLAIN )
25
+
26
+ private val tasksHandler: HttpHandler = path(" /tasks" ) {
17
27
get {
18
- val taskResponse =
19
- store.findAll().map { task -> task.convert(TaskRetrievalResponse ::class ) }
20
- ok(taskResponse)
28
+ val tasks = store.findAll()
29
+ val taskResponses = tasks.map { task -> task.convert(TaskRetrievalResponse ::class ) }
30
+
31
+ ok(taskResponses.serialize(Json ), contentType = json)
21
32
}
22
33
23
34
get(" /{id}" ) {
@@ -26,8 +37,8 @@ class Router(private val store: TaskStore) {
26
37
}
27
38
28
39
post {
29
- val taskCreationRequest =
30
- request. bodyString() .parseMap(Json ).convert(TaskCreationRequest ::class )
40
+ val bodyString = request.bodyString()
41
+ val taskCreationRequest = bodyString.parseMap(Json ).convert(TaskCreationRequest ::class )
31
42
val task = Task (
32
43
id = UUID .randomUUID().toString(),
33
44
title = taskCreationRequest.title,
@@ -39,16 +50,16 @@ class Router(private val store: TaskStore) {
39
50
40
51
patch(" /{id}" ) {
41
52
val id = pathParameters.getValue(" id" )
42
- val taskUpdateRequest =
43
- request. bodyString() .parseMap(Json ).convert(TaskUpdateRequest ::class )
53
+ val bodyString = request.bodyString()
54
+ val taskUpdateRequest = bodyString.parseMap(Json ).convert(TaskUpdateRequest ::class )
44
55
store.findOne(id) ? : notFound(" Task with id $id not found" )
45
56
val updates = mapOf (
46
57
Task ::title.name to taskUpdateRequest.title,
47
58
Task ::order.name to taskUpdateRequest.order,
48
59
Task ::completed.name to taskUpdateRequest.completed
49
60
)
50
61
if (store.updateOne(id, updates)) getTask(id, store)
51
- else badRequest(" Unable to update task with id $id " )
62
+ else badRequest(" Unable to update task with id $id " , contentType = text )
52
63
}
53
64
54
65
delete {
@@ -60,27 +71,27 @@ class Router(private val store: TaskStore) {
60
71
val id = pathParameters.getValue(" id" )
61
72
store.findOne(id) ? : notFound(" Task with id $id not found" )
62
73
if (store.deleteOne(id)) ok()
63
- else badRequest(" Unable to delete task with id $id " )
74
+ else badRequest(" Unable to delete task with id $id " , contentType = text )
64
75
}
65
76
}
66
77
67
- val handler = path {
68
- cors()
69
- after( " *" ) {
70
- send (
71
- body = response.body.serialize( Json ),
72
- contentType = ContentType ( APPLICATION_JSON )
78
+ override val handler: HttpHandler = path {
79
+ filter(
80
+ " *" ,
81
+ CorsCallback (
82
+ allowedMethods = setOf ( GET , POST , PATCH , DELETE , OPTIONS ),
83
+ allowedHeaders = setOf ( " Content-Type " )
73
84
)
74
- }
85
+ )
86
+
87
+ get(callback = UrlCallback (" classpath:static/index.html" ))
88
+
75
89
use(tasksHandler)
76
90
}
77
- }
78
91
79
- internal fun HttpContext.getTask (id : String , store : TaskStore ): HttpContext {
80
- val task = store.findOne(id)?.convert(TaskRetrievalResponse ::class )
81
- return if (task != null ) {
82
- ok(task)
83
- } else {
84
- notFound(" Task with id $id not found" )
92
+ private fun HttpContext.getTask (id : String , store : TaskStore ): HttpContext {
93
+ val task = store.findOne(id)?.convert(TaskRetrievalResponse ::class )
94
+ return if (task != null ) ok(task.serialize(Json ), contentType = json)
95
+ else notFound(" Task with id $id not found" , contentType = text)
85
96
}
86
97
}
0 commit comments