@@ -4,6 +4,7 @@ import com.google.firebase.firestore.FirebaseFirestore
4
4
import com.google.firebase.firestore.Query
5
5
import com.whyranoid.data.constant.CollectionId
6
6
import com.whyranoid.data.constant.FieldId.AUTHOR_ID
7
+ import com.whyranoid.data.constant.FieldId.GROUP_ID
7
8
import com.whyranoid.data.constant.FieldId.RUNNING_HISTORY_ID
8
9
import com.whyranoid.data.constant.FieldId.UPDATED_AT
9
10
import com.whyranoid.data.model.GroupInfoResponse
@@ -21,6 +22,7 @@ import kotlinx.coroutines.channels.awaitClose
21
22
import kotlinx.coroutines.flow.Flow
22
23
import kotlinx.coroutines.flow.callbackFlow
23
24
import kotlinx.coroutines.suspendCancellableCoroutine
25
+ import kotlinx.coroutines.tasks.await
24
26
import java.util.*
25
27
import javax.inject.Inject
26
28
import kotlin.coroutines.resume
@@ -221,22 +223,49 @@ class PostDataSourceImpl @Inject constructor(
221
223
): Boolean {
222
224
val postId = UUID .randomUUID().toString()
223
225
224
- return suspendCancellableCoroutine { cancellableContinuation ->
226
+ val previousRecruitPost = db.collection(CollectionId .POST_COLLECTION )
227
+ .whereEqualTo(GROUP_ID , groupUid)
228
+ .get()
229
+ .await()
225
230
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
+ }
240
269
}
241
270
}
242
271
0 commit comments