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