Course work: edit routes for new server

This commit is contained in:
abazov73 2023-12-26 18:55:30 +04:00
parent 6b3fc223c1
commit d79f6982fe
8 changed files with 68 additions and 22 deletions

View File

@ -16,53 +16,75 @@ import retrofit2.http.POST
import retrofit2.http.PUT
import retrofit2.http.Path
import retrofit2.http.Query
import java.util.concurrent.TimeUnit
interface MyServerService {
@GET("persons")
@GET("api/persons")
suspend fun getPeople(
@Query("_page") page: Int,
@Query("_limit") limit: Int,
): List<PersonRemote>
@GET("performances")
@GET("api/performances")
suspend fun getPerformances(
@Query("_page") page: Int,
@Query("_limit") limit: Int,
): List<PerformanceRemote>
@GET("events")
@GET("api/events")
suspend fun getEvents(
@Query("_page") page: Int,
@Query("_limit") limit: Int,
): List<EventRemote>
@GET("performances/{id}")
@GET("api/performances/{id}")
suspend fun getPerformance(
@Path("id") id: Int,
): PerformanceRemote
@GET("persons/{id}")
@GET("api/persons/{id}")
suspend fun getPerson(
@Path("id") id: Int,
): PersonRemote
@GET("events/{id}")
@GET("api/events/{id}")
suspend fun getEvent(
@Path("id") id: Int,
): EventRemote
companion object {
private const val BASE_URL = "http://10.0.2.2:26000/"
private const val BASE_URL = "http://10.0.2.2:8000/"
@Volatile
private var INSTANCE: MyServerService? = null
private var _token: String = ""
fun getInstance(): MyServerService {
return INSTANCE ?: synchronized(this) {
val logger = HttpLoggingInterceptor()
logger.level = HttpLoggingInterceptor.Level.BASIC
val client = OkHttpClient.Builder()
.connectTimeout(1, TimeUnit.DAYS)
.readTimeout(1, TimeUnit.DAYS)
.writeTimeout(1, TimeUnit.DAYS)
.retryOnConnectionFailure(false)
.callTimeout(1, TimeUnit.DAYS)
.addInterceptor(logger)
.addInterceptor {
val originalRequest = it.request()
if (_token.isEmpty()) {
it.proceed(originalRequest)
} else {
it.proceed(
originalRequest
.newBuilder()
.header("Authorization", "Bearer $_token")
.method(originalRequest.method, originalRequest.body)
.build()
)
}
}
.build()
return Retrofit.Builder()
.baseUrl(BASE_URL)
@ -73,5 +95,9 @@ interface MyServerService {
.also { INSTANCE = it }
}
}
fun setToken(token: String) {
_token = token
}
}
}

View File

@ -73,6 +73,9 @@ class EventRemoteMediator(
nextKey = nextKey
)
}
dbRemoteKeyRepository.deleteRemoteKey(RemoteKeyType.EVENT)
dbEventRepository.clearEvents()
performanceRestRepository.getAllPerformances()
dbRemoteKeyRepository.createRemoteKeys(keys)
dbEventRepository.insertEvents(events)

View File

@ -0,0 +1,9 @@
package com.example.mobile_labs.api.models
import kotlinx.serialization.Serializable
@Serializable
data class Credentials(
val login: String = "",
val password: String = "",
)

View File

@ -10,16 +10,16 @@ import java.time.LocalDate
data class EventRemote(
val id: Int = 0,
val date: String = "",
val performanceId: Int = 0,
val performance_id: Int = 0,
)
fun EventRemote.toEvent(): Event = Event(
id,
LocalDate.parse(date),
performanceId
performance_id
)
suspend fun EventRemote.toEventWithPerformance(service: MyServerService): EventWithPerformance = EventWithPerformance(
service.getEvent(id).toEvent(),
service.getPerformance(performanceId).toPerformance()
service.getPerformance(performance_id).toPerformance()
)

View File

@ -11,10 +11,10 @@ data class PerformanceRemote(
val id: Int = 0,
val title: String = "",
val description: String = "",
val authorId: Int = 0,
val directorId: Int = 0,
val imageURL: String = "",
val previewImageURL: String = "",
val author_id: Int = 0,
val director_id: Int = 0,
val image_url: String = "",
val preview_image_url: String = "",
val actors: List<Int> = listOf()
)
@ -22,10 +22,10 @@ fun PerformanceRemote.toPerformance(): Performance = Performance(
id,
title,
description,
authorId,
directorId,
imageURL,
previewImageURL
author_id,
director_id,
image_url,
preview_image_url
)
suspend fun PerformanceRemote.toPerformanceWithPeople(service: MyServerService): PerformanceWithPeople {
@ -36,8 +36,8 @@ suspend fun PerformanceRemote.toPerformanceWithPeople(service: MyServerService):
return PerformanceWithPeople(
service.getPerformance(id).toPerformance(),
service.getPerson(authorId).toPerson(),
service.getPerson(directorId).toPerson(),
service.getPerson(author_id).toPerson(),
service.getPerson(director_id).toPerson(),
actorsList.toList()
)
}

View File

@ -10,12 +10,12 @@ data class PersonRemote(
val id: Int = 0,
val last_name: String = "",
val first_name: String = "",
val imageURL: String = ""
val image_url: String = ""
)
fun PersonRemote.toPerson(): Person = Person(
id,
last_name,
first_name,
imageURL
image_url
)

View File

@ -71,6 +71,9 @@ class PeopleRemoteMediator(
nextKey = nextKey
)
}
// dbRemoteKeyRepository.deleteRemoteKey(RemoteKeyType.PERSON)
// dbPersonRepository.clearPeople()
dbRemoteKeyRepository.createRemoteKeys(keys)
dbPersonRepository.insertPeople(people)
}

View File

@ -1,9 +1,11 @@
package com.example.mobile_labs.api.performance
import android.util.Log
import androidx.paging.ExperimentalPagingApi
import androidx.paging.LoadType
import androidx.paging.PagingState
import androidx.paging.RemoteMediator
import androidx.paging.log
import androidx.room.withTransaction
import com.example.mobile_labs.api.MyServerService
import com.example.mobile_labs.api.models.toPerformance
@ -73,6 +75,9 @@ class PerformanceRemoteMediator(
nextKey = nextKey
)
}
dbRemoteKeyRepository.deleteRemoteKey(RemoteKeyType.PERFORMANCE)
dbPerformanceRepository.clearPerformances()
personRestRepository.getAllPeople()
dbRemoteKeyRepository.createRemoteKeys(keys)
dbPerformanceRepository.insertPerformances(performances)