Skip to content

Commit 32c524f

Browse files
committed
✨ 홍보 글 작성 시 그룹 당 하나의 글만 작성되도록 변경
1 parent b706b25 commit 32c524f

File tree

1 file changed

+44
-15
lines changed

1 file changed

+44
-15
lines changed

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

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import com.google.firebase.firestore.FirebaseFirestore
44
import com.google.firebase.firestore.Query
55
import com.whyranoid.data.constant.CollectionId
66
import com.whyranoid.data.constant.FieldId.AUTHOR_ID
7+
import com.whyranoid.data.constant.FieldId.GROUP_ID
78
import com.whyranoid.data.constant.FieldId.RUNNING_HISTORY_ID
89
import com.whyranoid.data.constant.FieldId.UPDATED_AT
910
import com.whyranoid.data.model.GroupInfoResponse
@@ -21,6 +22,7 @@ import kotlinx.coroutines.channels.awaitClose
2122
import kotlinx.coroutines.flow.Flow
2223
import kotlinx.coroutines.flow.callbackFlow
2324
import kotlinx.coroutines.suspendCancellableCoroutine
25+
import kotlinx.coroutines.tasks.await
2426
import java.util.*
2527
import javax.inject.Inject
2628
import kotlin.coroutines.resume
@@ -221,22 +223,49 @@ class PostDataSourceImpl @Inject constructor(
221223
): Boolean {
222224
val postId = UUID.randomUUID().toString()
223225

224-
return suspendCancellableCoroutine { cancellableContinuation ->
226+
val previousRecruitPost = db.collection(CollectionId.POST_COLLECTION)
227+
.whereEqualTo(GROUP_ID, groupUid)
228+
.get()
229+
.await()
225230

226-
db.collection(CollectionId.POST_COLLECTION)
227-
.document(postId)
228-
.set(
229-
RecruitPostResponse(
230-
postId = postId,
231-
authorId = authorUid,
232-
updatedAt = System.currentTimeMillis(),
233-
groupId = groupUid
234-
)
235-
).addOnSuccessListener {
236-
cancellableContinuation.resume(true)
237-
}.addOnFailureListener {
238-
cancellableContinuation.resume(false)
239-
}
231+
return if (previousRecruitPost.isEmpty) {
232+
suspendCancellableCoroutine { cancellableContinuation ->
233+
234+
db.collection(CollectionId.POST_COLLECTION)
235+
.document(postId)
236+
.set(
237+
RecruitPostResponse(
238+
postId = postId,
239+
authorId = authorUid,
240+
updatedAt = System.currentTimeMillis(),
241+
groupId = groupUid
242+
)
243+
).addOnSuccessListener {
244+
cancellableContinuation.resume(true)
245+
}.addOnFailureListener {
246+
cancellableContinuation.resume(false)
247+
}
248+
}
249+
} else {
250+
suspendCancellableCoroutine { cancellableContinuation ->
251+
252+
val previousRecruitPostId =
253+
previousRecruitPost.toObjects(RecruitPostResponse::class.java).first().postId
254+
db.collection(CollectionId.POST_COLLECTION)
255+
.document(previousRecruitPostId)
256+
.set(
257+
RecruitPostResponse(
258+
postId = postId,
259+
authorId = authorUid,
260+
updatedAt = System.currentTimeMillis(),
261+
groupId = groupUid
262+
)
263+
).addOnSuccessListener {
264+
cancellableContinuation.resume(true)
265+
}.addOnFailureListener {
266+
cancellableContinuation.resume(false)
267+
}
268+
}
240269
}
241270
}
242271

0 commit comments

Comments
 (0)