1 #1
@@ -1,8 +1,11 @@
|
|||||||
package com.example
|
package com.example
|
||||||
|
|
||||||
|
|
||||||
import com.example.dto.ErrorDto
|
import com.example.dto.ErrorDto
|
||||||
import com.example.dto.UserDto
|
import com.example.dto.UserDto
|
||||||
import com.example.dto.UserDto.BookingDto
|
import com.example.dto.UserDto.BookingDto
|
||||||
|
import domain.usecase.AddBookingUseCase
|
||||||
|
import domain.usecase.GetBookingsUseCase
|
||||||
import io.ktor.http.HttpStatusCode
|
import io.ktor.http.HttpStatusCode
|
||||||
import io.ktor.http.content.PartData
|
import io.ktor.http.content.PartData
|
||||||
import io.ktor.http.content.forEachPart
|
import io.ktor.http.content.forEachPart
|
||||||
@@ -10,6 +13,7 @@ import io.ktor.server.application.*
|
|||||||
import io.ktor.server.request.receiveMultipart
|
import io.ktor.server.request.receiveMultipart
|
||||||
import io.ktor.server.response.*
|
import io.ktor.server.response.*
|
||||||
import io.ktor.server.routing.*
|
import io.ktor.server.routing.*
|
||||||
|
import repository.InMemoryBookingRepository
|
||||||
|
|
||||||
val booking = mutableListOf(
|
val booking = mutableListOf(
|
||||||
BookingDto(
|
BookingDto(
|
||||||
@@ -23,10 +27,20 @@ val booking = mutableListOf(
|
|||||||
)
|
)
|
||||||
|
|
||||||
fun Application.configureRouting() {
|
fun Application.configureRouting() {
|
||||||
|
val repository = InMemoryBookingRepository()
|
||||||
|
val getBookingsUseCase = GetBookingsUseCase(repository)
|
||||||
|
val addBookingUseCase = AddBookingUseCase(repository)
|
||||||
|
|
||||||
routing {
|
routing {
|
||||||
get("/user") {
|
get("/user") {
|
||||||
call.respond(UserDto(booking = booking))
|
val bookings = getBookingsUseCase()
|
||||||
|
val userDto = UserDto(
|
||||||
|
name = "Administrator",
|
||||||
|
booking = bookings.map { UserDto.BookingDto(it.room, it.time) }
|
||||||
|
)
|
||||||
|
call.respond(userDto)
|
||||||
}
|
}
|
||||||
|
|
||||||
post("/book") {
|
post("/book") {
|
||||||
var room = ""
|
var room = ""
|
||||||
var time = ""
|
var time = ""
|
||||||
@@ -34,31 +48,25 @@ fun Application.configureRouting() {
|
|||||||
|
|
||||||
multipartData.forEachPart { part ->
|
multipartData.forEachPart { part ->
|
||||||
try {
|
try {
|
||||||
if (part !is PartData.FormItem) return@forEachPart
|
if (part is PartData.FormItem) {
|
||||||
when (part.name) {
|
when (part.name) {
|
||||||
"room" -> {
|
"room" -> room = part.value
|
||||||
room = part.value.trim()
|
"time" -> time = part.value
|
||||||
}
|
}
|
||||||
|
|
||||||
"time" -> {
|
|
||||||
time = part.value.trim()
|
|
||||||
}
|
|
||||||
|
|
||||||
else -> Unit
|
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
part.dispose()
|
part.dispose()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (room.isEmpty() && time.isEmpty()) {
|
val result = addBookingUseCase(room, time)
|
||||||
|
if (result.isSuccess) {
|
||||||
|
call.respond(HttpStatusCode.OK)
|
||||||
|
} else {
|
||||||
call.respond(
|
call.respond(
|
||||||
HttpStatusCode.BadRequest,
|
HttpStatusCode.BadRequest,
|
||||||
ErrorDto(error = "Field is empty")
|
ErrorDto("Field is empty")
|
||||||
)
|
)
|
||||||
} else {
|
|
||||||
booking.add(BookingDto(room = room, time = time))
|
|
||||||
call.respond(HttpStatusCode.OK)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
6
src/main/kotlin/domain/model/Booking.kt
Normal file
6
src/main/kotlin/domain/model/Booking.kt
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
package domain.model
|
||||||
|
|
||||||
|
data class Booking(
|
||||||
|
val room: String,
|
||||||
|
val time: String
|
||||||
|
)
|
||||||
27
src/main/kotlin/domain/usecase/BookingUseCase.kt
Normal file
27
src/main/kotlin/domain/usecase/BookingUseCase.kt
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
package domain.usecase
|
||||||
|
import domain.model.Booking
|
||||||
|
|
||||||
|
interface BookingRepository {
|
||||||
|
fun getAllBookings(): List<Booking>
|
||||||
|
fun addBooking(booking: Booking)
|
||||||
|
}
|
||||||
|
|
||||||
|
class GetBookingsUseCase(
|
||||||
|
private val repository: BookingRepository
|
||||||
|
) {
|
||||||
|
operator fun invoke(): List<Booking> {
|
||||||
|
return repository.getAllBookings()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class AddBookingUseCase(
|
||||||
|
private val repository: BookingRepository
|
||||||
|
) {
|
||||||
|
operator fun invoke(room: String, time: String): Result<Unit> {
|
||||||
|
if (room.isBlank() || time.isBlank()) {
|
||||||
|
return Result.failure(IllegalArgumentException("Room and time must not be empty"))
|
||||||
|
}
|
||||||
|
repository.addBooking(Booking(room.trim(), time.trim()))
|
||||||
|
return Result.success(Unit)
|
||||||
|
}
|
||||||
|
}
|
||||||
17
src/main/kotlin/repository/InMemoryBookingRepository.kt
Normal file
17
src/main/kotlin/repository/InMemoryBookingRepository.kt
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
package repository
|
||||||
|
|
||||||
|
import domain.model.Booking
|
||||||
|
import domain.usecase.BookingRepository
|
||||||
|
|
||||||
|
class InMemoryBookingRepository : BookingRepository {
|
||||||
|
private val bookings = mutableListOf<Booking>().apply {
|
||||||
|
add(Booking("502.6", "10:00 - 18:00"))
|
||||||
|
add(Booking("504.6", "12:00 - 16:00"))
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getAllBookings(): List<Booking> = bookings.toList()
|
||||||
|
|
||||||
|
override fun addBooking(booking: Booking) {
|
||||||
|
bookings.add(booking)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user