Skip to content

Commit

Permalink
[FEATURE] #15 : 사용자 등록 / 사용자 조회 로직 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
jeongjaino committed Oct 10, 2023
1 parent f52cc77 commit 0583a20
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.wap.wapp.core.data.repository.user

import com.wap.wapp.core.model.user.UserProfile

interface UserRepository {
suspend fun getUserProfile(userId: String): Result<UserProfile>

suspend fun postUserProfile(
userId: String,
userName: String,
studentId: String,
registeredAt: String,
): Result<Unit>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.wap.wapp.core.data.repository.user

import com.wap.wapp.core.model.user.UserProfile
import com.wap.wapp.core.network.model.user.UserProfileRequest
import com.wap.wapp.core.network.source.user.UserDataSource
import javax.inject.Inject

class UserRepositoryImpl @Inject constructor(
private val userDataSource: UserDataSource,
) : UserRepository {
override suspend fun getUserProfile(userId: String): Result<UserProfile> {
return userDataSource.getUserProfile(userId).mapCatching { response ->
response.toDomain()
}
}

override suspend fun postUserProfile(
userId: String,
userName: String,
studentId: String,
registeredAt: String,
): Result<Unit> {
return userDataSource.postUserProfile(
UserProfileRequest(
userId = userId,
userName = userName,
studentId = studentId,
registeredAt = registeredAt,
),
)
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.wap.wapp.core.network.source.user

import com.wap.wapp.core.network.model.user.UserProfileRequest
import com.wap.wapp.core.network.model.user.UserProfileResponse

interface UserDataSource {
suspend fun postUserProfile(userProfileRequest: UserProfileRequest): Result<Unit>

suspend fun getUserProfile(userId: String): Result<UserProfileResponse>
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,44 @@
package com.wap.wapp.core.network.source.user

class UserDataSourceImpl
import com.google.firebase.firestore.FirebaseFirestore
import com.google.firebase.firestore.SetOptions
import com.wap.wapp.core.network.constant.USER_COLLECTION
import com.wap.wapp.core.network.model.user.UserProfileRequest
import com.wap.wapp.core.network.model.user.UserProfileResponse
import com.wap.wapp.core.network.utils.await
import javax.inject.Inject

class UserDataSourceImpl @Inject constructor(
private val firebaseFirestore: FirebaseFirestore,
) : UserDataSource {
override suspend fun postUserProfile(
userProfileRequest: UserProfileRequest,
): Result<Unit> {
return runCatching {
val userId = userProfileRequest.userId
val setOption = SetOptions.merge()

firebaseFirestore.collection(USER_COLLECTION)
.document(userId)
.set(
userProfileRequest,
setOption,
)
.await()
}
}

override suspend fun getUserProfile(
userId: String,
): Result<UserProfileResponse> {
return runCatching {
val result = firebaseFirestore.collection(USER_COLLECTION)
.document(userId)
.get()
.await()

val userProfile = result.toObject(UserProfileResponse::class.java)
checkNotNull(userProfile)
}
}
}

0 comments on commit 0583a20

Please sign in to comment.