Skip to content

Commit 9786256

Browse files
committed
✨ PostDataSource 일부 구현
1 parent 3300959 commit 9786256

File tree

6 files changed

+142
-4
lines changed

6 files changed

+142
-4
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.whyranoid.data.Post
2+
3+
import com.google.firebase.firestore.FirebaseFirestore
4+
import com.whyranoid.data.constant.CollectionId
5+
import com.whyranoid.domain.model.GroupInfo
6+
import com.whyranoid.domain.model.RecruitPost
7+
import com.whyranoid.domain.model.User
8+
import kotlinx.coroutines.suspendCancellableCoroutine
9+
import java.util.*
10+
import javax.inject.Inject
11+
import kotlin.coroutines.resume
12+
13+
class PostDataSource @Inject constructor(
14+
private val db: FirebaseFirestore
15+
) {
16+
17+
suspend fun createRecruitPost(
18+
author: User,
19+
updatedAt: Long,
20+
groupInfo: GroupInfo
21+
): Boolean {
22+
return suspendCancellableCoroutine { cancellableContinuation ->
23+
val postId = UUID.randomUUID().toString()
24+
db.collection(CollectionId.POST_COLLECTION)
25+
.document(postId)
26+
.set(
27+
RecruitPost(
28+
postId = postId,
29+
author = author,
30+
updatedAt = updatedAt,
31+
groupInfo = groupInfo
32+
)
33+
).addOnSuccessListener {
34+
cancellableContinuation.resume(true)
35+
}.addOnFailureListener {
36+
cancellableContinuation.resume(false)
37+
}
38+
}
39+
}
40+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.whyranoid.data.Post
2+
3+
import com.whyranoid.domain.model.GroupInfo
4+
import com.whyranoid.domain.model.Post
5+
import com.whyranoid.domain.model.RunningHistory
6+
import com.whyranoid.domain.model.User
7+
import com.whyranoid.domain.repository.PostRepository
8+
import kotlinx.coroutines.flow.Flow
9+
import javax.inject.Inject
10+
11+
class PostRepositoryImpl @Inject constructor(
12+
private val postDataSource: PostDataSource
13+
) : PostRepository {
14+
15+
// TODO : 페이징처리하기
16+
override fun getPagingPosts(): Flow<List<Post>> {
17+
TODO("Not yet implemented")
18+
}
19+
20+
override suspend fun createPost(
21+
user: User,
22+
postContent: String,
23+
runningHistory: RunningHistory,
24+
updatedAt: Long
25+
): Boolean {
26+
TODO("Not yet implemented")
27+
}
28+
29+
override suspend fun createRecruitPost(
30+
author: User,
31+
updatedAt: Long,
32+
groupInfo: GroupInfo
33+
): Boolean {
34+
return postDataSource.createRecruitPost(author, updatedAt, groupInfo)
35+
}
36+
37+
override suspend fun deletePost(postId: String): Boolean {
38+
TODO("Not yet implemented")
39+
}
40+
41+
override suspend fun updatePost(postId: String, postContent: String, updatedAt: Long): Boolean {
42+
TODO("Not yet implemented")
43+
}
44+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.whyranoid.data.di
2+
3+
import com.whyranoid.data.Post.PostRepositoryImpl
4+
import com.whyranoid.domain.repository.PostRepository
5+
import dagger.Binds
6+
import dagger.Module
7+
import dagger.hilt.InstallIn
8+
import dagger.hilt.components.SingletonComponent
9+
10+
@Module
11+
@InstallIn(SingletonComponent::class)
12+
abstract class PostModule {
13+
14+
@Binds
15+
abstract fun bindPostRepository(postRepositoryImpl: PostRepositoryImpl): PostRepository
16+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.whyranoid.data.model
2+
3+
import com.whyranoid.domain.model.RecruitPost
4+
import com.whyranoid.domain.model.toRule
5+
6+
sealed interface PostResponse {
7+
val postId: String
8+
val author: UserResponse
9+
val updatedAt: Long
10+
}
11+
12+
data class RecruitPostResponse(
13+
override val postId: String = "",
14+
override val author: UserResponse = UserResponse(),
15+
override val updatedAt: Long = 0L,
16+
val groupInfo: GroupInfoResponse = GroupInfoResponse()
17+
) : PostResponse
18+
19+
fun RecruitPostResponse.toRecruitPost(): RecruitPost {
20+
val leader = this.author.toUser()
21+
return RecruitPost(
22+
postId = this.postId,
23+
author = leader,
24+
updatedAt = this.updatedAt,
25+
groupInfo = this.groupInfo.toGroupInfo(
26+
leader,
27+
this.groupInfo.rules.map {
28+
it.toRule()
29+
}
30+
)
31+
)
32+
}

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
package com.whyranoid.domain.repository
22

3-
import androidx.paging.PagingData
3+
import com.whyranoid.domain.model.GroupInfo
44
import com.whyranoid.domain.model.Post
55
import com.whyranoid.domain.model.RunningHistory
66
import com.whyranoid.domain.model.User
77
import kotlinx.coroutines.flow.Flow
88

99
interface PostRepository {
1010

11+
// TODO : 페이징 처리
1112
// 글(홍보 / 인증) 페이징으로 가져오기 - 리모트
12-
fun getPagingPosts(): Flow<PagingData<Post>>
13+
fun getPagingPosts(): Flow<List<Post>>
1314

1415
// 글 작성하기 - 리모트
1516
suspend fun createPost(
@@ -19,6 +20,12 @@ interface PostRepository {
1920
updatedAt: Long
2021
): Boolean
2122

23+
suspend fun createRecruitPost(
24+
author: User,
25+
updatedAt: Long = 0L,
26+
groupInfo: GroupInfo
27+
): Boolean
28+
2229
// 글 삭제하기 - 리모트
2330
suspend fun deletePost(postId: String): Boolean
2431

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
package com.whyranoid.domain.usecase
22

3-
import androidx.paging.PagingData
43
import com.whyranoid.domain.model.Post
54
import com.whyranoid.domain.repository.PostRepository
65
import kotlinx.coroutines.flow.Flow
76
import javax.inject.Inject
87

98
class GetPagingPostsUseCase @Inject constructor(private val postRepository: PostRepository) {
10-
operator fun invoke(): Flow<PagingData<Post>> {
9+
operator fun invoke(): Flow<List<Post>> {
1110
return postRepository.getPagingPosts()
1211
}
1312
}

0 commit comments

Comments
 (0)