forked from Olympic/NTO-2025-Client-Android-backend
Initial commit
This commit is contained in:
16
src/main/kotlin/Application.kt
Executable file
16
src/main/kotlin/Application.kt
Executable file
@@ -0,0 +1,16 @@
|
||||
package com.example
|
||||
|
||||
import io.ktor.serialization.kotlinx.json.json
|
||||
import io.ktor.server.application.*
|
||||
import io.ktor.server.plugins.contentnegotiation.ContentNegotiation
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
io.ktor.server.netty.EngineMain.main(args)
|
||||
}
|
||||
|
||||
fun Application.module() {
|
||||
install(ContentNegotiation) {
|
||||
json()
|
||||
}
|
||||
configureRouting()
|
||||
}
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
10
src/main/kotlin/dto/ErrorDto.kt
Normal file
10
src/main/kotlin/dto/ErrorDto.kt
Normal file
@@ -0,0 +1,10 @@
|
||||
package com.example.dto
|
||||
|
||||
import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
class ErrorDto(
|
||||
@SerialName("error")
|
||||
val error: String,
|
||||
)
|
||||
20
src/main/kotlin/dto/UserDto.kt
Normal file
20
src/main/kotlin/dto/UserDto.kt
Normal file
@@ -0,0 +1,20 @@
|
||||
package com.example.dto
|
||||
|
||||
import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
data class UserDto(
|
||||
@SerialName("name")
|
||||
val name: String = "Administrator",
|
||||
@SerialName("booking")
|
||||
val booking: List<BookingDto>
|
||||
) {
|
||||
@Serializable
|
||||
data class BookingDto(
|
||||
@SerialName("room")
|
||||
val room: String,
|
||||
@SerialName("time")
|
||||
val time: String,
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user