From d79f6982feee6046ccb1f5c71f7354221c42c46a Mon Sep 17 00:00:00 2001 From: abazov73 <92822431+abazov73@users.noreply.github.com> Date: Tue, 26 Dec 2023 18:55:30 +0400 Subject: [PATCH] Course work: edit routes for new server --- .../mobile_labs/api/MyServerService.kt | 40 +++++++++++++++---- .../api/events/EventRemoteMediator.kt | 3 ++ .../mobile_labs/api/models/Credentials.kt | 9 +++++ .../mobile_labs/api/models/EventRemote.kt | 6 +-- .../api/models/PerformanceRemote.kt | 20 +++++----- .../mobile_labs/api/models/PersonRemote.kt | 4 +- .../api/people/PeopleRemoteMediator.kt | 3 ++ .../performance/PerformanceRemoteMediator.kt | 5 +++ 8 files changed, 68 insertions(+), 22 deletions(-) create mode 100644 app/src/main/java/com/example/mobile_labs/api/models/Credentials.kt diff --git a/app/src/main/java/com/example/mobile_labs/api/MyServerService.kt b/app/src/main/java/com/example/mobile_labs/api/MyServerService.kt index b2afe9b..335d595 100644 --- a/app/src/main/java/com/example/mobile_labs/api/MyServerService.kt +++ b/app/src/main/java/com/example/mobile_labs/api/MyServerService.kt @@ -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 - @GET("performances") + @GET("api/performances") suspend fun getPerformances( @Query("_page") page: Int, @Query("_limit") limit: Int, ): List - @GET("events") + @GET("api/events") suspend fun getEvents( @Query("_page") page: Int, @Query("_limit") limit: Int, ): List - @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 + } } } \ No newline at end of file diff --git a/app/src/main/java/com/example/mobile_labs/api/events/EventRemoteMediator.kt b/app/src/main/java/com/example/mobile_labs/api/events/EventRemoteMediator.kt index f15351a..1f0e35c 100644 --- a/app/src/main/java/com/example/mobile_labs/api/events/EventRemoteMediator.kt +++ b/app/src/main/java/com/example/mobile_labs/api/events/EventRemoteMediator.kt @@ -73,6 +73,9 @@ class EventRemoteMediator( nextKey = nextKey ) } + dbRemoteKeyRepository.deleteRemoteKey(RemoteKeyType.EVENT) + dbEventRepository.clearEvents() + performanceRestRepository.getAllPerformances() dbRemoteKeyRepository.createRemoteKeys(keys) dbEventRepository.insertEvents(events) diff --git a/app/src/main/java/com/example/mobile_labs/api/models/Credentials.kt b/app/src/main/java/com/example/mobile_labs/api/models/Credentials.kt new file mode 100644 index 0000000..aae34b9 --- /dev/null +++ b/app/src/main/java/com/example/mobile_labs/api/models/Credentials.kt @@ -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 = "", +) \ No newline at end of file diff --git a/app/src/main/java/com/example/mobile_labs/api/models/EventRemote.kt b/app/src/main/java/com/example/mobile_labs/api/models/EventRemote.kt index 600ec3e..1c9c37c 100644 --- a/app/src/main/java/com/example/mobile_labs/api/models/EventRemote.kt +++ b/app/src/main/java/com/example/mobile_labs/api/models/EventRemote.kt @@ -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() ) \ No newline at end of file diff --git a/app/src/main/java/com/example/mobile_labs/api/models/PerformanceRemote.kt b/app/src/main/java/com/example/mobile_labs/api/models/PerformanceRemote.kt index 306ed57..e7c2abd 100644 --- a/app/src/main/java/com/example/mobile_labs/api/models/PerformanceRemote.kt +++ b/app/src/main/java/com/example/mobile_labs/api/models/PerformanceRemote.kt @@ -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 = 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() ) } \ No newline at end of file diff --git a/app/src/main/java/com/example/mobile_labs/api/models/PersonRemote.kt b/app/src/main/java/com/example/mobile_labs/api/models/PersonRemote.kt index fa4da4b..6d1d027 100644 --- a/app/src/main/java/com/example/mobile_labs/api/models/PersonRemote.kt +++ b/app/src/main/java/com/example/mobile_labs/api/models/PersonRemote.kt @@ -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 ) \ No newline at end of file diff --git a/app/src/main/java/com/example/mobile_labs/api/people/PeopleRemoteMediator.kt b/app/src/main/java/com/example/mobile_labs/api/people/PeopleRemoteMediator.kt index a746f56..b5a556d 100644 --- a/app/src/main/java/com/example/mobile_labs/api/people/PeopleRemoteMediator.kt +++ b/app/src/main/java/com/example/mobile_labs/api/people/PeopleRemoteMediator.kt @@ -71,6 +71,9 @@ class PeopleRemoteMediator( nextKey = nextKey ) } + +// dbRemoteKeyRepository.deleteRemoteKey(RemoteKeyType.PERSON) +// dbPersonRepository.clearPeople() dbRemoteKeyRepository.createRemoteKeys(keys) dbPersonRepository.insertPeople(people) } diff --git a/app/src/main/java/com/example/mobile_labs/api/performance/PerformanceRemoteMediator.kt b/app/src/main/java/com/example/mobile_labs/api/performance/PerformanceRemoteMediator.kt index 458c09a..138db2b 100644 --- a/app/src/main/java/com/example/mobile_labs/api/performance/PerformanceRemoteMediator.kt +++ b/app/src/main/java/com/example/mobile_labs/api/performance/PerformanceRemoteMediator.kt @@ -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)