|
1 | 1 | package com.whyranoid.data.post
|
2 | 2 |
|
| 3 | +import com.google.firebase.firestore.DocumentSnapshot |
3 | 4 | import com.google.firebase.firestore.FirebaseFirestore
|
4 | 5 | import com.google.firebase.firestore.Query
|
| 6 | +import com.google.firebase.firestore.QueryDocumentSnapshot |
| 7 | +import com.google.firebase.firestore.QuerySnapshot |
5 | 8 | import com.whyranoid.data.constant.CollectionId
|
6 | 9 | import com.whyranoid.data.constant.FieldId.AUTHOR_ID
|
7 | 10 | import com.whyranoid.data.constant.FieldId.GROUP_ID
|
@@ -219,6 +222,90 @@ class PostDataSourceImpl @Inject constructor(
|
219 | 222 | awaitClose()
|
220 | 223 | }
|
221 | 224 |
|
| 225 | + override suspend fun getCurrentPagingPost(key: QuerySnapshot?): QuerySnapshot { |
| 226 | + return key ?: db.collection(CollectionId.POST_COLLECTION) |
| 227 | + .orderBy(UPDATED_AT, Query.Direction.DESCENDING) |
| 228 | + .limit(DATA_COUNT_PER_PAGE) |
| 229 | + .get() |
| 230 | + .await() |
| 231 | + } |
| 232 | + |
| 233 | + override suspend fun getNextPagingPost(lastDocumentSnapshot: DocumentSnapshot): QuerySnapshot { |
| 234 | + return db.collection(CollectionId.POST_COLLECTION) |
| 235 | + .orderBy(UPDATED_AT, Query.Direction.DESCENDING) |
| 236 | + .limit(DATA_COUNT_PER_PAGE).startAfter(lastDocumentSnapshot) |
| 237 | + .get() |
| 238 | + .await() |
| 239 | + } |
| 240 | + |
| 241 | + override suspend fun getMyCurrentPagingPost(key: QuerySnapshot?, uid: String): QuerySnapshot { |
| 242 | + return key ?: db.collection(CollectionId.POST_COLLECTION) |
| 243 | + .whereEqualTo(AUTHOR_ID, uid) |
| 244 | + .limit(DATA_COUNT_PER_PAGE) |
| 245 | + .get() |
| 246 | + .await() |
| 247 | + } |
| 248 | + |
| 249 | + override suspend fun getMyNextPagingPost( |
| 250 | + lastDocumentSnapshot: DocumentSnapshot, |
| 251 | + uid: String |
| 252 | + ): QuerySnapshot { |
| 253 | + return db.collection(CollectionId.POST_COLLECTION) |
| 254 | + .whereEqualTo(AUTHOR_ID, uid) |
| 255 | + .limit(DATA_COUNT_PER_PAGE) |
| 256 | + .startAfter(lastDocumentSnapshot) |
| 257 | + .get() |
| 258 | + .await() |
| 259 | + } |
| 260 | + |
| 261 | + // TODO : 예외 처리 |
| 262 | + override suspend fun convertPostType(document: QueryDocumentSnapshot): Post? { |
| 263 | + return if (document[RUNNING_HISTORY_ID] != null) { |
| 264 | + document.toObject(RunningPostResponse::class.java).let { postResponse -> |
| 265 | + val authorResponse = getUserResponse(postResponse.authorId) |
| 266 | + |
| 267 | + authorResponse?.let { |
| 268 | + val runningHistory = getRunningHistory(postResponse.runningHistoryId) |
| 269 | + |
| 270 | + runningHistory?.let { |
| 271 | + RunningPost( |
| 272 | + postId = postResponse.postId, |
| 273 | + author = authorResponse.toUser(), |
| 274 | + updatedAt = postResponse.updatedAt, |
| 275 | + runningHistory = it, |
| 276 | + likeCount = 0, |
| 277 | + content = postResponse.content |
| 278 | + ) |
| 279 | + } |
| 280 | + } |
| 281 | + } |
| 282 | + } else { |
| 283 | + document.toObject(RecruitPostResponse::class.java).let { postResponse -> |
| 284 | + val authorResponse = getUserResponse(postResponse.authorId) |
| 285 | + |
| 286 | + authorResponse?.let { |
| 287 | + val groupInfoResponse = getGroupInfoResponse(postResponse.groupId) |
| 288 | + |
| 289 | + groupInfoResponse?.let { |
| 290 | + val author = authorResponse.toUser() |
| 291 | + RecruitPost( |
| 292 | + postId = postResponse.postId, |
| 293 | + author = author, |
| 294 | + updatedAt = postResponse.updatedAt, |
| 295 | + groupInfo = groupInfoResponse |
| 296 | + .toGroupInfo( |
| 297 | + author, |
| 298 | + rules = groupInfoResponse.rules.map { |
| 299 | + it.toRule() |
| 300 | + } |
| 301 | + ) |
| 302 | + ) |
| 303 | + } |
| 304 | + } |
| 305 | + } |
| 306 | + } |
| 307 | + } |
| 308 | + |
222 | 309 | override suspend fun createRecruitPost(
|
223 | 310 | authorUid: String,
|
224 | 311 | groupUid: String
|
@@ -310,4 +397,35 @@ class PostDataSourceImpl @Inject constructor(
|
310 | 397 | }
|
311 | 398 | }
|
312 | 399 | }
|
| 400 | + |
| 401 | + // TODO : 예외 처리 |
| 402 | + private suspend fun getUserResponse(userId: String): UserResponse? { |
| 403 | + return db.collection(CollectionId.USERS_COLLECTION) |
| 404 | + .document(userId) |
| 405 | + .get() |
| 406 | + .await() |
| 407 | + .toObject(UserResponse::class.java) |
| 408 | + } |
| 409 | + |
| 410 | + // TODO : 예외 처리 |
| 411 | + private suspend fun getRunningHistory(runningHistoryId: String): RunningHistory? { |
| 412 | + return db.collection(CollectionId.RUNNING_HISTORY_COLLECTION) |
| 413 | + .document(runningHistoryId) |
| 414 | + .get() |
| 415 | + .await() |
| 416 | + .toObject(RunningHistory::class.java) |
| 417 | + } |
| 418 | + |
| 419 | + // TODO : 예외 처리 |
| 420 | + private suspend fun getGroupInfoResponse(groupId: String): GroupInfoResponse? { |
| 421 | + return db.collection(CollectionId.GROUPS_COLLECTION) |
| 422 | + .document(groupId) |
| 423 | + .get() |
| 424 | + .await() |
| 425 | + .toObject(GroupInfoResponse::class.java) |
| 426 | + } |
| 427 | + |
| 428 | + companion object { |
| 429 | + private const val DATA_COUNT_PER_PAGE = 10L |
| 430 | + } |
313 | 431 | }
|
0 commit comments