Skip to content

Commit 1e92d72

Browse files
authored
๐Ÿ”€ #46 from boostcampwm-2022/feat/editGroup
๊ทธ๋ฃน ์ˆ˜์ • ํŽ˜์ด์ง€ ์ผ๋ถ€ ๊ตฌํ˜„, ํ™๋ณด ๊ธ€ ์ž‘์„ฑ ๋กœ์ง ์ถ”๊ฐ€
2 parents 797258e + 995ec08 commit 1e92d72

31 files changed

+637
-54
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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.data.model.GroupInfoResponse
6+
import com.whyranoid.data.model.UserResponse
7+
import com.whyranoid.data.model.toGroupInfo
8+
import com.whyranoid.data.model.toUser
9+
import com.whyranoid.domain.model.RecruitPost
10+
import com.whyranoid.domain.model.toRule
11+
import kotlinx.coroutines.suspendCancellableCoroutine
12+
import kotlinx.coroutines.tasks.await
13+
import java.util.*
14+
import javax.inject.Inject
15+
import kotlin.coroutines.resume
16+
17+
class PostDataSource @Inject constructor(
18+
private val db: FirebaseFirestore
19+
) {
20+
21+
suspend fun createRecruitPost(
22+
authorUid: String,
23+
updatedAt: Long,
24+
groupUid: String
25+
): Boolean {
26+
val postId = UUID.randomUUID().toString()
27+
28+
val author = db.collection(CollectionId.USERS_COLLECTION)
29+
.document(authorUid)
30+
.get()
31+
.await()
32+
.toObject(UserResponse::class.java)
33+
?.toUser()
34+
35+
author?.let {
36+
val groupInfo = db.collection(CollectionId.GROUPS_COLLECTION)
37+
.document(groupUid)
38+
.get()
39+
.await()
40+
.toObject(GroupInfoResponse::class.java)
41+
42+
groupInfo?.let {
43+
return suspendCancellableCoroutine { cancellableContinuation ->
44+
45+
db.collection(CollectionId.POST_COLLECTION)
46+
.document(postId)
47+
.set(
48+
RecruitPost(
49+
postId = postId,
50+
author = author,
51+
updatedAt = updatedAt,
52+
groupInfo = groupInfo.toGroupInfo(
53+
leader = author,
54+
rules = groupInfo.rules.map { it.toRule() }
55+
)
56+
)
57+
).addOnSuccessListener {
58+
cancellableContinuation.resume(true)
59+
}.addOnFailureListener {
60+
cancellableContinuation.resume(false)
61+
}
62+
}
63+
}
64+
}
65+
return false
66+
}
67+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.whyranoid.data.Post
2+
3+
import com.whyranoid.domain.model.Post
4+
import com.whyranoid.domain.model.RunningHistory
5+
import com.whyranoid.domain.model.User
6+
import com.whyranoid.domain.repository.PostRepository
7+
import kotlinx.coroutines.flow.Flow
8+
import javax.inject.Inject
9+
10+
class PostRepositoryImpl @Inject constructor(
11+
private val postDataSource: PostDataSource
12+
) : PostRepository {
13+
14+
// TODO : ํŽ˜์ด์ง•์ฒ˜๋ฆฌํ•˜๊ธฐ
15+
override fun getPagingPosts(): Flow<List<Post>> {
16+
TODO("Not yet implemented")
17+
}
18+
19+
override suspend fun createPost(
20+
user: User,
21+
postContent: String,
22+
runningHistory: RunningHistory,
23+
updatedAt: Long
24+
): Boolean {
25+
TODO("Not yet implemented")
26+
}
27+
28+
override suspend fun createRecruitPost(
29+
authorUid: String,
30+
updatedAt: Long,
31+
groupUid: String
32+
): Boolean {
33+
return postDataSource.createRecruitPost(authorUid, updatedAt, groupUid)
34+
}
35+
36+
override suspend fun deletePost(postId: String): Boolean {
37+
TODO("Not yet implemented")
38+
}
39+
40+
override suspend fun updatePost(postId: String, postContent: String, updatedAt: Long): Boolean {
41+
TODO("Not yet implemented")
42+
}
43+
}

โ€Ždata/src/main/java/com/whyranoid/data/constant/CollectionId.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ object CollectionId {
88
const val GROUP_NOTIFICATIONS_COLLECTION = "GroupNotifications"
99
const val START_NOTIFICATION = "start"
1010
const val FINISH_NOTIFICATION = "finish"
11+
const val POST_COLLECTION = "Posts"
1112
}
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+
}

โ€Ždata/src/main/java/com/whyranoid/data/group/GroupDataSource.kt

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,15 @@ import com.whyranoid.data.constant.FieldId.GROUP_NAME
1010
import com.whyranoid.data.constant.FieldId.JOINED_GROUP_LIST
1111
import com.whyranoid.data.constant.FieldId.RULES
1212
import com.whyranoid.data.model.GroupInfoResponse
13+
import com.whyranoid.data.model.UserResponse
14+
import com.whyranoid.data.model.toGroupInfo
15+
import com.whyranoid.data.model.toUser
16+
import com.whyranoid.domain.model.GroupInfo
1317
import com.whyranoid.domain.model.Rule
18+
import com.whyranoid.domain.model.toRule
19+
import kotlinx.coroutines.channels.awaitClose
20+
import kotlinx.coroutines.flow.Flow
21+
import kotlinx.coroutines.flow.callbackFlow
1422
import kotlinx.coroutines.suspendCancellableCoroutine
1523
import java.util.UUID
1624
import javax.inject.Inject
@@ -116,4 +124,32 @@ class GroupDataSource @Inject constructor(
116124
}
117125
}
118126
}
127+
128+
fun getGroupInfoFlow(uid: String, groupId: String): Flow<GroupInfo> = callbackFlow {
129+
db.collection(GROUPS_COLLECTION)
130+
.document(groupId)
131+
.addSnapshotListener { documentSnapshot, _ ->
132+
val groupInfoResponse = documentSnapshot?.toObject(GroupInfoResponse::class.java)
133+
134+
groupInfoResponse?.let {
135+
db.collection(USERS_COLLECTION)
136+
.document(uid)
137+
.addSnapshotListener { documentSnapshot, _ ->
138+
val userResponse = documentSnapshot?.toObject(UserResponse::class.java)
139+
140+
userResponse?.let {
141+
trySend(
142+
groupInfoResponse.toGroupInfo(
143+
leader = userResponse.toUser(),
144+
rules = groupInfoResponse.rules.map {
145+
it.toRule()
146+
}
147+
)
148+
)
149+
}
150+
}
151+
}
152+
}
153+
awaitClose()
154+
}
119155
}

โ€Ždata/src/main/java/com/whyranoid/data/group/GroupRepositoryImpl.kt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ class GroupRepositoryImpl @Inject constructor(
3838
return groupDataSource.joinGroup(uid, groupId)
3939
}
4040

41+
override fun getGroupInfoFlow(uid: String, groupId: String): Flow<GroupInfo> {
42+
return groupDataSource.getGroupInfoFlow(uid, groupId)
43+
}
44+
4145
override fun getGroupNotifications(groupId: String): Flow<List<GroupNotification>> {
4246
return groupNotificationDataSource.getGroupNotifications(groupId)
4347
}
@@ -46,7 +50,12 @@ class GroupRepositoryImpl @Inject constructor(
4650
groupNotificationDataSource.notifyRunningStart(uid, groupIdList)
4751
}
4852

49-
override suspend fun createGroup(groupName: String, introduce: String, rules: List<String>, uid: String): Boolean {
53+
override suspend fun createGroup(
54+
groupName: String,
55+
introduce: String,
56+
rules: List<String>,
57+
uid: String
58+
): Boolean {
5059
return groupDataSource.createGroup(groupName, introduce, rules, uid)
5160
}
5261
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.whyranoid.data.model
2+
3+
sealed interface PostResponse {
4+
val postId: String
5+
val author: String
6+
val updatedAt: Long
7+
}
8+
9+
data class RecruitPostResponse(
10+
override val postId: String = "",
11+
override val author: String = "",
12+
override val updatedAt: Long = 0L,
13+
val groupUid: String = ""
14+
) : PostResponse
Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.whyranoid.data.model
22

3-
import com.whyranoid.domain.model.DayOfWeek
4-
import com.whyranoid.domain.model.Rule
53
import com.whyranoid.domain.model.User
64

75
data class UserResponse(
@@ -17,12 +15,3 @@ fun UserResponse.toUser() =
1715
name = this.name,
1816
profileUrl = this.profileUrl
1917
)
20-
21-
fun String.toRule(): Rule {
22-
val ruleString = this.split("-")
23-
return Rule(
24-
dayOfWeek = DayOfWeek.values().find { it.dayResId == ruleString[0] } ?: DayOfWeek.SUN,
25-
hour = ruleString[1].toInt(),
26-
minute = ruleString[2].toInt()
27-
)
28-
}

โ€Ždata/src/main/java/com/whyranoid/data/user/UserDataSource.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ import com.whyranoid.data.constant.Exceptions.NO_USER_EXCEPTION
99
import com.whyranoid.data.model.GroupInfoResponse
1010
import com.whyranoid.data.model.UserResponse
1111
import com.whyranoid.data.model.toGroupInfo
12-
import com.whyranoid.data.model.toRule
1312
import com.whyranoid.data.model.toUser
1413
import com.whyranoid.domain.model.GroupInfo
14+
import com.whyranoid.domain.model.toRule
1515
import kotlinx.coroutines.channels.awaitClose
1616
import kotlinx.coroutines.flow.Flow
1717
import kotlinx.coroutines.flow.callbackFlow

โ€Ždomain/src/main/java/com/whyranoid/domain/model/Rule.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@ data class Rule(
1010
}
1111
}
1212

13+
fun String.toRule(): Rule {
14+
val ruleString = this.split("-")
15+
return Rule(
16+
dayOfWeek = DayOfWeek.values().find { it.dayResId == ruleString[0] } ?: DayOfWeek.SUN,
17+
hour = ruleString[1].toInt(),
18+
minute = ruleString[2].toInt()
19+
)
20+
}
21+
1322
enum class DayOfWeek(val dayResId: String) {
1423
MON("์›”"),
1524
TUE("ํ™”"),

0 commit comments

Comments
ย (0)