Create UserAccountAuthority entity to establish a N:N relationship between the UserAccount and Authority entities in the database. Also made some structure improvements to all the Entities and Daos already described.

This commit is contained in:
Severiano Jaramillo 2018-11-01 11:19:32 -06:00
parent da9e0225cc
commit 1c018346ae
12 changed files with 76 additions and 40 deletions

View file

@ -9,9 +9,9 @@ import cy.agorise.bitsybitshareswallet.models.Asset
@Dao @Dao
interface AssetDao { interface AssetDao {
@Query("SELECT * FROM assets")
fun getAllAssets(): LiveData<List<Asset>>
@Insert @Insert
fun insert(asset: Asset) fun insert(asset: Asset)
@Query("SELECT * FROM assets")
fun getAllAssets(): LiveData<List<Asset>>
} }

View file

@ -9,9 +9,9 @@ import cy.agorise.bitsybitshareswallet.models.Authority
@Dao @Dao
interface AuthorityDao { interface AuthorityDao {
@Query("SELECT * FROM authorities")
fun getAllAuthorities(): LiveData<List<Authority>>
@Insert @Insert
fun insert(authority: Authority) fun insert(authority: Authority)
@Query("SELECT * FROM authorities")
fun getAllAuthorities(): LiveData<List<Authority>>
} }

View file

@ -9,9 +9,9 @@ import cy.agorise.bitsybitshareswallet.models.Balance
@Dao @Dao
interface BalanceDao { interface BalanceDao {
@Query("SELECT * FROM balances")
fun getAllBalances(): LiveData<List<Balance>>
@Insert @Insert
fun insert(balance: Balance) fun insert(balance: Balance)
@Query("SELECT * FROM balances")
fun getAllBalances(): LiveData<List<Balance>>
} }

View file

@ -4,15 +4,22 @@ import android.content.Context
import androidx.room.Database import androidx.room.Database
import androidx.room.Room import androidx.room.Room
import androidx.room.RoomDatabase import androidx.room.RoomDatabase
import cy.agorise.bitsybitshareswallet.models.Asset import cy.agorise.bitsybitshareswallet.models.*
@Database(entities = [Asset::class], version = 1, exportSchema = false) @Database(entities = [
Asset::class,
Balance::class,
UserAccount::class,
Authority::class,
UserAccountAuthority::class
], version = 1, exportSchema = false)
abstract class BitsyDatabase : RoomDatabase() { abstract class BitsyDatabase : RoomDatabase() {
abstract fun assetDao(): AssetDao abstract fun assetDao(): AssetDao
abstract fun balanceDao(): BalanceDao abstract fun balanceDao(): BalanceDao
abstract fun userAccountDao(): UserAccountDao abstract fun userAccountDao(): UserAccountDao
abstract fun authorityDao(): AuthorityDao abstract fun authorityDao(): AuthorityDao
abstract fun userAccountAuthorityDao(): UserAccountAuthorityDao
companion object { companion object {

View file

@ -0,0 +1,17 @@
package cy.agorise.bitsybitshareswallet.daos
import androidx.lifecycle.LiveData
import androidx.room.Dao
import androidx.room.Insert
import androidx.room.Query
import cy.agorise.bitsybitshareswallet.models.Authority
@Dao
interface UserAccountAuthorityDao {
@Insert
fun insert(userAccountAuthorityDao: UserAccountAuthorityDao)
@Query("SELECT * FROM user_accounts INNER JOIN user_accounts__authorities ON user_accounts.id=user_accounts__authorities.user_account_id WHERE user_accounts__authorities.user_account_id=:userAccountId")
fun getAuthoritiesForUserAccount(userAccountId: String): LiveData<List<Authority>>
}

View file

@ -9,9 +9,9 @@ import cy.agorise.bitsybitshareswallet.models.UserAccount
@Dao @Dao
interface UserAccountDao { interface UserAccountDao {
@Query("SELECT * FROM user_accounts")
fun getAllUserAccounts(): LiveData<List<UserAccount>>
@Insert @Insert
fun insert(userAccount: UserAccount) fun insert(userAccount: UserAccount)
@Query("SELECT * FROM user_accounts")
fun getAllUserAccounts(): LiveData<List<UserAccount>>
} }

View file

@ -2,18 +2,9 @@ package cy.agorise.bitsybitshareswallet.models
import androidx.room.ColumnInfo import androidx.room.ColumnInfo
import androidx.room.Entity import androidx.room.Entity
import androidx.room.ForeignKey
import androidx.room.PrimaryKey import androidx.room.PrimaryKey
@Entity(tableName = "authorities", foreignKeys = @Entity(tableName = "authorities")
[ForeignKey(
entity = UserAccount::class,
parentColumns = ["id"],
childColumns = ["user_id"],
onUpdate = ForeignKey.CASCADE,
onDelete = ForeignKey.CASCADE
)]
)
data class Authority ( data class Authority (
@PrimaryKey(autoGenerate = true) @PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "id") val id: Long, @ColumnInfo(name = "id") val id: Long,

View file

@ -3,9 +3,10 @@ package cy.agorise.bitsybitshareswallet.models
import androidx.room.ColumnInfo import androidx.room.ColumnInfo
import androidx.room.Entity import androidx.room.Entity
@Entity(tableName = "balances", primaryKeys = ["user_id", "asset_id"]) @Entity(tableName = "balances", primaryKeys = ["user_account_id", "asset_id"])
// TODO verify if we can add user_account_id as primary key
data class Balance( data class Balance(
@ColumnInfo(name = "user_id") val userId: String, @ColumnInfo(name = "user_account_id") val userAccountId: String,
@ColumnInfo(name = "asset_id") val assetId: String, @ColumnInfo(name = "asset_id") val assetId: String,
@ColumnInfo(name = "asset_amount") val assetAmount: Long, @ColumnInfo(name = "asset_amount") val assetAmount: Long,
@ColumnInfo(name = "last_update") val lastUpdate: Long @ColumnInfo(name = "last_update") val lastUpdate: Long

View file

@ -9,15 +9,11 @@ import androidx.room.PrimaryKey
[ForeignKey( [ForeignKey(
entity = Transfer::class, entity = Transfer::class,
parentColumns = ["operation_id"], parentColumns = ["operation_id"],
childColumns = ["transfer_id"], childColumns = ["transfer_id"]
onUpdate = ForeignKey.CASCADE,
onDelete = ForeignKey.CASCADE
), ForeignKey( ), ForeignKey(
entity = Asset::class, entity = Asset::class,
parentColumns = ["id"], parentColumns = ["id"],
childColumns = ["asset_id"], childColumns = ["asset_id"]
onUpdate = ForeignKey.CASCADE,
onDelete = ForeignKey.CASCADE
)] )]
) )
data class EquivalentValue ( data class EquivalentValue (

View file

@ -9,20 +9,18 @@ import androidx.room.PrimaryKey
[ForeignKey( [ForeignKey(
entity = Operation::class, entity = Operation::class,
parentColumns = ["id"], parentColumns = ["id"],
childColumns = ["operation_id"], childColumns = ["operation_id"]
onUpdate = ForeignKey.CASCADE,
onDelete = ForeignKey.CASCADE
)] )]
) )
data class Transfer ( data class Transfer (
@PrimaryKey @PrimaryKey
@ColumnInfo(name = "operation_id") val operationId: String, @ColumnInfo(name = "operation_id") val operationId: String,
@ColumnInfo(name = "fee_amount") val feeAmount: Long, @ColumnInfo(name = "fee_amount") val feeAmount: Long,
@ColumnInfo(name = "fee_asset_id") val feeAssetId: String, @ColumnInfo(name = "fee_asset_id") val feeAssetId: String, // TODO should be foreign key to Asset
@ColumnInfo(name = "source") val source: String, @ColumnInfo(name = "source") val source: String, // TODO should be foreign key to UserAccount
@ColumnInfo(name = "destination") val destination: String, @ColumnInfo(name = "destination") val destination: String, // TODO should be foreign key to UserAccount
@ColumnInfo(name = "transfer_amount") val transferAmount: Long, @ColumnInfo(name = "transfer_amount") val transferAmount: Long,
@ColumnInfo(name = "transfer_asset_id") val transferAssetId: String, @ColumnInfo(name = "transfer_asset_id") val transferAssetId: String, // TODO should be foreign key to Asset
@ColumnInfo(name = "memo") val memo: String, @ColumnInfo(name = "memo") val memo: String,
@ColumnInfo(name = "memo_from_key") val memoFromKey: String, @ColumnInfo(name = "memo_from_key") val memoFromKey: String,
@ColumnInfo(name = "memo_to_key") val memoToKey: String @ColumnInfo(name = "memo_to_key") val memoToKey: String

View file

@ -9,5 +9,6 @@ data class UserAccount (
@PrimaryKey @PrimaryKey
@ColumnInfo(name = "id") val id: String, @ColumnInfo(name = "id") val id: String,
@ColumnInfo(name = "name") val name: String, @ColumnInfo(name = "name") val name: String,
@ColumnInfo(name = "is_ltm") val isLtm: Boolean // Todo verify data type @ColumnInfo(name = "is_ltm") val isLtm: Boolean, // Todo verify data type
@ColumnInfo(name = "weight_threshold") val weightThreshold: Int // TODO verify data type
) )

View file

@ -0,0 +1,25 @@
package cy.agorise.bitsybitshareswallet.models
import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.ForeignKey
/**
* Table to create a N:N relationship between [UserAccount] and [Authority]
*/
@Entity(tableName = "user_accounts__authorities",
primaryKeys = ["user_account_id", "authority_id"],
foreignKeys = [ForeignKey(
entity = UserAccount::class,
parentColumns = ["id"],
childColumns = ["user_account_id"]
), ForeignKey(
entity = Authority::class,
parentColumns = ["id"],
childColumns = ["authority_id"]
)])
data class UserAccountAuthority (
@ColumnInfo(name = "user_account_id") val userAccountId: String,
@ColumnInfo(name = "authority_id") val authorityId: Long,
@ColumnInfo(name = "weight") val weight: Int // TODO verify data type
)