forked from student-25326/NTO-2025-Android-TeamTask
data + domain
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
package ru.myitschool.work.data.repo
|
||||
|
||||
import ru.myitschool.work.data.source.NetworkDataSource
|
||||
import ru.myitschool.work.domain.MyResult
|
||||
import ru.myitschool.work.domain.booking.BookingData
|
||||
|
||||
object BookingRepository {
|
||||
suspend fun getAvailableForBooking(code: String): MyResult<BookingData> {
|
||||
return when (val result = NetworkDataSource.getAvailableForBooking(code)) {
|
||||
is MyResult.Success -> {
|
||||
val bookingData = BookingData(
|
||||
dateToPlaces = result.data.dates
|
||||
)
|
||||
MyResult.Success(bookingData)
|
||||
}
|
||||
is MyResult.Error -> {
|
||||
MyResult.Error(result.error)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun createBooking(code: String, date: String, placeId: Int): MyResult<Unit> {
|
||||
return NetworkDataSource.createBooking(code, date, placeId)
|
||||
}
|
||||
}
|
||||
@@ -5,12 +5,18 @@ import io.ktor.client.call.body
|
||||
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.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.json.Json
|
||||
import ru.myitschool.work.core.Constants
|
||||
import ru.myitschool.work.data.dto.AvailableBookingResponse
|
||||
import ru.myitschool.work.data.dto.BookRequest
|
||||
import ru.myitschool.work.data.dto.UserInfoResponse
|
||||
import ru.myitschool.work.domain.MyResult
|
||||
|
||||
@@ -60,5 +66,40 @@ object NetworkDataSource {
|
||||
MyResult.Error("Сетевая ошибка")
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun getAvailableForBooking(code: String): MyResult<AvailableBookingResponse> = withContext(Dispatchers.IO) {
|
||||
try {
|
||||
val response = client.get(getUrl(code, Constants.BOOKING_URL))
|
||||
when (response.status) {
|
||||
HttpStatusCode.OK -> {
|
||||
MyResult.Success(response.body())
|
||||
}
|
||||
else -> {
|
||||
MyResult.Error("Ошибка получения доступных мест")
|
||||
}
|
||||
}
|
||||
} catch (e: Exception){
|
||||
MyResult.Error("Сетевая ошибка")
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun createBooking(code: String, date: String, placeId: Int): MyResult<Unit> = withContext(Dispatchers.IO){
|
||||
try {
|
||||
val response = client.post(getUrl(code, Constants.BOOK_URL)) {
|
||||
contentType(ContentType.Application.Json)
|
||||
setBody(BookRequest(date = date, placeID = placeId))
|
||||
}
|
||||
when (response.status) {
|
||||
HttpStatusCode.OK -> {
|
||||
MyResult.Success(Unit)
|
||||
}
|
||||
else -> {
|
||||
MyResult.Error("Ошибка бронирования")
|
||||
}
|
||||
}
|
||||
} catch (e: Exception){
|
||||
MyResult.Error("Сетевая ошибка")
|
||||
}
|
||||
}
|
||||
private fun getUrl(code: String, targetUrl: String) = "${Constants.HOST}/api/$code$targetUrl"
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package ru.myitschool.work.domain.booking
|
||||
|
||||
import ru.myitschool.work.data.dto.AvailablePlace
|
||||
|
||||
data class BookingData(
|
||||
val dateToPlaces: Map<String, List<AvailablePlace>>
|
||||
){
|
||||
fun getSortedDatesWithPlaces(): List<String> {
|
||||
return dateToPlaces.keys
|
||||
.filter { dateToPlaces[it]?.isNotEmpty() == true }
|
||||
.sorted()
|
||||
}
|
||||
|
||||
fun getPlacesForDate(date: String): List<AvailablePlace> {
|
||||
return dateToPlaces[date] ?: emptyList()
|
||||
}
|
||||
|
||||
fun hasAvailableDates(): Boolean {
|
||||
return dateToPlaces.any { it.value.isNotEmpty() }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@ import androidx.navigation.compose.NavHost
|
||||
import androidx.navigation.compose.composable
|
||||
import androidx.navigation.compose.rememberNavController
|
||||
import ru.myitschool.work.ui.auth.AuthScreen
|
||||
import ru.myitschool.work.ui.booking.BookingScreen
|
||||
import ru.myitschool.work.ui.main.MainScreen
|
||||
import ru.myitschool.work.util.DataStoreManager
|
||||
|
||||
@@ -49,11 +50,7 @@ fun AppNavHost(
|
||||
MainScreen(navController = navController)
|
||||
}
|
||||
composable<Screen.Book> {
|
||||
Box(
|
||||
contentAlignment = Alignment.Center
|
||||
) {
|
||||
Text(text = "Hello")
|
||||
}
|
||||
BookingScreen(navController = navController)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,6 @@ data class MainState(
|
||||
data class BookingDisplayItem(
|
||||
val date: String, // dd.MM.yyyy
|
||||
val place: String,
|
||||
val originalDate: String
|
||||
val originalDate: String //для сортировки
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user