58 lines
1.8 KiB
Kotlin
Executable File
58 lines
1.8 KiB
Kotlin
Executable File
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.request.receiveParameters
|
|
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") до закрывающей скобки):
|
|
post("/book") {
|
|
try {
|
|
// Получаем данные в формате x-www-form-urlencoded
|
|
val parameters = call.receiveParameters()
|
|
val room = parameters["room"]?.trim() ?: ""
|
|
val time = parameters["time"]?.trim() ?: ""
|
|
|
|
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))
|
|
}
|
|
} catch (e: Exception) {
|
|
call.respond(
|
|
HttpStatusCode.BadRequest,
|
|
ErrorDto(error = "Invalid request format")
|
|
)
|
|
}
|
|
}
|
|
}
|
|
}
|