@@ -11,8 +11,12 @@ package infrastructure.api
11
11
import application.controller.RoomController
12
12
import application.presenter.api.deserializer.ApiDeserializer.toRoom
13
13
import application.presenter.api.model.RoomApiDto
14
+ import application.presenter.api.serializer.ApiSerializer.toRoomApiDto
14
15
import application.service.Service
16
+ import entity.zone.RoomID
15
17
import infrastructure.provider.ManagerProvider
18
+ import io.ktor.http.HttpHeaders
19
+ import io.ktor.http.HttpStatusCode
16
20
import io.ktor.serialization.kotlinx.json.json
17
21
import io.ktor.server.application.Application
18
22
import io.ktor.server.application.call
@@ -21,6 +25,8 @@ import io.ktor.server.engine.embeddedServer
21
25
import io.ktor.server.netty.Netty
22
26
import io.ktor.server.plugins.contentnegotiation.ContentNegotiation
23
27
import io.ktor.server.request.receive
28
+ import io.ktor.server.response.header
29
+ import io.ktor.server.response.respond
24
30
import io.ktor.server.response.respondText
25
31
import io.ktor.server.routing.delete
26
32
import io.ktor.server.routing.get
@@ -37,7 +43,7 @@ class APIController(private val provider: ManagerProvider) {
37
43
* Starts the http server to serve the client requests.
38
44
*/
39
45
fun start () {
40
- embeddedServer(Netty , port = 3000 ) {
46
+ embeddedServer(Netty , port = port ) {
41
47
dispatcher(this )
42
48
install(ContentNegotiation ) {
43
49
json()
@@ -73,14 +79,42 @@ class APIController(private val provider: ManagerProvider) {
73
79
2. creo il digital twin su Azure Digital Twins
74
80
*/
75
81
val room = call.receive<RoomApiDto >().toRoom()
76
- Service .CreateRoom (room, RoomController (provider.roomDigitalTwinManager))
77
- call.respondText(" [${Thread .currentThread().name} ] Room POST! \n $room " )
82
+ Service .CreateRoom (room, RoomController (provider.roomDigitalTwinManager)).execute().apply {
83
+ when (this ) {
84
+ null -> call.respond(HttpStatusCode .Conflict )
85
+ else -> {
86
+ call.response.header(
87
+ HttpHeaders .Location ,
88
+ " http://localhost:$port$apiPath /rooms/${room.id.value} "
89
+ )
90
+ call.respond(HttpStatusCode .Created )
91
+ }
92
+ }
93
+ }
94
+ }
95
+ get(" $apiPath /rooms" ) {
96
+ call.respondText(" Get Rooms CALLED" )
78
97
}
79
98
get(" $apiPath /rooms/{roomId}" ) {
80
- call.respondText(" [${Thread .currentThread().name} ] Room GET!" )
99
+ Service .GetRoom (
100
+ RoomID (call.parameters[" roomId" ].orEmpty()),
101
+ RoomController (provider.roomDigitalTwinManager)
102
+ ).execute().apply {
103
+ when (this ) {
104
+ null -> call.respond(HttpStatusCode .NotFound )
105
+ else -> call.respond(this .toRoomApiDto())
106
+ }
107
+ }
81
108
}
82
109
delete(" $apiPath /rooms/{roomId}" ) {
83
- call.respondText(" [${Thread .currentThread().name} ] Room DELETE!" )
110
+ call.respond(
111
+ Service .DeleteRoom (
112
+ RoomID (call.parameters[" roomId" ].orEmpty()),
113
+ RoomController (provider.roomDigitalTwinManager)
114
+ ).execute().let { result ->
115
+ if (result) HttpStatusCode .NoContent else HttpStatusCode .NotFound
116
+ }
117
+ )
84
118
}
85
119
}
86
120
}
@@ -110,6 +144,7 @@ class APIController(private val provider: ManagerProvider) {
110
144
}
111
145
112
146
companion object {
147
+ private const val port = 3000
113
148
private const val apiVersion = " v1"
114
149
private const val apiPath = " /api/$apiVersion "
115
150
}
0 commit comments