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.PUT
import retrofit2.http.Path import retrofit2.http.Path
import retrofit2.http.Query import retrofit2.http.Query
import java.util.concurrent.TimeUnit
interface MyServerService { interface MyServerService {
@GET("persons") @GET("api/persons")
suspend fun getPeople( suspend fun getPeople(
@Query("_page") page: Int, @Query("_page") page: Int,
@Query("_limit") limit: Int, @Query("_limit") limit: Int,
): List<PersonRemote> ): List<PersonRemote>
@GET("performances") @GET("api/performances")
suspend fun getPerformances( suspend fun getPerformances(
@Query("_page") page: Int, @Query("_page") page: Int,
@Query("_limit") limit: Int, @Query("_limit") limit: Int,
): List<PerformanceRemote> ): List<PerformanceRemote>
@GET("events") @GET("api/events")
suspend fun getEvents( suspend fun getEvents(
@Query("_page") page: Int, @Query("_page") page: Int,
@Query("_limit") limit: Int, @Query("_limit") limit: Int,
): List<EventRemote> ): List<EventRemote>
@GET("performances/{id}") @GET("api/performances/{id}")
suspend fun getPerformance( suspend fun getPerformance(
@Path("id") id: Int, @Path("id") id: Int,
): PerformanceRemote ): PerformanceRemote
@GET("persons/{id}") @GET("api/persons/{id}")
suspend fun getPerson( suspend fun getPerson(
@Path("id") id: Int, @Path("id") id: Int,
): PersonRemote ): PersonRemote
@GET("events/{id}") @GET("api/events/{id}")
suspend fun getEvent( suspend fun getEvent(
@Path("id") id: Int, @Path("id") id: Int,
): EventRemote ): EventRemote
companion object { 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 @Volatile
private var INSTANCE: MyServerService? = null private var INSTANCE: MyServerService? = null
private var _token: String = ""
fun getInstance(): MyServerService { fun getInstance(): MyServerService {
return INSTANCE ?: synchronized(this) { return INSTANCE ?: synchronized(this) {
val logger = HttpLoggingInterceptor() val logger = HttpLoggingInterceptor()
logger.level = HttpLoggingInterceptor.Level.BASIC logger.level = HttpLoggingInterceptor.Level.BASIC
val client = OkHttpClient.Builder() 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(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() .build()
return Retrofit.Builder() return Retrofit.Builder()
.baseUrl(BASE_URL) .baseUrl(BASE_URL)
@ -73,5 +95,9 @@ interface MyServerService {
.also { INSTANCE = it } .also { INSTANCE = it }
} }
} }
fun setToken(token: String) {
_token = token
}
} }
} }

View File

@ -73,6 +73,9 @@ class EventRemoteMediator(
nextKey = nextKey nextKey = nextKey
) )
} }
dbRemoteKeyRepository.deleteRemoteKey(RemoteKeyType.EVENT)
dbEventRepository.clearEvents()
performanceRestRepository.getAllPerformances() performanceRestRepository.getAllPerformances()
dbRemoteKeyRepository.createRemoteKeys(keys) dbRemoteKeyRepository.createRemoteKeys(keys)
dbEventRepository.insertEvents(events) 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( data class EventRemote(
val id: Int = 0, val id: Int = 0,
val date: String = "", val date: String = "",
val performanceId: Int = 0, val performance_id: Int = 0,
) )
fun EventRemote.toEvent(): Event = Event( fun EventRemote.toEvent(): Event = Event(
id, id,
LocalDate.parse(date), LocalDate.parse(date),
performanceId performance_id
) )
suspend fun EventRemote.toEventWithPerformance(service: MyServerService): EventWithPerformance = EventWithPerformance( suspend fun EventRemote.toEventWithPerformance(service: MyServerService): EventWithPerformance = EventWithPerformance(
service.getEvent(id).toEvent(), 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 id: Int = 0,
val title: String = "", val title: String = "",
val description: String = "", val description: String = "",
val authorId: Int = 0, val author_id: Int = 0,
val directorId: Int = 0, val director_id: Int = 0,
val imageURL: String = "", val image_url: String = "",
val previewImageURL: String = "", val preview_image_url: String = "",
val actors: List<Int> = listOf() val actors: List<Int> = listOf()
) )
@ -22,10 +22,10 @@ fun PerformanceRemote.toPerformance(): Performance = Performance(
id, id,
title, title,
description, description,
authorId, author_id,
directorId, director_id,
imageURL, image_url,
previewImageURL preview_image_url
) )
suspend fun PerformanceRemote.toPerformanceWithPeople(service: MyServerService): PerformanceWithPeople { suspend fun PerformanceRemote.toPerformanceWithPeople(service: MyServerService): PerformanceWithPeople {
@ -36,8 +36,8 @@ suspend fun PerformanceRemote.toPerformanceWithPeople(service: MyServerService):
return PerformanceWithPeople( return PerformanceWithPeople(
service.getPerformance(id).toPerformance(), service.getPerformance(id).toPerformance(),
service.getPerson(authorId).toPerson(), service.getPerson(author_id).toPerson(),
service.getPerson(directorId).toPerson(), service.getPerson(director_id).toPerson(),
actorsList.toList() actorsList.toList()
) )
} }

View File

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

View File

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

View File

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