main #6
@@ -14,4 +14,12 @@ class BookingRepository {
|
||||
}
|
||||
return NetworkDataSource.getAvailableBookings(code)
|
||||
}
|
||||
|
||||
suspend fun createBooking(date: LocalDate, placeId: Long): Result<Boolean> {
|
||||
val code = DataStoreDataSource.getAuthCode()
|
||||
if (code.isEmpty() || code == "0") {
|
||||
return Result.failure(Exception("Auth code not found"))
|
||||
}
|
||||
return NetworkDataSource.createBooking(code, date, placeId)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,11 +5,16 @@ import io.ktor.client.HttpClient
|
||||
import io.ktor.client.engine.cio.CIO
|
||||
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
|
||||
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.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
|
||||
import kotlinx.serialization.Serializable
|
||||
import kotlinx.serialization.json.Json
|
||||
import ru.myitschool.work.core.Constants
|
||||
import ru.myitschool.work.data.entity.Employee
|
||||
@@ -141,5 +146,25 @@ object NetworkDataSource {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Serializable
|
||||
private data class CreateBookingBody(val date: String, val placeID: Long)
|
||||
|
||||
suspend fun createBooking(code: String, date: LocalDate, placeId: Long): Result<Boolean> = withContext(Dispatchers.IO) {
|
||||
return@withContext runCatching {
|
||||
// Формируем тело запроса
|
||||
val requestBody = CreateBookingBody(date.toString(), placeId)
|
||||
|
||||
val response = client.post(getUrl(code, Constants.BOOKING_URL)) { // Используем ту же константу BOOKING_URL
|
||||
contentType(ContentType.Application.Json)
|
||||
setBody(requestBody)
|
||||
}
|
||||
|
||||
when (response.status) {
|
||||
HttpStatusCode.OK -> true
|
||||
else -> error("Ошибка бронирования: ${response.bodyAsText()}")
|
||||
}
|
||||
}
|
||||
}
|
||||
private fun getUrl(code: String, targetUrl: String) = "${Constants.HOST}/api/$code$targetUrl"
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package ru.myitschool.work.domain.book
|
||||
|
||||
import ru.myitschool.work.data.repo.BookingRepository
|
||||
import java.time.LocalDate
|
||||
|
||||
class CreateBookingUseCase(
|
||||
private val repository: BookingRepository
|
||||
) {
|
||||
suspend operator fun invoke(date: LocalDate, placeId: Long): Result<Boolean> {
|
||||
return repository.createBooking(date, placeId)
|
||||
}
|
||||
}
|
||||
@@ -13,14 +13,14 @@ import kotlinx.coroutines.flow.update
|
||||
import kotlinx.coroutines.launch
|
||||
import ru.myitschool.work.data.entity.Place
|
||||
import ru.myitschool.work.data.repo.BookingRepository
|
||||
// import ru.myitschool.work.domain.book.CreateBookingUseCase
|
||||
import ru.myitschool.work.domain.book.CreateBookingUseCase
|
||||
import ru.myitschool.work.domain.book.GetAvailableBookingsUseCase
|
||||
import java.time.LocalDate
|
||||
|
||||
class BookViewModel : ViewModel() {
|
||||
private val repository by lazy { BookingRepository() }
|
||||
private val getAvailableBookingsUseCase by lazy { GetAvailableBookingsUseCase(repository) }
|
||||
// private val createBookingUseCase by lazy { CreateBookingUseCase(repository) }
|
||||
private val createBookingUseCase by lazy { CreateBookingUseCase(repository) }
|
||||
|
||||
|
||||
private val _uiState = MutableStateFlow<BookState>(BookState.Loading)
|
||||
@@ -111,10 +111,14 @@ class BookViewModel : ViewModel() {
|
||||
}
|
||||
|
||||
private fun bookPlace() {
|
||||
/*
|
||||
selectedPlaceId?.let { placeId ->
|
||||
// Раскомментируйте и измените этот блок
|
||||
val currentState = _uiState.value
|
||||
if (currentState is BookState.Data && currentState.selectedPlace != null && currentState.selectedDate != null) {
|
||||
val placeId = selectedPlaceId ?: return // Дополнительная проверка
|
||||
val date = currentState.selectedDate
|
||||
|
||||
viewModelScope.launch(Dispatchers.IO) {
|
||||
createBookingUseCase(placeId).fold(
|
||||
createBookingUseCase (date, placeId).fold(
|
||||
onSuccess = {
|
||||
_actionFlow.emit(BookAction.BookSuccess)
|
||||
},
|
||||
@@ -124,7 +128,7 @@ class BookViewModel : ViewModel() {
|
||||
}
|
||||
)
|
||||
}
|
||||
}*/
|
||||
}
|
||||
}
|
||||
|
||||
private fun refresh() {
|
||||
|
||||
Reference in New Issue
Block a user