Add db models for seerr servers

This commit is contained in:
Damontecres 2025-12-20 19:09:11 -05:00
parent 4a0198b462
commit 1030741631
No known key found for this signature in database
5 changed files with 697 additions and 4 deletions

View file

@ -1,13 +1,14 @@
package com.github.damontecres.wholphin.api.seerr
import com.github.damontecres.wholphin.api.seerr.infrastructure.ApiClient
import com.github.damontecres.wholphin.ui.isNotNullOrBlank
import okhttp3.Call
import okhttp3.OkHttpClient
import timber.log.Timber
class SeerrApiClient(
val baseUrl: String,
val apiKey: String,
val apiKey: String?,
okHttpClient: OkHttpClient,
) {
private val client =
@ -19,8 +20,9 @@ class SeerrApiClient(
it
.request()
.newBuilder()
.header("X-Api-Key", apiKey)
.build(),
.apply {
if (apiKey.isNotNullOrBlank()) header("X-Api-Key", apiKey)
}.build(),
)
}.build()

View file

@ -15,6 +15,8 @@ import com.github.damontecres.wholphin.data.model.JellyfinUser
import com.github.damontecres.wholphin.data.model.LibraryDisplayInfo
import com.github.damontecres.wholphin.data.model.NavDrawerPinnedItem
import com.github.damontecres.wholphin.data.model.PlaybackLanguageChoice
import com.github.damontecres.wholphin.data.model.SeerrServer
import com.github.damontecres.wholphin.data.model.SeerrUser
import com.github.damontecres.wholphin.ui.components.ViewOptions
import kotlinx.serialization.json.Json
import org.jellyfin.sdk.model.api.ItemSortBy
@ -32,8 +34,10 @@ import java.util.UUID
LibraryDisplayInfo::class,
PlaybackLanguageChoice::class,
ItemTrackModification::class,
SeerrServer::class,
SeerrUser::class,
],
version = 12,
version = 20,
exportSchema = true,
autoMigrations = [
AutoMigration(3, 4),
@ -45,6 +49,7 @@ import java.util.UUID
AutoMigration(9, 10),
AutoMigration(10, 11),
AutoMigration(11, 12),
AutoMigration(12, 20),
],
)
@TypeConverters(Converters::class)
@ -58,6 +63,8 @@ abstract class AppDatabase : RoomDatabase() {
abstract fun libraryDisplayInfoDao(): LibraryDisplayInfoDao
abstract fun playbackLanguageChoiceDao(): PlaybackLanguageChoiceDao
abstract fun seerrServerDao(): SeerrServerDao
}
class Converters {

View file

@ -0,0 +1,63 @@
package com.github.damontecres.wholphin.data
import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import androidx.room.Transaction
import androidx.room.Update
import com.github.damontecres.wholphin.data.model.SeerrServer
import com.github.damontecres.wholphin.data.model.SeerrServerUsers
import com.github.damontecres.wholphin.data.model.SeerrUser
import java.util.UUID
@Dao
interface SeerrServerDao {
@Insert(onConflict = OnConflictStrategy.IGNORE)
suspend fun addServer(server: SeerrServer): Long
@Update
suspend fun updateServer(server: SeerrServer): Int
@Transaction
suspend fun addOrUpdateServer(server: SeerrServer) {
val result = addServer(server)
if (result == -1L) {
updateServer(server)
}
}
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun addUser(user: SeerrUser): Long
suspend fun updateUser(user: SeerrUser) = addUser(user)
@Query("SELECT * FROM seerr_users WHERE serverId = :serverId AND jellyfinRowId = :userId")
suspend fun getUser(
serverId: Int,
userId: UUID,
): SeerrUser?
@Query("DELETE FROM seerr_servers WHERE id = :serverId")
suspend fun deleteServer(serverId: Int)
@Query("DELETE FROM seerr_users WHERE serverId = :serverId AND jellyfinRowId = :jellyfinRowId")
suspend fun deleteUser(
serverId: Int,
jellyfinRowId: Int,
)
suspend fun deleteUser(user: SeerrUser) = deleteUser(user.serverId, user.jellyfinRowId)
@Transaction
@Query("SELECT * FROM seerr_servers")
suspend fun getServers(): List<SeerrServerUsers>
@Transaction
@Query("SELECT * FROM seerr_servers WHERE id = :serverId")
suspend fun getServer(serverId: Int): SeerrServerUsers?
@Transaction
@Query("SELECT * FROM seerr_servers WHERE url = :url")
suspend fun getServer(url: String): SeerrServerUsers?
}

View file

@ -0,0 +1,72 @@
@file:UseSerializers(UUIDSerializer::class)
package com.github.damontecres.wholphin.data.model
import androidx.room.Embedded
import androidx.room.Entity
import androidx.room.ForeignKey
import androidx.room.Index
import androidx.room.PrimaryKey
import androidx.room.Relation
import com.github.damontecres.wholphin.ui.isNotNullOrBlank
import kotlinx.serialization.Serializable
import kotlinx.serialization.UseSerializers
import org.jellyfin.sdk.model.serializer.UUIDSerializer
@Entity(
tableName = "seerr_servers",
indices = [Index("url", unique = true)],
)
@Serializable
data class SeerrServer(
@PrimaryKey(autoGenerate = true)
val id: Int,
val name: String?,
val url: String,
val version: String?,
)
@Entity(
tableName = "seerr_users",
foreignKeys = [
ForeignKey(
entity = SeerrServer::class,
parentColumns = arrayOf("id"),
childColumns = arrayOf("serverId"),
onDelete = ForeignKey.CASCADE,
),
ForeignKey(
entity = JellyfinUser::class,
parentColumns = arrayOf("rowId"),
childColumns = arrayOf("jellyfinRowId"),
onDelete = ForeignKey.CASCADE,
),
],
primaryKeys = ["jellyfinRowId", "serverId"],
)
data class SeerrUser(
val jellyfinRowId: Int,
val serverId: Int,
val authMethod: SeerrAuthMethod,
val username: String?,
val password: String?,
val credential: String?,
) {
override fun toString(): String =
"SeerrUser(jellyfinRowId=$jellyfinRowId, serverId=$serverId, authMethod=$authMethod, username=$username, password?=${password.isNotNullOrBlank()}, credential?=${credential.isNotNullOrBlank()})"
}
enum class SeerrAuthMethod {
LOCAL,
JELLYFIN,
API_KEY,
}
data class SeerrServerUsers(
@Embedded val server: SeerrServer,
@Relation(
parentColumn = "id",
entityColumn = "serverId",
)
val users: List<SeerrUser>,
)