diff --git a/app/src/main/java/ru/ulstu/is/airticketrentservice/TicketApplication.kt b/app/src/main/java/ru/ulstu/is/airticketrentservice/TicketApplication.kt index 3d39116..d2559ea 100644 --- a/app/src/main/java/ru/ulstu/is/airticketrentservice/TicketApplication.kt +++ b/app/src/main/java/ru/ulstu/is/airticketrentservice/TicketApplication.kt @@ -1,6 +1,7 @@ package ru.ulstu.`is`.airticketrentservice import android.app.Application +import ru.ulstu.`is`.airticketrentservice.api.AppService import ru.ulstu.`is`.airticketrentservice.database.AppContainer import ru.ulstu.`is`.airticketrentservice.database.AppDataContainer diff --git a/app/src/main/java/ru/ulstu/is/airticketrentservice/api/AppService.kt b/app/src/main/java/ru/ulstu/is/airticketrentservice/api/AppService.kt index 540fc8c..e0dd443 100644 --- a/app/src/main/java/ru/ulstu/is/airticketrentservice/api/AppService.kt +++ b/app/src/main/java/ru/ulstu/is/airticketrentservice/api/AppService.kt @@ -28,14 +28,19 @@ interface AppService { @Query("_page") page: Int, @Query("_limit") limit: Int, ): List - @GET("findFlights/{direction_from}-{direction_to}-{departure_date}") - suspend fun findFlights( - @Path("direction_from") from: String, - @Path("direction_to") to: String, - @Path("departure_date") departureDate: String + @GET("searchFlights") + suspend fun foundFlights( ): List +// suspend fun findFlights( +// @Path("direction_from") from: String, +// @Path("direction_to") to: String, +// @Path("departure_date") departureDate: String +// ): List @GET("tickets") suspend fun getAllTickets(): List + + @GET("foundFlights") + suspend fun getAllFlights(): List @GET("tickets") suspend fun getTickets( @Query("_page") page: Int, @@ -126,7 +131,7 @@ interface AppService { @Path("id") id: Int, ): TicketRemote - companion object { + companion object : AppService { private const val BASE_URL = "http://192.168.1.100:8079/" @Volatile diff --git a/app/src/main/java/ru/ulstu/is/airticketrentservice/api/model/FlightRemote.kt b/app/src/main/java/ru/ulstu/is/airticketrentservice/api/model/FlightRemote.kt index 821e770..131b0d7 100644 --- a/app/src/main/java/ru/ulstu/is/airticketrentservice/api/model/FlightRemote.kt +++ b/app/src/main/java/ru/ulstu/is/airticketrentservice/api/model/FlightRemote.kt @@ -1,7 +1,9 @@ package ru.ulstu.`is`.airticketrentservice.api.model import kotlinx.serialization.Serializable +import kotlinx.serialization.json.Json import ru.ulstu.`is`.airticketrentservice.database.models.Flight +import java.io.File @Serializable data class FlightRemote( @@ -32,4 +34,22 @@ fun Flight.toFlightRemote(): FlightRemote = FlightRemote( arrival_date, tickets_count, one_ticket_cost -) \ No newline at end of file +) + +fun foundFlights( + from: String, + to: String, + departureDate: String +): List { + val json = File("C:\\Users\\User\\AndroidStudioProjects\\AirTicketRentService\\server").readText() + + val flights = Json.decodeFromString>(json) + + val matchingFlights = flights.filter { flight -> + flight.direction_from == from || + flight.direction_to == to || + flight.departure_date == departureDate + } + + return matchingFlights +} \ No newline at end of file diff --git a/app/src/main/java/ru/ulstu/is/airticketrentservice/api/repository/RestFlightRepository.kt b/app/src/main/java/ru/ulstu/is/airticketrentservice/api/repository/RestFlightRepository.kt index 835559f..cefe4d0 100644 --- a/app/src/main/java/ru/ulstu/is/airticketrentservice/api/repository/RestFlightRepository.kt +++ b/app/src/main/java/ru/ulstu/is/airticketrentservice/api/repository/RestFlightRepository.kt @@ -14,6 +14,7 @@ import retrofit2.Call import retrofit2.Callback import retrofit2.Response import androidx.lifecycle.viewModelScope +import kotlinx.serialization.json.Json import ru.ulstu.`is`.airticketrentservice.api.AppService import ru.ulstu.`is`.airticketrentservice.api.model.toFlight import ru.ulstu.`is`.airticketrentservice.api.model.toFlightRemote @@ -25,6 +26,7 @@ import ru.ulstu.`is`.airticketrentservice.database.repository.FlightRepository import ru.ulstu.`is`.airticketrentservice.database.repository.OfflineRemoteKeyRepository import ru.ulstu.`is`.airticketrentservice.api.mediator.FlightRemoteMediator import ru.ulstu.`is`.airticketrentservice.api.model.FlightRemote +import java.io.File import kotlin.coroutines.resume import kotlin.coroutines.resumeWithException import kotlin.coroutines.suspendCoroutine @@ -59,24 +61,46 @@ class RestFlightRepository( override suspend fun getFlightById(flightId: Int): Flight = service.getFlight(flightId).toFlight() - override suspend fun findFlights( + override suspend fun foundFlights( from: String, to: String, departureDate: String ): List { - Log.d(RestFlightRepository::class.simpleName, "Find flights") - val existFlights = dbFlightRepository.findFlights(from, to, departureDate).associateBy { it.id }.toMutableMap() + val existFlights = dbFlightRepository.getAllFlights().associateBy { it.id }.toMutableMap() - service.findFlights(from, to, departureDate) + service.foundFlights() .map { it.toFlight() } .forEach { flight -> + flight.direction_from == from || + flight.direction_to == to || + flight.departure_date == departureDate existFlights[flight.id] = flight } return existFlights.map { it.value }.sortedBy { it.id } + } +// override suspend fun findFlights( +// from: String, +// to: String, +// departureDate: String +// ): List { +// Log.d(RestFlightRepository::class.simpleName, "Find flights") +// +// +// val existFlights = dbFlightRepository.findFlights(from, to, departureDate).associateBy { it.id }.toMutableMap() +// +// service.findFlights(from, to, departureDate) +// .map { it.toFlight() } +// .forEach { flight -> +// existFlights[flight.id] = flight +// } +// +// return existFlights.map { it.value }.sortedBy { it.id } +// } + override suspend fun insertFlight(flight: Flight) { service.createFlight(flight.toFlightRemote()).toFlight() } diff --git a/app/src/main/java/ru/ulstu/is/airticketrentservice/database/repository/FlightRepository.kt b/app/src/main/java/ru/ulstu/is/airticketrentservice/database/repository/FlightRepository.kt index 91f673e..a35a471 100644 --- a/app/src/main/java/ru/ulstu/is/airticketrentservice/database/repository/FlightRepository.kt +++ b/app/src/main/java/ru/ulstu/is/airticketrentservice/database/repository/FlightRepository.kt @@ -9,6 +9,6 @@ interface FlightRepository { suspend fun updateFlight(flight: Flight) suspend fun deleteFlight(flight: Flight) suspend fun getFlightById(flightId: Int): Flight - suspend fun findFlights(from: String, to: String, departureDate: String): List + suspend fun foundFlights(from: String, to: String, departureDate: String): List fun getFlights(): Flow> } \ No newline at end of file diff --git a/app/src/main/java/ru/ulstu/is/airticketrentservice/database/repository/OfflineFlightRepository.kt b/app/src/main/java/ru/ulstu/is/airticketrentservice/database/repository/OfflineFlightRepository.kt index 291c307..54381b1 100644 --- a/app/src/main/java/ru/ulstu/is/airticketrentservice/database/repository/OfflineFlightRepository.kt +++ b/app/src/main/java/ru/ulstu/is/airticketrentservice/database/repository/OfflineFlightRepository.kt @@ -11,6 +11,7 @@ import ru.ulstu.`is`.airticketrentservice.database.models.Flight import ru.ulstu.`is`.airticketrentservice.database.models.Rent class OfflineFlightRepository(private val flightDao: FlightDao) : FlightRepository{ + suspend fun getAllFlights(): List = flightDao.getAll() override suspend fun insertFlight(flight: Flight) = flightDao.insert(flight) override suspend fun updateFlight(flight: Flight) = flightDao.update(flight) override suspend fun deleteFlight(flight: Flight) = flightDao.delete(flight) @@ -24,7 +25,7 @@ class OfflineFlightRepository(private val flightDao: FlightDao) : FlightReposito ).flow fun getAllFlightsPagingSource(): PagingSource = flightDao.getFlights() - override suspend fun findFlights(from: String, to: String, departureDate: String): List = flightDao.findFlights(from, to, departureDate) + override suspend fun foundFlights(from: String, to: String, departureDate: String): List = flightDao.findFlights(from, to, departureDate) suspend fun clearFlights() = flightDao.deleteAll() suspend fun insertFlights(flights: List) = flightDao.insert(*flights.toTypedArray()) diff --git a/app/src/main/java/ru/ulstu/is/airticketrentservice/screen/FoundFlights.kt b/app/src/main/java/ru/ulstu/is/airticketrentservice/screen/FoundFlights.kt index 45cffe8..c414a12 100644 --- a/app/src/main/java/ru/ulstu/is/airticketrentservice/screen/FoundFlights.kt +++ b/app/src/main/java/ru/ulstu/is/airticketrentservice/screen/FoundFlights.kt @@ -40,14 +40,13 @@ import ru.ulstu.`is`.airticketrentservice.database.models.Flight import ru.ulstu.`is`.airticketrentservice.navigation.BottomBarScreen import ru.ulstu.`is`.airticketrentservice.viewModel.AppViewModelProvider import ru.ulstu.`is`.airticketrentservice.viewModel.FindFlightsViewModel -import ru.ulstu.`is`.airticketrentservice.viewModel.FoundFlightsUiState @Composable fun FoundFlights( navController: NavController, viewModel: FindFlightsViewModel = viewModel(factory = AppViewModelProvider.Factory) ) { - val foundFlightsUiState = viewModel.foundFlightsUiState + val foundFlightsUiState = viewModel.foundFlightsList Scaffold( topBar = {} @@ -56,7 +55,7 @@ fun FoundFlights( modifier = Modifier .padding(innerPadding) .fillMaxSize(), - flightList = foundFlightsUiState.flightList, + flightList = foundFlightsUiState, onClick = { uid: Int -> val route = BottomBarScreen.FlightInfo.passId(uid.toString()) navController.navigate(route) diff --git a/app/src/main/java/ru/ulstu/is/airticketrentservice/viewModel/AppViewModelProvider.kt b/app/src/main/java/ru/ulstu/is/airticketrentservice/viewModel/AppViewModelProvider.kt index d85512e..49ff0af 100644 --- a/app/src/main/java/ru/ulstu/is/airticketrentservice/viewModel/AppViewModelProvider.kt +++ b/app/src/main/java/ru/ulstu/is/airticketrentservice/viewModel/AppViewModelProvider.kt @@ -6,6 +6,7 @@ import androidx.lifecycle.viewmodel.CreationExtras import androidx.lifecycle.viewmodel.initializer import androidx.lifecycle.viewmodel.viewModelFactory import ru.ulstu.`is`.airticketrentservice.TicketApplication +import ru.ulstu.`is`.airticketrentservice.api.AppService object AppViewModelProvider { val Factory = viewModelFactory { @@ -52,7 +53,8 @@ object AppViewModelProvider { initializer { FindFlightsViewModel( this.createSavedStateHandle(), - ticketApplication().container.flightRestRepository + ticketApplication().container.flightRestRepository, + ticketApplication().container.appService ) } initializer { diff --git a/app/src/main/java/ru/ulstu/is/airticketrentservice/viewModel/FindFlightsViewModel.kt b/app/src/main/java/ru/ulstu/is/airticketrentservice/viewModel/FindFlightsViewModel.kt index 431a690..c592b00 100644 --- a/app/src/main/java/ru/ulstu/is/airticketrentservice/viewModel/FindFlightsViewModel.kt +++ b/app/src/main/java/ru/ulstu/is/airticketrentservice/viewModel/FindFlightsViewModel.kt @@ -11,24 +11,36 @@ import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.stateIn import kotlinx.coroutines.launch +import ru.ulstu.`is`.airticketrentservice.api.AppService +import ru.ulstu.`is`.airticketrentservice.api.model.toFlight import ru.ulstu.`is`.airticketrentservice.database.AppContainer import ru.ulstu.`is`.airticketrentservice.database.models.Flight import ru.ulstu.`is`.airticketrentservice.database.repository.FlightRepository class FindFlightsViewModel( savedStateHandle: SavedStateHandle, - private val flightRepository: FlightRepository + private val flightRepository: FlightRepository, + ) : ViewModel() { private val from: String = checkNotNull(savedStateHandle["direction_from"]) private val to: String = checkNotNull(savedStateHandle["direction_to"]) private val departureDate: String = checkNotNull(savedStateHandle["departure_date"]) - - var foundFlightsUiState by mutableStateOf(FoundFlightsUiState()) + private val appService: AppService + get() { + return AppService + } + var foundFlightsList: List by mutableStateOf(emptyList()) private set + + +// var foundFlightsUiState by mutableStateOf(FoundFlightsUiState()) +// private set + init { viewModelScope.launch { - foundFlightsUiState = FoundFlightsUiState(flightRepository.findFlights(from, to, departureDate)) + foundFlightsList = appService.foundFlights().map { it.toFlight() } + //foundFlightsUiState = FoundFlightsUiState(flightRepository.foundFlights(from, to, departureDate)) } } } diff --git a/server/data.json b/server/data.json index abb9337..594de25 100644 --- a/server/data.json +++ b/server/data.json @@ -7,7 +7,7 @@ "flightId": 2 } ], - "flights": [ + "searchFlights": [ { "id": 2, "direction_from": "a", @@ -17,15 +17,6 @@ "tickets_count": 100, "one_ticket_cost": 1000 }, - { - "direction_from": "ульск", - "direction_to": "мск", - "departure_date": "24-12-2023", - "arrival_date": "24-12-2023", - "tickets_count": 20, - "one_ticket_cost": 2000, - "id": 3 - }, { "direction_from": "Санкт-Петербург ", "direction_to": "Сочи", @@ -36,14 +27,16 @@ "id": 5 }, { + "id": 6, "direction_from": "Ульяновск ", "direction_to": "Москва ", "departure_date": "26-12-2023", "arrival_date": "26-12-2023", - "tickets_count": 40, - "one_ticket_cost": 2680, - "id": 6 - }, + "tickets_count": 50, + "one_ticket_cost": 2680 + } + ], + "flights": [ { "direction_from": "Хабаровск ", "direction_to": "Кострома ", @@ -121,6 +114,26 @@ "password": "aaa", "role": "user", "id": 5 + }, + { + "surname": "фффф", + "name": "ффф", + "patronymic": "ффф", + "date_of_birth": "7-5-2005", + "email": "userff@mail.ru", + "password": "password", + "role": "user", + "id": 6 + }, + { + "surname": "ккк", + "name": "ккк", + "patronymic": "ккк", + "date_of_birth": "23-12-2011", + "email": "kkk@mail.ru", + "password": "kkk", + "role": "user", + "id": 7 } ], "rents": [ diff --git a/server/data.json.bak b/server/data.json.bak index 38fafe5..739696b 100644 --- a/server/data.json.bak +++ b/server/data.json.bak @@ -7,7 +7,7 @@ "flightId": 2 } ], - "flights": [ + "foundFlights": [ { "id": 2, "direction_from": "a", @@ -17,15 +17,6 @@ "tickets_count": 100, "one_ticket_cost": 1000 }, - { - "direction_from": "ульск", - "direction_to": "мск", - "departure_date": "24-12-2023", - "arrival_date": "24-12-2023", - "tickets_count": 20, - "one_ticket_cost": 2000, - "id": 3 - }, { "direction_from": "Санкт-Петербург ", "direction_to": "Сочи", @@ -36,14 +27,16 @@ "id": 5 }, { + "id": 6, "direction_from": "Ульяновск ", "direction_to": "Москва ", "departure_date": "26-12-2023", "arrival_date": "26-12-2023", - "tickets_count": 40, - "one_ticket_cost": 2680, - "id": 6 - }, + "tickets_count": 50, + "one_ticket_cost": 2680 + } + ], + "flights": [ { "direction_from": "Хабаровск ", "direction_to": "Кострома ", @@ -117,10 +110,30 @@ "name": "шчшчгч", "patronymic": "шчшчшч", "date_of_birth": "10-12-2015", - "email": "aaa@mail.ru", + "email": "aa@mail.ru", "password": "aaa", "role": "user", "id": 5 + }, + { + "surname": "фффф", + "name": "ффф", + "patronymic": "ффф", + "date_of_birth": "7-5-2005", + "email": "userff@mail.ru", + "password": "password", + "role": "user", + "id": 6 + }, + { + "surname": "ккк", + "name": "ккк", + "patronymic": "ккк", + "date_of_birth": "23-12-2011", + "email": "kkk@mail.ru", + "password": "kkk", + "role": "user", + "id": 7 } ], "rents": [