Fix crash when inserting existing server and improve DAOs

This commit is contained in:
Maxr1998
2020-11-16 23:02:29 +01:00
parent 6511c60282
commit eabc6cc9f8
3 changed files with 28 additions and 5 deletions
@@ -28,13 +28,13 @@ class ServerController(
suspend fun setupServer(hostname: String) {
appPreferences.currentServerId = withContext(Dispatchers.IO) {
serverDao.insert(hostname)
serverDao.getServerByHostname(hostname)?.id ?: serverDao.insert(hostname)
}
}
suspend fun setupUser(serverId: Long, userId: String, accessToken: String) {
appPreferences.currentUserId = withContext(Dispatchers.IO) {
userDao.insert(serverId, userId, accessToken)
userDao.upsert(serverId, userId, accessToken)
}
apiClient.SetAuthenticationInfo(accessToken, userId)
}
@@ -9,11 +9,14 @@ import org.jellyfin.mobile.model.sql.entity.ServerEntity.Key.TABLE_NAME
@Dao
interface ServerDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
@Insert(onConflict = OnConflictStrategy.ABORT)
fun insert(entity: ServerEntity): Long
fun insert(hostname: String) = insert(ServerEntity(hostname))
@Query("SELECT * FROM $TABLE_NAME WHERE id = :id")
fun getServer(id: Long): ServerEntity?
@Query("SELECT * FROM $TABLE_NAME WHERE hostname = :hostname")
fun getServerByHostname(hostname: String): ServerEntity?
}
@@ -7,20 +7,40 @@ import org.jellyfin.mobile.model.sql.entity.UserEntity.Key.ACCESS_TOKEN
import org.jellyfin.mobile.model.sql.entity.UserEntity.Key.ID
import org.jellyfin.mobile.model.sql.entity.UserEntity.Key.SERVER_ID
import org.jellyfin.mobile.model.sql.entity.UserEntity.Key.TABLE_NAME
import org.jellyfin.mobile.model.sql.entity.UserEntity.Key.USER_ID
@Dao
interface UserDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
@Insert(onConflict = OnConflictStrategy.ABORT)
fun insert(entity: UserEntity): Long
fun insert(serverId: Long, userId: String, accessToken: String?) = insert(UserEntity(serverId, userId, accessToken))
@Transaction
fun upsert(serverId: Long, userId: String, accessToken: String?): Long {
val user = getByUserId(serverId, userId)
return if (user != null) {
update(user.id, accessToken)
user.id
} else insert(serverId, userId, accessToken)
}
@Transaction
@Query("SELECT * FROM $TABLE_NAME WHERE $SERVER_ID = :serverId AND $ID = :userId")
fun getServerUser(serverId: Long, userId: Long): ServerUser?
@Transaction
@Query("SELECT * FROM $TABLE_NAME WHERE $SERVER_ID = :serverId AND $USER_ID = :userId")
fun getServerUser(serverId: Long, userId: String): ServerUser?
@Query("SELECT * FROM $TABLE_NAME WHERE $SERVER_ID = :serverId AND $USER_ID = :userId")
fun getByUserId(serverId: Long, userId: String): UserEntity?
@Query("SELECT * FROM $TABLE_NAME WHERE $SERVER_ID = :serverId")
fun getUsersForServer(serverId: Long): List<UserEntity>
fun getAllForServer(serverId: Long): List<UserEntity>
@Query("UPDATE $TABLE_NAME SET access_token = :accessToken WHERE $ID = :userId")
fun update(userId: Long, accessToken: String?): Int
@Query("UPDATE $TABLE_NAME SET $ACCESS_TOKEN = NULL WHERE $ID = :userId")
fun logout(userId: Long)