mirror of
https://github.com/imcarlost/Friendlists.git
synced 2026-04-09 18:38:36 -04:00
add room entities and daos
This commit is contained in:
17
base/src/main/java/com/hako/base/room/Database.kt
Normal file
17
base/src/main/java/com/hako/base/room/Database.kt
Normal file
@@ -0,0 +1,17 @@
|
||||
package com.hako.base.room
|
||||
|
||||
import androidx.room.Database
|
||||
import androidx.room.RoomDatabase
|
||||
import com.hako.base.room.dao.AlbumDao
|
||||
import com.hako.base.room.dao.PhotoDao
|
||||
import com.hako.base.room.dao.UserDao
|
||||
import com.hako.base.room.entities.AlbumEntity
|
||||
import com.hako.base.room.entities.PhotoEntity
|
||||
import com.hako.base.room.entities.UserEntity
|
||||
|
||||
@Database(entities = [UserEntity::class, AlbumEntity::class, PhotoEntity::class], version = 1, exportSchema = false)
|
||||
abstract class Database : RoomDatabase() {
|
||||
abstract fun userDao(): UserDao
|
||||
abstract fun albumDao(): AlbumDao
|
||||
abstract fun photoDao(): PhotoDao
|
||||
}
|
||||
27
base/src/main/java/com/hako/base/room/dao/AlbumDao.kt
Normal file
27
base/src/main/java/com/hako/base/room/dao/AlbumDao.kt
Normal file
@@ -0,0 +1,27 @@
|
||||
package com.hako.base.room.dao
|
||||
|
||||
import androidx.room.Dao
|
||||
import androidx.room.Insert
|
||||
import androidx.room.OnConflictStrategy
|
||||
import androidx.room.Query
|
||||
import com.hako.base.room.entities.AlbumEntity
|
||||
|
||||
@Dao
|
||||
interface AlbumDao {
|
||||
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
fun save(entity: AlbumEntity)
|
||||
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
fun saveAll(entities: List<AlbumEntity>)
|
||||
|
||||
@get:Query("SELECT * FROM ${AlbumEntity.TABLE_NAME}")
|
||||
val all: List<AlbumEntity>
|
||||
|
||||
@Query("SELECT COUNT(*) FROM ${AlbumEntity.TABLE_NAME}")
|
||||
fun count(page: Int): Int
|
||||
|
||||
@Query("DELETE FROM ${AlbumEntity.TABLE_NAME}")
|
||||
fun nukeDatabase()
|
||||
|
||||
}
|
||||
27
base/src/main/java/com/hako/base/room/dao/PhotoDao.kt
Normal file
27
base/src/main/java/com/hako/base/room/dao/PhotoDao.kt
Normal file
@@ -0,0 +1,27 @@
|
||||
package com.hako.base.room.dao
|
||||
|
||||
import androidx.room.Dao
|
||||
import androidx.room.Insert
|
||||
import androidx.room.OnConflictStrategy
|
||||
import androidx.room.Query
|
||||
import com.hako.base.room.entities.PhotoEntity
|
||||
|
||||
@Dao
|
||||
interface PhotoDao {
|
||||
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
fun save(entity: PhotoEntity)
|
||||
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
fun saveAll(entities: List<PhotoEntity>)
|
||||
|
||||
@get:Query("SELECT * FROM ${PhotoEntity.TABLE_NAME}")
|
||||
val all: List<PhotoEntity>
|
||||
|
||||
@Query("SELECT COUNT(*) FROM ${PhotoEntity.TABLE_NAME}")
|
||||
fun count(page: Int): Int
|
||||
|
||||
@Query("DELETE FROM ${PhotoEntity.TABLE_NAME}")
|
||||
fun nukeDatabase()
|
||||
|
||||
}
|
||||
27
base/src/main/java/com/hako/base/room/dao/UserDao.kt
Normal file
27
base/src/main/java/com/hako/base/room/dao/UserDao.kt
Normal file
@@ -0,0 +1,27 @@
|
||||
package com.hako.base.room.dao
|
||||
|
||||
import androidx.room.Dao
|
||||
import androidx.room.Insert
|
||||
import androidx.room.OnConflictStrategy
|
||||
import androidx.room.Query
|
||||
import com.hako.base.room.entities.UserEntity
|
||||
|
||||
@Dao
|
||||
interface UserDao {
|
||||
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
fun save(entity: UserEntity)
|
||||
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
fun saveAll(entities: List<UserEntity>)
|
||||
|
||||
@get:Query("SELECT * FROM ${UserEntity.TABLE_NAME}")
|
||||
val all: List<UserEntity>
|
||||
|
||||
@Query("SELECT COUNT(*) FROM ${UserEntity.TABLE_NAME}")
|
||||
fun count(page: Int): Int
|
||||
|
||||
@Query("DELETE FROM ${UserEntity.TABLE_NAME}")
|
||||
fun nukeDatabase()
|
||||
|
||||
}
|
||||
17
base/src/main/java/com/hako/base/room/entities/Album.kt
Normal file
17
base/src/main/java/com/hako/base/room/entities/Album.kt
Normal file
@@ -0,0 +1,17 @@
|
||||
package com.hako.base.room.entities
|
||||
|
||||
import androidx.room.Entity
|
||||
import androidx.room.Index
|
||||
import androidx.room.PrimaryKey
|
||||
|
||||
@Entity(tableName = AlbumEntity.TABLE_NAME, indices = [Index(value = ["id"], unique = true)])
|
||||
data class AlbumEntity(
|
||||
@PrimaryKey
|
||||
val id: Int,
|
||||
val userId: Int,
|
||||
val title: String
|
||||
) {
|
||||
companion object {
|
||||
const val TABLE_NAME = "albums"
|
||||
}
|
||||
}
|
||||
19
base/src/main/java/com/hako/base/room/entities/Photos.kt
Normal file
19
base/src/main/java/com/hako/base/room/entities/Photos.kt
Normal file
@@ -0,0 +1,19 @@
|
||||
package com.hako.base.room.entities
|
||||
|
||||
import androidx.room.Entity
|
||||
import androidx.room.Index
|
||||
import androidx.room.PrimaryKey
|
||||
|
||||
@Entity(tableName = PhotoEntity.TABLE_NAME, indices = [Index(value = ["id"], unique = true)])
|
||||
data class PhotoEntity(
|
||||
@PrimaryKey
|
||||
val id: Int,
|
||||
val albumId: Int,
|
||||
val title: String,
|
||||
val photoUrl: String,
|
||||
val thumbnailUrl: String
|
||||
) {
|
||||
companion object {
|
||||
const val TABLE_NAME = "photos"
|
||||
}
|
||||
}
|
||||
20
base/src/main/java/com/hako/base/room/entities/User.kt
Normal file
20
base/src/main/java/com/hako/base/room/entities/User.kt
Normal file
@@ -0,0 +1,20 @@
|
||||
package com.hako.base.room.entities
|
||||
|
||||
import androidx.room.Entity
|
||||
import androidx.room.Index
|
||||
import androidx.room.PrimaryKey
|
||||
|
||||
@Entity(tableName = UserEntity.TABLE_NAME, indices = [Index(value = ["id"], unique = true)])
|
||||
data class UserEntity(
|
||||
@PrimaryKey
|
||||
val id: Int,
|
||||
val realName: String,
|
||||
val userName: String,
|
||||
val email: String,
|
||||
val phone: String,
|
||||
val website: String
|
||||
) {
|
||||
companion object {
|
||||
const val TABLE_NAME = "users"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user