More with UserDAO
This commit is contained in:
		
							parent
							
								
									566102d5d4
								
							
						
					
					
						commit
						7c95e78b2a
					
				
					 3 changed files with 33 additions and 15 deletions
				
			
		|  | @ -0,0 +1,6 @@ | ||||||
|  | package be.ugent.sel.studeez.data.remote | ||||||
|  | 
 | ||||||
|  | object FirebaseUser { | ||||||
|  |     const val USERNAME: String = "username" | ||||||
|  |     const val BIOGRAPHY: String = "biography" | ||||||
|  | } | ||||||
|  | @ -5,6 +5,8 @@ import kotlinx.coroutines.flow.Flow | ||||||
| 
 | 
 | ||||||
| interface UserDAO { | interface UserDAO { | ||||||
| 
 | 
 | ||||||
|  |     fun getCurrentUserId(): String | ||||||
|  | 
 | ||||||
|     /** |     /** | ||||||
|      * @return all users |      * @return all users | ||||||
|      */ |      */ | ||||||
|  | @ -13,8 +15,10 @@ interface UserDAO { | ||||||
|     /** |     /** | ||||||
|      * @return all users based on a query, a trimmed down version of getAllUsers() |      * @return all users based on a query, a trimmed down version of getAllUsers() | ||||||
|      */ |      */ | ||||||
|     fun getUsersWithQuery(): Flow<List<User>> |     fun getUsersWithQuery( | ||||||
|     // TODO Add query parameter |         fieldName: String, | ||||||
|  |         value: String | ||||||
|  |     ): Flow<List<User>> | ||||||
| 
 | 
 | ||||||
|     /** |     /** | ||||||
|      * Request information about a user |      * Request information about a user | ||||||
|  |  | ||||||
|  | @ -3,8 +3,11 @@ package be.ugent.sel.studeez.domain.implementation | ||||||
| import be.ugent.sel.studeez.R | import be.ugent.sel.studeez.R | ||||||
| import be.ugent.sel.studeez.common.snackbar.SnackbarManager | import be.ugent.sel.studeez.common.snackbar.SnackbarManager | ||||||
| import be.ugent.sel.studeez.data.local.models.User | 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.AccountDAO | ||||||
| import be.ugent.sel.studeez.domain.UserDAO | 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.DocumentReference | ||||||
| import com.google.firebase.firestore.FirebaseFirestore | import com.google.firebase.firestore.FirebaseFirestore | ||||||
| import com.google.firebase.firestore.ktx.snapshots | import com.google.firebase.firestore.ktx.snapshots | ||||||
|  | @ -19,10 +22,8 @@ class FirebaseUserDAO @Inject constructor( | ||||||
|     private val auth: AccountDAO |     private val auth: AccountDAO | ||||||
| ) : UserDAO { | ) : UserDAO { | ||||||
| 
 | 
 | ||||||
|     companion object { |     override fun getCurrentUserId(): String { | ||||||
|         private const val USER_COLLECTION = FirebaseCollections.USER_COLLECTION |         return auth.currentUserId | ||||||
|         private const val USERNAME_FIELD = "username" |  | ||||||
|         private const val BIOGRAPHY_FIELD = "biography" |  | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     private fun currentUserDocument(): DocumentReference = |     private fun currentUserDocument(): DocumentReference = | ||||||
|  | @ -32,19 +33,26 @@ class FirebaseUserDAO @Inject constructor( | ||||||
| 
 | 
 | ||||||
|     override fun getAllUsers(): Flow<List<User>> { |     override fun getAllUsers(): Flow<List<User>> { | ||||||
|         return firestore |         return firestore | ||||||
|             .collection(FirebaseCollections.USER_COLLECTION) |             .collection(USER_COLLECTION) | ||||||
|             .snapshots() |             .snapshots() | ||||||
|             .map { it.toObjects(User::class.java) } |             .map { it.toObjects(User::class.java) } | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     override fun getUsersWithQuery(): Flow<List<User>> { |     override fun getUsersWithQuery( | ||||||
|         TODO("Not yet implemented") |         fieldName: String, | ||||||
|  |         value: String | ||||||
|  |     ): Flow<List<User>> { | ||||||
|  |         return firestore | ||||||
|  |             .collection(USER_COLLECTION) | ||||||
|  |             .whereEqualTo(fieldName, value) | ||||||
|  |             .snapshots() | ||||||
|  |             .map { it.toObjects(User::class.java) } | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     override fun getUserDetails(userId: String): Flow<User> { |     override fun getUserDetails(userId: String): Flow<User> { | ||||||
|         return flow { |         return flow { | ||||||
|             val snapshot = firestore |             val snapshot = firestore | ||||||
|                 .collection(FirebaseCollections.USER_COLLECTION) |                 .collection(USER_COLLECTION) | ||||||
|                 .document(userId) |                 .document(userId) | ||||||
|                 .get() |                 .get() | ||||||
|                 .await() |                 .await() | ||||||
|  | @ -56,8 +64,8 @@ class FirebaseUserDAO @Inject constructor( | ||||||
|     override suspend fun getLoggedInUser(): User { |     override suspend fun getLoggedInUser(): User { | ||||||
|         val userDocument = currentUserDocument().get().await() |         val userDocument = currentUserDocument().get().await() | ||||||
|         return User( |         return User( | ||||||
|             username = userDocument.getString(USERNAME_FIELD) ?: "", |             username = userDocument.getString(USERNAME) ?: "", | ||||||
|             biography = userDocument.getString(BIOGRAPHY_FIELD) ?: "" |             biography = userDocument.getString(BIOGRAPHY) ?: "" | ||||||
|         ) |         ) | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  | @ -66,8 +74,8 @@ class FirebaseUserDAO @Inject constructor( | ||||||
|         newBiography: String |         newBiography: String | ||||||
|     ) { |     ) { | ||||||
|         currentUserDocument().set(mapOf( |         currentUserDocument().set(mapOf( | ||||||
|             USERNAME_FIELD to newUsername, |             USERNAME to newUsername, | ||||||
|             BIOGRAPHY_FIELD to newBiography |             BIOGRAPHY to newBiography | ||||||
|         )) |         )) | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
		Reference in a new issue