UI/UX solution:

fix POST booking request
This commit is contained in:
2025-12-05 22:19:51 +07:00
parent dbe735e541
commit b40749faa1
4 changed files with 29 additions and 18 deletions

View File

@@ -1,7 +1,7 @@
package ru.myitschool.work.core
object Constants {
const val HOST = "http://192.168.1.46:8080"
const val HOST = "http://10.0.2.2:8080"
const val AUTH_URL = "/auth"
const val INFO_URL = "/info"
const val BOOKING_URL = "/booking"

View File

@@ -8,7 +8,10 @@ import io.ktor.client.request.get
import io.ktor.client.request.post
import io.ktor.client.request.setBody
import io.ktor.client.statement.bodyAsText
import io.ktor.client.utils.EmptyContent.contentType
import io.ktor.http.ContentType
import io.ktor.http.HttpStatusCode
import io.ktor.http.contentType
import io.ktor.serialization.kotlinx.json.json
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
@@ -145,6 +148,7 @@ object NetworkDataSource {
return withContext(Dispatchers.IO) {
runCatching {
val response = client.post(getUrl(code, Constants.BOOK_URL)) {
contentType(ContentType.Application.Json)
setBody(
BookRequestDto(
date = date,

View File

@@ -2,11 +2,12 @@ package ru.myitschool.work.domain.booking
import ru.myitschool.work.data.repo.UserRepository
class BookPlaceUseCase(
private val repository: UserRepository,
) {
suspend operator fun invoke(date: String, placeId: Int): Result<Unit> {
return repository.book(date, placeId)
suspend operator fun invoke(roomName: String, placeId: Int): Result<Unit> {
// Нужно получить дату из какого-то источника
// Либо изменить логику в ViewModel
return repository.book(roomName, placeId) // ← но repository.book ожидает date, а не roomName
}
}

View File

@@ -37,7 +37,7 @@ class BookViewModel : ViewModel() {
private var allGroups: List<DateGroup> = emptyList()
private var selectedDateIndex: Int = 0
private var selectedPlaceId: Int? = null
private var selectedSlotId: Int? = null
fun onIntent(intent: BookIntent) {
when (intent) {
@@ -51,7 +51,7 @@ class BookViewModel : ViewModel() {
}
is BookIntent.SelectPlace -> {
selectedPlaceId = intent.id
selectedSlotId = intent.id
val current = _uiState.value
if (current is BookState.Data) {
val updatedPlaces = current.places.map { item ->
@@ -77,7 +77,7 @@ class BookViewModel : ViewModel() {
if (bookings.isEmpty()) {
_uiState.value = BookState.Empty
allGroups = emptyList()
selectedPlaceId = null
selectedSlotId = null
selectedDateIndex = 0
return@fold
}
@@ -86,18 +86,18 @@ class BookViewModel : ViewModel() {
if (allGroups.isEmpty()) {
_uiState.value = BookState.Empty
selectedPlaceId = null
selectedSlotId = null
selectedDateIndex = 0
return@fold
}
selectedDateIndex = 0
selectedPlaceId = null
selectedSlotId = null
val firstGroup = allGroups[0]
val places = firstGroup.slots.mapIndexed { index, slot ->
val places = firstGroup.slots.map { slot ->
BookPlaceItem(
id = index,
id = slot.id,
roomName = slot.roomName,
time = slot.time,
isSelected = false,
@@ -124,10 +124,9 @@ class BookViewModel : ViewModel() {
if (index !in groups.indices) return
selectedDateIndex = index
selectedPlaceId = null
selectedSlotId = null
val group = groups[index]
val places = group.slots.mapIndexed { idx, slot ->
val places = group.slots.map { slot ->
BookPlaceItem(
id = slot.id,
roomName = slot.roomName,
@@ -135,7 +134,6 @@ class BookViewModel : ViewModel() {
isSelected = false,
)
}
val datesLabels = groups.map { it.label }
val current = _uiState.value
@@ -158,11 +156,19 @@ class BookViewModel : ViewModel() {
val current = _uiState.value
if (current !is BookState.Data) return
val placeId = selectedPlaceId ?: return
val place = current.places.firstOrNull { it.id == placeId } ?: return
val slotId = selectedSlotId ?: return
allGroups
.flatMap { it.slots }
.firstOrNull { it.id == slotId }
?: return
val selectedDate = allGroups[selectedDateIndex].date.toString()
viewModelScope.launch(Dispatchers.Default) {
bookPlaceUseCase(place.roomName, place.id)
bookPlaceUseCase(selectedDate, slotId)
.fold(
onSuccess = {
_actionFlow.emit(Action.CloseWithSuccess)