Home > Back-end >  Checking if entry exists
Checking if entry exists

Time:07-07

I'm trying to create a login app. I want it to do nothing more than either store the account info on a db if the username is not already used. Problem is, no matter what I try, I cannot check if the username is already in the db

I've tried to get both LiveData from my query, as well as String?, as shown below with no success

I have also tried setting an onConflict strategy other than ignore, and then putting my addUser function call in a try-catch block, but it crashes

@Entity(tableName = "Users_table")
data class User(
    @PrimaryKey var username: String,
    @ColumnInfo var firstName: String,
    @ColumnInfo var lastName: String,
    @ColumnInfo var email: String,
    @ColumnInfo var password: String
)

@Dao
interface UsersDao {

    @Insert(onConflict = OnConflictStrategy.IGNORE)
    fun addUser(user: User)

    @Query("Select username from users_table Where username = :username")
    fun getUser(username: String): String?

}

@Database(entities = [User::class], version = 1)
abstract class UsersDatabase: RoomDatabase() {

    abstract fun userDao(): UsersDao


    companion object{
        @Volatile
        private var INSTANCE: UsersDatabase? = null

        fun getDatabase(context: Context): UsersDatabase {
            val tempInstance = INSTANCE
            if (tempInstance != null) {
                return tempInstance
            }
            synchronized(this) {
                val instance = Room.databaseBuilder(
                    context.applicationContext,
                    UsersDatabase::class.java,
                    "users_database"
                ).build()
                INSTANCE = instance
                return instance
            }
        }
    }
}

class UsersRepository(private val usersDao: UsersDao) {


    suspend fun addUser(user: User){
        usersDao.addUser(user)
    }

    suspend fun getUser(username: String) {
        usersDao.getUser(username)
    }

}

class UsersViewModel(application: Application): AndroidViewModel(application) {

    //private val getUser: LiveData<List<User>>
    private val repository: UsersRepository

    init {
        val userDao = UsersDatabase.getDatabase(application).userDao()
        repository = UsersRepository(userDao)
        //getUser = repository.getUser
    }

    fun addUser(user: User) {
        viewModelScope.launch(Dispatchers.IO) {
            repository.addUser(user)
        }
    }

    fun getUser(username: String) {
        viewModelScope.launch(Dispatchers.IO) {
            repository.getUser(username)
        }
    }

}

...


        if (mUsersViewModel.getUser(binding.etRegUsername.text.toString()).toString() != "0") {
            Toast.makeText(this, "Username already exists", Toast.LENGTH_SHORT).show()
        } else {
            mUsersViewModel.addUser(user)
            Toast.makeText(this, "User successfully created", Toast.LENGTH_SHORT).show()
        }



 ...

CodePudding user response:

You can use this query to get count of users with given user name

@Query("Select count(*) from users_table Where username LIKE :username")
fun getUser(username: String): Int?

and use this to check if there are user

if (mUsersViewModel.getUser(binding.etRegUsername.text.toString()) != 0) {
        Toast.makeText(this, "Username already exists", Toast.LENGTH_SHORT).show()
    } else {
        mUsersViewModel.addUser(user)
        Toast.makeText(this, "User successfully created", Toast.LENGTH_SHORT).show()
    }

CodePudding user response:

you have two options , -> you can try to fetch all data first and then compare it with your editText's data(username,email)

-> set the query on database side on your DAO to check is the same entry exists

  • Related