123
This commit is contained in:
@@ -8,6 +8,7 @@ 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.request.receiveParameters
|
||||
import io.ktor.server.response.*
|
||||
import io.ktor.server.routing.*
|
||||
|
||||
@@ -27,38 +28,29 @@ fun Application.configureRouting() {
|
||||
get("/user") {
|
||||
call.respond(UserDto(booking = booking))
|
||||
}
|
||||
|
||||
// ЗАМЕНИТЕ ЭТОТ БЛОК (от post("/book") до закрывающей скобки):
|
||||
post("/book") {
|
||||
var room = ""
|
||||
var time = ""
|
||||
val multipartData = call.receiveMultipart()
|
||||
try {
|
||||
// Получаем данные в формате x-www-form-urlencoded
|
||||
val parameters = call.receiveParameters()
|
||||
val room = parameters["room"]?.trim() ?: ""
|
||||
val time = parameters["time"]?.trim() ?: ""
|
||||
|
||||
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 = "Fields 'room' and 'time' are required")
|
||||
)
|
||||
} else {
|
||||
booking.add(BookingDto(room = room, time = time))
|
||||
call.respond(HttpStatusCode.OK, mapOf("success" to true))
|
||||
}
|
||||
}
|
||||
|
||||
if (room.isEmpty() && time.isEmpty()) {
|
||||
} catch (e: Exception) {
|
||||
call.respond(
|
||||
HttpStatusCode.BadRequest,
|
||||
ErrorDto(error = "Field is empty")
|
||||
ErrorDto(error = "Invalid request format")
|
||||
)
|
||||
} else {
|
||||
booking.add(BookingDto(room = room, time = time))
|
||||
call.respond(HttpStatusCode.OK)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user