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
interface AssetDao {
@Query("SELECT * FROM assets")
fun getAllAssets(): LiveData<List<Asset>>
@Insert
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
interface AuthorityDao {
@Query("SELECT * FROM authorities")
fun getAllAuthorities(): LiveData<List<Authority>>
@Insert
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
interface BalanceDao {
@Query("SELECT * FROM balances")
fun getAllBalances(): LiveData<List<Balance>>
@Insert
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.Room
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 fun assetDao(): AssetDao
abstract fun balanceDao(): BalanceDao
abstract fun userAccountDao(): UserAccountDao
abstract fun authorityDao(): AuthorityDao
abstract fun userAccountAuthorityDao(): UserAccountAuthorityDao
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
interface UserAccountDao {
@Query("SELECT * FROM user_accounts")
fun getAllUserAccounts(): LiveData<List<UserAccount>>
@Insert
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.Entity
import androidx.room.ForeignKey
import androidx.room.PrimaryKey
@Entity(tableName = "authorities", foreignKeys =
[ForeignKey(
entity = UserAccount::class,
parentColumns = ["id"],
childColumns = ["user_id"],
onUpdate = ForeignKey.CASCADE,
onDelete = ForeignKey.CASCADE
)]
)
@Entity(tableName = "authorities")
data class Authority (
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "id") val id: Long,

View file

@ -3,9 +3,10 @@ package cy.agorise.bitsybitshareswallet.models
import androidx.room.ColumnInfo
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(
@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_amount") val assetAmount: Long,
@ColumnInfo(name = "last_update") val lastUpdate: Long

View file

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

View file

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

View file

@ -9,5 +9,6 @@ data class UserAccount (
@PrimaryKey
@ColumnInfo(name = "id") val id: 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
)