diff --git a/app/src/main/java/be/ugent/sel/studeez/data/remote/FirebaseUser.kt b/app/src/main/java/be/ugent/sel/studeez/data/remote/FirebaseUser.kt new file mode 100644 index 0000000..9ee5aa2 --- /dev/null +++ b/app/src/main/java/be/ugent/sel/studeez/data/remote/FirebaseUser.kt @@ -0,0 +1,6 @@ +package be.ugent.sel.studeez.data.remote + +object FirebaseUser { + const val USERNAME: String = "username" + const val BIOGRAPHY: String = "biography" +} \ No newline at end of file diff --git a/app/src/main/java/be/ugent/sel/studeez/domain/UserDAO.kt b/app/src/main/java/be/ugent/sel/studeez/domain/UserDAO.kt index 6017342..09179f1 100644 --- a/app/src/main/java/be/ugent/sel/studeez/domain/UserDAO.kt +++ b/app/src/main/java/be/ugent/sel/studeez/domain/UserDAO.kt @@ -5,6 +5,8 @@ import kotlinx.coroutines.flow.Flow interface UserDAO { + fun getCurrentUserId(): String + /** * @return all users */ @@ -13,8 +15,10 @@ interface UserDAO { /** * @return all users based on a query, a trimmed down version of getAllUsers() */ - fun getUsersWithQuery(): Flow> - // TODO Add query parameter + fun getUsersWithQuery( + fieldName: String, + value: String + ): Flow> /** * Request information about a user diff --git a/app/src/main/java/be/ugent/sel/studeez/domain/implementation/FirebaseUserDAO.kt b/app/src/main/java/be/ugent/sel/studeez/domain/implementation/FirebaseUserDAO.kt index 8c84914..f41fcd9 100644 --- a/app/src/main/java/be/ugent/sel/studeez/domain/implementation/FirebaseUserDAO.kt +++ b/app/src/main/java/be/ugent/sel/studeez/domain/implementation/FirebaseUserDAO.kt @@ -3,8 +3,11 @@ package be.ugent.sel.studeez.domain.implementation import be.ugent.sel.studeez.R import be.ugent.sel.studeez.common.snackbar.SnackbarManager import be.ugent.sel.studeez.data.local.models.User +import be.ugent.sel.studeez.data.remote.FirebaseUser.BIOGRAPHY +import be.ugent.sel.studeez.data.remote.FirebaseUser.USERNAME import be.ugent.sel.studeez.domain.AccountDAO import be.ugent.sel.studeez.domain.UserDAO +import be.ugent.sel.studeez.domain.implementation.FirebaseCollections.USER_COLLECTION import com.google.firebase.firestore.DocumentReference import com.google.firebase.firestore.FirebaseFirestore import com.google.firebase.firestore.ktx.snapshots @@ -17,12 +20,10 @@ import javax.inject.Inject class FirebaseUserDAO @Inject constructor( private val firestore: FirebaseFirestore, private val auth: AccountDAO - ) : UserDAO { +) : UserDAO { - companion object { - private const val USER_COLLECTION = FirebaseCollections.USER_COLLECTION - private const val USERNAME_FIELD = "username" - private const val BIOGRAPHY_FIELD = "biography" + override fun getCurrentUserId(): String { + return auth.currentUserId } private fun currentUserDocument(): DocumentReference = @@ -32,19 +33,26 @@ class FirebaseUserDAO @Inject constructor( override fun getAllUsers(): Flow> { return firestore - .collection(FirebaseCollections.USER_COLLECTION) + .collection(USER_COLLECTION) .snapshots() .map { it.toObjects(User::class.java) } } - override fun getUsersWithQuery(): Flow> { - TODO("Not yet implemented") + override fun getUsersWithQuery( + fieldName: String, + value: String + ): Flow> { + return firestore + .collection(USER_COLLECTION) + .whereEqualTo(fieldName, value) + .snapshots() + .map { it.toObjects(User::class.java) } } override fun getUserDetails(userId: String): Flow { return flow { val snapshot = firestore - .collection(FirebaseCollections.USER_COLLECTION) + .collection(USER_COLLECTION) .document(userId) .get() .await() @@ -56,8 +64,8 @@ class FirebaseUserDAO @Inject constructor( override suspend fun getLoggedInUser(): User { val userDocument = currentUserDocument().get().await() return User( - username = userDocument.getString(USERNAME_FIELD) ?: "", - biography = userDocument.getString(BIOGRAPHY_FIELD) ?: "" + username = userDocument.getString(USERNAME) ?: "", + biography = userDocument.getString(BIOGRAPHY) ?: "" ) } @@ -66,8 +74,8 @@ class FirebaseUserDAO @Inject constructor( newBiography: String ) { currentUserDocument().set(mapOf( - USERNAME_FIELD to newUsername, - BIOGRAPHY_FIELD to newBiography + USERNAME to newUsername, + BIOGRAPHY to newBiography )) }