Initial commit
This commit is contained in:
65
src/main/kotlin/Routing.kt
Executable file
65
src/main/kotlin/Routing.kt
Executable file
@@ -0,0 +1,65 @@
|
||||
package com.example
|
||||
|
||||
import com.example.dto.ErrorDto
|
||||
import com.example.dto.UserDto
|
||||
import com.example.dto.UserDto.BookingDto
|
||||
import io.ktor.http.HttpStatusCode
|
||||
import io.ktor.http.content.PartData
|
||||
import io.ktor.http.content.forEachPart
|
||||
import io.ktor.server.application.*
|
||||
import io.ktor.server.request.receiveMultipart
|
||||
import io.ktor.server.response.*
|
||||
import io.ktor.server.routing.*
|
||||
|
||||
val booking = mutableListOf(
|
||||
BookingDto(
|
||||
room = "502.6",
|
||||
time = "10:00 - 18:00"
|
||||
),
|
||||
BookingDto(
|
||||
room = "504.6",
|
||||
time = "12:00 - 16:00"
|
||||
),
|
||||
)
|
||||
|
||||
fun Application.configureRouting() {
|
||||
routing {
|
||||
get("/user") {
|
||||
call.respond(UserDto(booking = booking))
|
||||
}
|
||||
post("/book") {
|
||||
var room = ""
|
||||
var time = ""
|
||||
val multipartData = call.receiveMultipart()
|
||||
|
||||
multipartData.forEachPart { part ->
|
||||
try {
|
||||
if (part !is PartData.FormItem) return@forEachPart
|
||||
when (part.name) {
|
||||
"room" -> {
|
||||
room = part.value.trim()
|
||||
}
|
||||
|
||||
"time" -> {
|
||||
time = part.value.trim()
|
||||
}
|
||||
|
||||
else -> Unit
|
||||
}
|
||||
} finally {
|
||||
part.dispose()
|
||||
}
|
||||
}
|
||||
|
||||
if (room.isEmpty() && time.isEmpty()) {
|
||||
call.respond(
|
||||
HttpStatusCode.BadRequest,
|
||||
ErrorDto(error = "Field is empty")
|
||||
)
|
||||
} else {
|
||||
booking.add(BookingDto(room = room, time = time))
|
||||
call.respond(HttpStatusCode.OK)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user