Skip to content

Commit d681ca7

Browse files
committed
✨ 내가 쓴 글 불러오기 UseCase 추가
1 parent da7cd59 commit d681ca7

File tree

5 files changed

+119
-1
lines changed

5 files changed

+119
-1
lines changed

data/src/main/java/com/whyranoid/data/constant/FieldId.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ object FieldId {
44

55
const val GROUP_MEMBERS_ID = "membersId"
66
const val GROUP_ID = "groupId"
7+
const val AUTHOR_ID = "authorId"
78
const val GROUP_NAME = "groupName"
89
const val GROUP_INTRODUCE = "introduce"
910
const val RULES = "rules"

data/src/main/java/com/whyranoid/data/post/PostDataSourceImpl.kt

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.whyranoid.data.post
33
import com.google.firebase.firestore.FirebaseFirestore
44
import com.google.firebase.firestore.Query
55
import com.whyranoid.data.constant.CollectionId
6+
import com.whyranoid.data.constant.FieldId.AUTHOR_ID
67
import com.whyranoid.data.constant.FieldId.RUNNING_HISTORY_ID
78
import com.whyranoid.data.constant.FieldId.UPDATED_AT
89
import com.whyranoid.data.model.GroupInfoResponse
@@ -121,6 +122,99 @@ class PostDataSourceImpl @Inject constructor(
121122
awaitClose()
122123
}
123124

125+
fun getMyPostFlow(uid: String): Flow<List<Post>> =
126+
callbackFlow {
127+
db.collection(CollectionId.POST_COLLECTION)
128+
.whereEqualTo(AUTHOR_ID, uid)
129+
.orderBy(UPDATED_AT, Query.Direction.DESCENDING)
130+
.addSnapshotListener { snapshot, _ ->
131+
val postList = mutableListOf<Post>()
132+
snapshot?.forEach { docuemnt ->
133+
134+
if (docuemnt[RUNNING_HISTORY_ID] != null) {
135+
docuemnt.toObject(RunningPostResponse::class.java).let { postResponse ->
136+
db.collection(CollectionId.USERS_COLLECTION)
137+
.document(postResponse.authorId)
138+
.get()
139+
.addOnSuccessListener { authorDocument ->
140+
val authorResponse =
141+
authorDocument?.toObject(UserResponse::class.java)
142+
143+
authorResponse?.let {
144+
db.collection(CollectionId.RUNNING_HISTORY_COLLECTION)
145+
.document(postResponse.runningHistoryId)
146+
.get()
147+
.addOnSuccessListener { runningHistoryDocument ->
148+
val runningHistoryResponse =
149+
runningHistoryDocument.toObject(
150+
RunningHistory::class.java
151+
)
152+
153+
runningHistoryResponse?.let {
154+
val author = authorResponse.toUser()
155+
156+
postList.add(
157+
RunningPost(
158+
postId = postResponse.postId,
159+
author = author,
160+
updatedAt = postResponse.updatedAt,
161+
runningHistory = it,
162+
likeCount = 0,
163+
content = postResponse.content
164+
)
165+
)
166+
}
167+
}
168+
}
169+
}
170+
}
171+
} else {
172+
docuemnt.toObject(RecruitPostResponse::class.java).let { postResponse ->
173+
174+
db.collection(CollectionId.USERS_COLLECTION)
175+
.document(postResponse.authorId)
176+
.get()
177+
.addOnSuccessListener { authorDocument ->
178+
val authorResponse =
179+
authorDocument?.toObject(UserResponse::class.java)
180+
181+
authorResponse?.let {
182+
db.collection(CollectionId.GROUPS_COLLECTION)
183+
.document(postResponse.groupId)
184+
.get()
185+
.addOnSuccessListener { groupDocument ->
186+
val groupInfoResponse =
187+
groupDocument.toObject(GroupInfoResponse::class.java)
188+
189+
groupInfoResponse?.let { groupInfoResponse ->
190+
val author = authorResponse.toUser()
191+
postList.add(
192+
RecruitPost(
193+
postId = postResponse.postId,
194+
author = author,
195+
updatedAt = postResponse.updatedAt,
196+
groupInfo = groupInfoResponse
197+
.toGroupInfo(
198+
author,
199+
rules = groupInfoResponse.rules.map {
200+
it.toRule()
201+
}
202+
)
203+
)
204+
)
205+
trySend(postList)
206+
}
207+
}
208+
}
209+
}
210+
}
211+
}
212+
}
213+
}
214+
215+
awaitClose()
216+
}
217+
124218
suspend fun createRecruitPost(
125219
authorUid: String,
126220
groupUid: String

data/src/main/java/com/whyranoid/data/post/PostRepositoryImpl.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ class PostRepositoryImpl @Inject constructor(
1818
return postDataSourceImpl.getAllPostFlow()
1919
}
2020

21+
override fun getMyPostFlow(uid: String): Flow<List<Post>> {
22+
return postDataSourceImpl.getMyPostFlow(uid)
23+
}
24+
2125
override suspend fun createRunningPost(
2226
authorUid: String,
2327
runningHistoryId: String,

domain/src/main/java/com/whyranoid/domain/repository/PostRepository.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,16 @@ interface PostRepository {
1111

1212
fun getAllPostFlow(): Flow<List<Post>>
1313

14-
// 글 작성하기 - 리모트
14+
fun getMyPostFlow(uid: String): Flow<List<Post>>
15+
16+
// 인증 글 작성
1517
suspend fun createRunningPost(
1618
authorUid: String,
1719
runningHistoryId: String,
1820
content: String
1921
): Result<Boolean>
2022

23+
// 홍보 글 작성
2124
suspend fun createRecruitPost(
2225
authorUid: String,
2326
groupUid: String
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.whyranoid.domain.usecase
2+
3+
import com.whyranoid.domain.model.Post
4+
import com.whyranoid.domain.repository.AccountRepository
5+
import com.whyranoid.domain.repository.PostRepository
6+
import kotlinx.coroutines.flow.Flow
7+
import javax.inject.Inject
8+
9+
class GetMyPostUseCase @Inject constructor(
10+
private val accountRepository: AccountRepository,
11+
private val postRepository: PostRepository
12+
) {
13+
suspend operator fun invoke(): Flow<List<Post>> {
14+
return postRepository.getMyPostFlow(accountRepository.getUid())
15+
}
16+
}

0 commit comments

Comments
 (0)