main #6

Closed
student-20690 wants to merge 20 commits from (deleted):main into main
4 changed files with 55 additions and 6 deletions
Showing only changes of commit 60198e13cb - Show all commits

View File

@@ -14,4 +14,12 @@ class BookingRepository {
} }
return NetworkDataSource.getAvailableBookings(code) 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)
}
} }

View File

@@ -5,11 +5,16 @@ import io.ktor.client.HttpClient
import io.ktor.client.engine.cio.CIO import io.ktor.client.engine.cio.CIO
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
import io.ktor.client.request.get 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.statement.bodyAsText
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
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json import kotlinx.serialization.json.Json
import ru.myitschool.work.core.Constants import ru.myitschool.work.core.Constants
import ru.myitschool.work.data.entity.Employee 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" private fun getUrl(code: String, targetUrl: String) = "${Constants.HOST}/api/$code$targetUrl"
} }

View File

@@ -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)
}
}

View File

@@ -13,14 +13,14 @@ import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import ru.myitschool.work.data.entity.Place import ru.myitschool.work.data.entity.Place
import ru.myitschool.work.data.repo.BookingRepository 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 ru.myitschool.work.domain.book.GetAvailableBookingsUseCase
import java.time.LocalDate import java.time.LocalDate
class BookViewModel : ViewModel() { class BookViewModel : ViewModel() {
private val repository by lazy { BookingRepository() } private val repository by lazy { BookingRepository() }
private val getAvailableBookingsUseCase by lazy { GetAvailableBookingsUseCase(repository) } 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) private val _uiState = MutableStateFlow<BookState>(BookState.Loading)
@@ -111,10 +111,14 @@ class BookViewModel : ViewModel() {
} }
private fun bookPlace() { 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) { viewModelScope.launch(Dispatchers.IO) {
createBookingUseCase(placeId).fold( createBookingUseCase (date, placeId).fold(
onSuccess = { onSuccess = {
_actionFlow.emit(BookAction.BookSuccess) _actionFlow.emit(BookAction.BookSuccess)
}, },
@@ -124,7 +128,7 @@ class BookViewModel : ViewModel() {
} }
) )
} }
}*/ }
} }
private fun refresh() { private fun refresh() {