60 lines
1.3 KiB
Kotlin
Executable File
60 lines
1.3 KiB
Kotlin
Executable File
/*
|
|
* Created by sweetbread
|
|
* Copyright (c) 2025. All rights reserved.
|
|
*/
|
|
|
|
package ru.risdeveau.pixeldragon.db
|
|
|
|
import androidx.room.Dao
|
|
import androidx.room.Delete
|
|
import androidx.room.Entity
|
|
import androidx.room.Insert
|
|
import androidx.room.OnConflictStrategy
|
|
import androidx.room.PrimaryKey
|
|
import androidx.room.Query
|
|
import androidx.room.TypeConverters
|
|
import org.json.JSONObject
|
|
import ru.risdeveau.pixeldragon.repo.User
|
|
import java.time.Instant
|
|
|
|
@Entity(tableName = "user")
|
|
@TypeConverters(Converters::class)
|
|
data class UserDB (
|
|
@PrimaryKey val id: String,
|
|
val updatedAt: Instant,
|
|
val name: String?,
|
|
val avatar: String?,
|
|
val attrs: String,
|
|
)
|
|
|
|
fun UserDB.isExpired(cacheDuration: Long = 60 * 60): Boolean {
|
|
return Instant.now().minusSeconds(cacheDuration) > updatedAt
|
|
}
|
|
|
|
fun UserDB.toDomain(): User = User(
|
|
id = id,
|
|
name = name,
|
|
avatarUrl = avatar,
|
|
attrs = JSONObject(attrs),
|
|
)
|
|
|
|
fun User.toEntity(): UserDB = UserDB(
|
|
id = id,
|
|
updatedAt = Instant.now(),
|
|
name = name,
|
|
avatar = avatarUrl,
|
|
attrs = attrs.toString().trim(),
|
|
)
|
|
|
|
|
|
@Dao
|
|
interface UserDao {
|
|
@Query("SELECT * FROM user WHERE id LIKE :id LIMIT 1")
|
|
fun getById(id: String): UserDB?
|
|
|
|
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
|
fun insert(vararg users: UserDB)
|
|
|
|
@Delete
|
|
fun delete(user: UserDB)
|
|
} |