forked from Olympic/NTO-2025-Android-TeamTask
booking fix (1)
This commit is contained in:
@@ -137,9 +137,23 @@ object NetworkDataSource {
|
|||||||
return@withContext runCatching {
|
return@withContext runCatching {
|
||||||
val response = client.get(getUrl(code, Constants.BOOKING_URL))
|
val response = client.get(getUrl(code, Constants.BOOKING_URL))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
when (response.status) {
|
when (response.status) {
|
||||||
HttpStatusCode.OK -> response.body<BookingInfoResponse>()
|
HttpStatusCode.OK -> {
|
||||||
HttpStatusCode.NoContent -> BookingInfoResponse(emptyMap())
|
try {
|
||||||
|
val body = response.body<BookingInfoResponse>()
|
||||||
|
println("Parsed response: $body")
|
||||||
|
body
|
||||||
|
} catch (e: Exception) {
|
||||||
|
println("Parsing error: ${e.message}")
|
||||||
|
BookingInfoResponse(emptyMap())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
HttpStatusCode.NoContent -> {
|
||||||
|
println("No content received")
|
||||||
|
BookingInfoResponse(emptyMap())
|
||||||
|
}
|
||||||
else -> {
|
else -> {
|
||||||
val errorMsg = response.body<String>().ifBlank {
|
val errorMsg = response.body<String>().ifBlank {
|
||||||
"Ошибка загрузки данных: ${response.status}"
|
"Ошибка загрузки данных: ${response.status}"
|
||||||
@@ -149,7 +163,6 @@ object NetworkDataSource {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun book(code: String, date: String, placeId: Int): Result<Unit> = withContext(Dispatchers.IO) {
|
suspend fun book(code: String, date: String, placeId: Int): Result<Unit> = withContext(Dispatchers.IO) {
|
||||||
return@withContext runCatching {
|
return@withContext runCatching {
|
||||||
val response = client.post(getUrl(code, Constants.BOOK_URL)) {
|
val response = client.post(getUrl(code, Constants.BOOK_URL)) {
|
||||||
|
|||||||
@@ -74,8 +74,8 @@ class BookViewModel(private val bookRepo: BookRepository) : ViewModel() {
|
|||||||
val datesMap = response.dates ?: emptyMap()
|
val datesMap = response.dates ?: emptyMap()
|
||||||
|
|
||||||
val dateItems = datesMap.entries
|
val dateItems = datesMap.entries
|
||||||
.filter { (_, places) -> places.isNotEmpty() }
|
|
||||||
.sortedBy { (date, _) -> parseDate(date) }
|
.sortedBy { (date, _) -> parseDate(date) }
|
||||||
|
.filter { (_, places) -> places.isNotEmpty() }
|
||||||
.map { (dateString, places) ->
|
.map { (dateString, places) ->
|
||||||
DateItem(
|
DateItem(
|
||||||
id = dateString,
|
id = dateString,
|
||||||
@@ -89,7 +89,6 @@ class BookViewModel(private val bookRepo: BookRepository) : ViewModel() {
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (dateItems.isEmpty()) {
|
if (dateItems.isEmpty()) {
|
||||||
_uiState.update { BookState.Empty }
|
_uiState.update { BookState.Empty }
|
||||||
} else {
|
} else {
|
||||||
@@ -102,6 +101,7 @@ class BookViewModel(private val bookRepo: BookRepository) : ViewModel() {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
onFailure = { error ->
|
onFailure = { error ->
|
||||||
|
println("Error loading bookings: ${error.message}")
|
||||||
_uiState.update {
|
_uiState.update {
|
||||||
BookState.Data(
|
BookState.Data(
|
||||||
error = error.message ?: "Ошибка загрузки данных"
|
error = error.message ?: "Ошибка загрузки данных"
|
||||||
|
|||||||
Reference in New Issue
Block a user