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 package ru.myitschool.work.core
object Constants { 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 AUTH_URL = "/auth"
const val INFO_URL = "/info" const val INFO_URL = "/info"
const val BOOKING_URL = "/booking" 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.post
import io.ktor.client.request.setBody import io.ktor.client.request.setBody
import io.ktor.client.statement.bodyAsText 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.HttpStatusCode
import io.ktor.http.contentType
import io.ktor.serialization.kotlinx.json.json import io.ktor.serialization.kotlinx.json.json
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
@@ -145,6 +148,7 @@ object NetworkDataSource {
return withContext(Dispatchers.IO) { return withContext(Dispatchers.IO) {
runCatching { runCatching {
val response = client.post(getUrl(code, Constants.BOOK_URL)) { val response = client.post(getUrl(code, Constants.BOOK_URL)) {
contentType(ContentType.Application.Json)
setBody( setBody(
BookRequestDto( BookRequestDto(
date = date, date = date,

View File

@@ -2,11 +2,12 @@ package ru.myitschool.work.domain.booking
import ru.myitschool.work.data.repo.UserRepository import ru.myitschool.work.data.repo.UserRepository
class BookPlaceUseCase( class BookPlaceUseCase(
private val repository: UserRepository, private val repository: UserRepository,
) { ) {
suspend operator fun invoke(date: String, placeId: Int): Result<Unit> { suspend operator fun invoke(roomName: String, placeId: Int): Result<Unit> {
return repository.book(date, placeId) // Нужно получить дату из какого-то источника
// Либо изменить логику в 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 allGroups: List<DateGroup> = emptyList()
private var selectedDateIndex: Int = 0 private var selectedDateIndex: Int = 0
private var selectedPlaceId: Int? = null private var selectedSlotId: Int? = null
fun onIntent(intent: BookIntent) { fun onIntent(intent: BookIntent) {
when (intent) { when (intent) {
@@ -51,7 +51,7 @@ class BookViewModel : ViewModel() {
} }
is BookIntent.SelectPlace -> { is BookIntent.SelectPlace -> {
selectedPlaceId = intent.id selectedSlotId = intent.id
val current = _uiState.value val current = _uiState.value
if (current is BookState.Data) { if (current is BookState.Data) {
val updatedPlaces = current.places.map { item -> val updatedPlaces = current.places.map { item ->
@@ -77,7 +77,7 @@ class BookViewModel : ViewModel() {
if (bookings.isEmpty()) { if (bookings.isEmpty()) {
_uiState.value = BookState.Empty _uiState.value = BookState.Empty
allGroups = emptyList() allGroups = emptyList()
selectedPlaceId = null selectedSlotId = null
selectedDateIndex = 0 selectedDateIndex = 0
return@fold return@fold
} }
@@ -86,18 +86,18 @@ class BookViewModel : ViewModel() {
if (allGroups.isEmpty()) { if (allGroups.isEmpty()) {
_uiState.value = BookState.Empty _uiState.value = BookState.Empty
selectedPlaceId = null selectedSlotId = null
selectedDateIndex = 0 selectedDateIndex = 0
return@fold return@fold
} }
selectedDateIndex = 0 selectedDateIndex = 0
selectedPlaceId = null selectedSlotId = null
val firstGroup = allGroups[0] val firstGroup = allGroups[0]
val places = firstGroup.slots.mapIndexed { index, slot -> val places = firstGroup.slots.map { slot ->
BookPlaceItem( BookPlaceItem(
id = index, id = slot.id,
roomName = slot.roomName, roomName = slot.roomName,
time = slot.time, time = slot.time,
isSelected = false, isSelected = false,
@@ -124,10 +124,9 @@ class BookViewModel : ViewModel() {
if (index !in groups.indices) return if (index !in groups.indices) return
selectedDateIndex = index selectedDateIndex = index
selectedPlaceId = null selectedSlotId = null
val group = groups[index] val group = groups[index]
val places = group.slots.mapIndexed { idx, slot -> val places = group.slots.map { slot ->
BookPlaceItem( BookPlaceItem(
id = slot.id, id = slot.id,
roomName = slot.roomName, roomName = slot.roomName,
@@ -135,7 +134,6 @@ class BookViewModel : ViewModel() {
isSelected = false, isSelected = false,
) )
} }
val datesLabels = groups.map { it.label } val datesLabels = groups.map { it.label }
val current = _uiState.value val current = _uiState.value
@@ -158,11 +156,19 @@ class BookViewModel : ViewModel() {
val current = _uiState.value val current = _uiState.value
if (current !is BookState.Data) return if (current !is BookState.Data) return
val placeId = selectedPlaceId ?: return val slotId = selectedSlotId ?: return
val place = current.places.firstOrNull { it.id == placeId } ?: return
allGroups
.flatMap { it.slots }
.firstOrNull { it.id == slotId }
?: return
val selectedDate = allGroups[selectedDateIndex].date.toString()
viewModelScope.launch(Dispatchers.Default) { viewModelScope.launch(Dispatchers.Default) {
bookPlaceUseCase(place.roomName, place.id)
bookPlaceUseCase(selectedDate, slotId)
.fold( .fold(
onSuccess = { onSuccess = {
_actionFlow.emit(Action.CloseWithSuccess) _actionFlow.emit(Action.CloseWithSuccess)