Skip to content

Commit

Permalink
#91 댓글 기능 구현 완료
Browse files Browse the repository at this point in the history
  • Loading branch information
pknujsp committed Jun 7, 2023
1 parent 0481551 commit 60a7aef
Show file tree
Hide file tree
Showing 24 changed files with 275 additions and 97 deletions.
3 changes: 2 additions & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.android.mediproject.core.data.remote.comments
import androidx.paging.PagingData
import com.android.mediproject.core.model.comments.CommentChangedResponse
import com.android.mediproject.core.model.comments.CommentListResponse
import com.android.mediproject.core.model.comments.LikeResponse
import com.android.mediproject.core.model.comments.MyCommentDto
import com.android.mediproject.core.model.requestparameters.DeleteCommentParameter
import com.android.mediproject.core.model.requestparameters.EditCommentParameter
Expand Down Expand Up @@ -39,5 +40,5 @@ interface CommentsRepository {
/**
* 댓글 좋아요 클릭
*/
fun likeComment(parameter: LikeCommentParameter): Flow<Result<CommentChangedResponse>>
fun likeComment(parameter: LikeCommentParameter): Flow<Result<LikeResponse>>
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.android.mediproject.core.common.AWS_LOAD_PAGE_SIZE
import com.android.mediproject.core.data.remote.sign.SignRepository
import com.android.mediproject.core.model.comments.CommentChangedResponse
import com.android.mediproject.core.model.comments.CommentListResponse
import com.android.mediproject.core.model.comments.LikeResponse
import com.android.mediproject.core.model.comments.MyCommentDto
import com.android.mediproject.core.model.remote.token.TokenState
import com.android.mediproject.core.model.requestparameters.DeleteCommentParameter
Expand All @@ -23,7 +24,8 @@ import kotlinx.coroutines.flow.lastOrNull
import javax.inject.Inject

class CommentsRepositoryImpl @Inject constructor(
private val commentsDataSource: CommentsDataSource, private val signRepository: SignRepository) : CommentsRepository {
private val commentsDataSource: CommentsDataSource, private val signRepository: SignRepository
) : CommentsRepository {
override fun getCommentsForAMedicine(medicineId: Long): Flow<PagingData<CommentListResponse.Comment>> =
Pager(config = PagingConfig(pageSize = AWS_LOAD_PAGE_SIZE, prefetchDistance = 5), pagingSourceFactory = {
CommentsListDataSourceImpl(commentsDataSource, medicineId)
Expand Down Expand Up @@ -70,7 +72,7 @@ class CommentsRepositoryImpl @Inject constructor(
}
}

override fun likeComment(parameter: LikeCommentParameter): Flow<Result<CommentChangedResponse>> = channelFlow {
override fun likeComment(parameter: LikeCommentParameter): Flow<Result<LikeResponse>> = channelFlow {
checkToken().collectLatest { tokenState ->
tokenState.onSuccess {
commentsDataSource.likeComment(parameter).collectLatest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class SignRepositoryImpl @Inject constructor(
signInResult.onSuccess {
// 내 계정 정보 메모리에 저장
userInfoRepository.updateMyAccountInfo(AccountState.SignedIn(it._userId!!.toLong(), it._nickName!!, it._email!!))
saveMyAccountInfo(if (signInParameter.isSavedEmail) it._email!! else "", it._nickName!!, it._userId!!.toLong())
saveMyAccountInfo(it._email!!, it._nickName!!, it._userId!!.toLong())
}
}

Expand Down Expand Up @@ -78,7 +78,7 @@ class SignRepositoryImpl @Inject constructor(
signUpResult.onSuccess {
// 내 계정 정보 메모리에 저장
userInfoRepository.updateMyAccountInfo(AccountState.SignedIn(it._userId!!.toLong(), it._nickName!!, it._email!!))
saveMyAccountInfo("", it._nickName!!, it._userId!!.toLong())
saveMyAccountInfo(it._email!!, it._nickName!!, it._userId!!.toLong())
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ class UserInfoRepositoryImpl @Inject constructor(
private val userInfoDataSource: UserInfoDataSource, private val appDataStore: AppDataStore
) : UserInfoRepository {


private val _myAccountInfo = MutableStateFlow<AccountState>(AccountState.Unknown)

override val myAccountInfo get() = _myAccountInfo as StateFlow<AccountState>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class TokenServerImpl @Inject constructor() : TokenServer {
*/
override val currentTokens: EndpointTokenState
get() {
val token = tokens.replayCache.firstOrNull()
val token = tokens.replayCache.lastOrNull()
return if (token == null) {
EndpointTokenState.NoToken
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import androidx.paging.flatMap
import com.android.mediproject.core.common.network.Dispatcher
import com.android.mediproject.core.common.network.MediDispatchers
import com.android.mediproject.core.data.remote.comments.CommentsRepository
import com.android.mediproject.core.data.remote.user.UserInfoRepository
import com.android.mediproject.core.model.comments.CommentDto
import com.android.mediproject.core.model.comments.MyCommentDto
import com.android.mediproject.core.model.comments.toDto
Expand All @@ -13,6 +14,8 @@ import com.android.mediproject.core.model.requestparameters.EditCommentParameter
import com.android.mediproject.core.model.requestparameters.LikeCommentParameter
import com.android.mediproject.core.model.requestparameters.NewCommentParameter
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.channels.BufferOverflow
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.channelFlow
import kotlinx.coroutines.flow.collectLatest
Expand All @@ -21,19 +24,31 @@ import javax.inject.Inject

class CommentsUseCase @Inject constructor(
private val commentsRepository: CommentsRepository,
@Dispatcher(MediDispatchers.Default) private val defaultDispatcher: CoroutineDispatcher) {
private val userInfoRepository: UserInfoRepository,
@Dispatcher(MediDispatchers.Default) private val defaultDispatcher: CoroutineDispatcher
) {

val scrollChannel = Channel<Unit>(capacity = 3, onBufferOverflow = BufferOverflow.DROP_OLDEST)

/**
* 약에 대한 댓글을 가져오는 메서드입니다.
*
* @param medicineId 약의 고유 번호
*/
fun getCommentsForAMedicine(medicineId: Long): Flow<PagingData<CommentDto>> = channelFlow {
fun getCommentsForAMedicine(medicineId: Long, myUserId: Long): Flow<PagingData<CommentDto>> = channelFlow {
commentsRepository.getCommentsForAMedicine(medicineId).collectLatest { pagingData ->
val result = pagingData.flatMap {
(it.replies.map { reply ->
reply.toDto()
}.reversed()) + listOf(it.toDto())
reply.toDto().apply {
reply.likeList.forEach { like ->
if (like.userId == myUserId) this.isLiked = true
}
}
}.reversed()) + listOf(it.toDto().apply {
it.likeList.forEach { like ->
if (like.userId == myUserId) this.isLiked = true
}
})
}
send(result)
}
Expand Down Expand Up @@ -61,7 +76,9 @@ class CommentsUseCase @Inject constructor(
*/
fun applyNewComment(parameter: NewCommentParameter): Flow<Result<Unit>> =
commentsRepository.applyNewComment(parameter).mapLatest { result ->
result.fold(onSuccess = { Result.success(Unit) }, onFailure = { Result.failure(it) })
result.fold(onSuccess = {
Result.success(Unit)
}, onFailure = { Result.failure(it) })
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ import kotlinx.serialization.Serializable
*/
@Serializable
data class CommentChangedResponse(
val commentId: Long
val commentId: Long = 0L,
) : BaseAwsQueryResponse()
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,16 @@ data class CommentDto(
val content: String,
val createdAt: String,
val updatedAt: String,
var onClickReply: ((Int) -> Unit)?,
var onClickLike: ((Long) -> Unit)?,
var onClickReply: ((String, Long) -> Unit)?,
var onClickLike: ((Long, Boolean) -> Unit)?,
var onClickDelete: ((Long) -> Unit)?,
var onClickEdit: ((CommentDto, Int) -> Unit)?,
var onClickApplyEdited: ((CommentDto) -> Unit)?,
var isMine: Boolean = false) {
var isMine: Boolean = false
) {

var isEditing: Boolean = false
var isLiked: Boolean = false
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,38 @@ import kotlinx.serialization.Serializable

@Serializable
data class CommentListResponse(
@SerialName("commentList") val commentList: List<Comment>) : BaseAwsQueryResponse() {
@SerialName("commentList") val commentList: List<Comment>
) : BaseAwsQueryResponse() {

@Serializable
data class Comment(
@SerialName("CONTENT") val content: String, // 테스트 - 메인 댓글 1
@SerialName("createdAt") val createdAt: String, // 2023-06-02T10:43:12.435Z
@SerialName("ID") val id: Long, // 2
@SerialName("likeList") val likeList: List<Int>,
@SerialName("MEDICINEID") val medicineId: Long, // 41
@SerialName("replies") val replies: List<Reply>,
@SerialName("SUBORDINATION") val subordination: Long, // 0
@SerialName("USERID") val userId: Long, // 2
@SerialName("likeList") val likeList: List<Like>,
@SerialName("nickname") val nickName: String,
@SerialName("updatedAt") val updatedAt: String) {

@SerialName("updatedAt") val updatedAt: String
) {
@Serializable
data class Reply(
@SerialName("CONTENT") val content: String, // 테스트 - 서브 댓글 1 - 1
@SerialName("createdAt") val createdAt: String, // 2023-06-02T10:43:54.211Z
@SerialName("ID") val id: Long, // 5
@SerialName("likeList") val likeList: List<Int>,
@SerialName("MEDICINEID") val medicineId: Long, // 41
@SerialName("likeList") val likeList: List<Like>, @SerialName("MEDICINEID") val medicineId: Long, // 41
@SerialName("SUBORDINATION") val subordination: Long, // 2
@SerialName("USERID") val userId: Long, // 3
@SerialName("nickname") val nickName: String,
@SerialName("updatedAt") val updatedAt: String)
@SerialName("nickname") val nickName: String, @SerialName("updatedAt") val updatedAt: String
)
}
}
}

@Serializable
data class Like(
@SerialName("COMMENTID") val commentId: Long, // 2
@SerialName("ID") val id: Long, // 1
@SerialName("USERID") val userId: Long
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.android.mediproject.core.model.comments

import com.android.mediproject.core.model.awscommon.BaseAwsQueryResponse
import kotlinx.serialization.Serializable

/**
* 댓글 등록,수정,삭제,좋아요 처리 후 서버로 부터 받는 응답
*/
@Serializable
data class LikeResponse(
val likeId: Long = 0L,
) : BaseAwsQueryResponse()
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package com.android.mediproject.core.model.requestparameters

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

/**
* 댓글 좋아요를 위한 파라미터 클래스입니다.
*
* @property commentId 댓글 id
* @property userId 사용자 id
* @property medicineId 약 id
* @property toLike 좋아요를 누를지 취소할지 여부
*/
@Serializable
data class LikeCommentParameter(
@SerialName("commentId") val commentId: Long, // 5
@SerialName("userId") val userId: Long)
val commentId: Long, // 5
val medicineId: Long, val toLike: Boolean
)
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.android.mediproject.core.network.datasource.comments
import androidx.paging.PagingData
import com.android.mediproject.core.model.comments.CommentChangedResponse
import com.android.mediproject.core.model.comments.CommentListResponse
import com.android.mediproject.core.model.comments.LikeResponse
import com.android.mediproject.core.model.requestparameters.DeleteCommentParameter
import com.android.mediproject.core.model.requestparameters.EditCommentParameter
import com.android.mediproject.core.model.requestparameters.LikeCommentParameter
Expand All @@ -20,5 +21,5 @@ interface CommentsDataSource {
fun applyNewComment(parameter: NewCommentParameter): Flow<Result<CommentChangedResponse>>
fun deleteComment(parameter: DeleteCommentParameter): Flow<Result<CommentChangedResponse>>

fun likeComment(likeCommentParameter: LikeCommentParameter): Flow<Result<CommentChangedResponse>>
fun likeComment(likeCommentParameter: LikeCommentParameter): Flow<Result<LikeResponse>>
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.android.mediproject.core.network.datasource.comments
import androidx.paging.PagingData
import com.android.mediproject.core.model.comments.CommentChangedResponse
import com.android.mediproject.core.model.comments.CommentListResponse
import com.android.mediproject.core.model.comments.LikeResponse
import com.android.mediproject.core.model.requestparameters.DeleteCommentParameter
import com.android.mediproject.core.model.requestparameters.EditCommentParameter
import com.android.mediproject.core.model.requestparameters.LikeCommentParameter
Expand All @@ -14,7 +15,8 @@ import kotlinx.coroutines.flow.flow
import javax.inject.Inject

class CommentsDataSourceImpl @Inject constructor(
private val awsNetworkApi: AwsNetworkApi) : CommentsDataSource {
private val awsNetworkApi: AwsNetworkApi
) : CommentsDataSource {

/**
* 약품에 대한 댓글 리스트를 가져온다.
Expand Down Expand Up @@ -57,8 +59,11 @@ class CommentsDataSourceImpl @Inject constructor(
}).also { emit(it) }
}

override fun likeComment(parameter: LikeCommentParameter): Flow<Result<CommentChangedResponse>> = flow {
awsNetworkApi.likeComment(parameter).onResponse().fold(onSuccess = { response ->
override fun likeComment(parameter: LikeCommentParameter): Flow<Result<LikeResponse>> = flow {
parameter.let {
if (it.toLike) awsNetworkApi.likeComment(parameter.medicineId, parameter.commentId)
else awsNetworkApi.unlikeComment(parameter.medicineId, parameter.commentId)
}.onResponse().fold(onSuccess = { response ->
Result.success(response)
}, onFailure = {
Result.failure(it)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.android.mediproject.core.common.util.AesCoder
import com.android.mediproject.core.datastore.TokenDataSourceImpl
import com.android.mediproject.core.model.comments.CommentChangedResponse
import com.android.mediproject.core.model.comments.CommentListResponse
import com.android.mediproject.core.model.comments.LikeResponse
import com.android.mediproject.core.model.medicine.InterestedMedicine.InterestedMedicineListResponse
import com.android.mediproject.core.model.medicine.MedicineIdResponse
import com.android.mediproject.core.model.remote.sign.SignInResponse
Expand All @@ -15,7 +16,6 @@ import com.android.mediproject.core.model.requestparameters.ChangePasswordReques
import com.android.mediproject.core.model.requestparameters.DeleteCommentParameter
import com.android.mediproject.core.model.requestparameters.EditCommentParameter
import com.android.mediproject.core.model.requestparameters.GetMedicineIdParameter
import com.android.mediproject.core.model.requestparameters.LikeCommentParameter
import com.android.mediproject.core.model.requestparameters.NewCommentParameter
import com.android.mediproject.core.model.user.remote.ChangeNicknameResponse
import com.android.mediproject.core.model.user.remote.ChangePasswordResponse
Expand Down Expand Up @@ -164,12 +164,22 @@ interface AwsNetworkApi {
): Response<CommentChangedResponse>

/**
* 댓글 좋아요
* 댓글 좋아요 추가
*/
@POST(value = "medicine/comment")
@POST(value = "medicine/comment/{medicineId}/like/{commentId}")
suspend fun likeComment(
@Body likeCommentParameter: LikeCommentParameter
): Response<CommentChangedResponse>
@Path("medicineId", encoded = true) medicineId: Long,
@Path("commentId", encoded = true) commentId: Long,
): Response<LikeResponse>

/**
* 댓글 좋아요 해제
*/
@DELETE(value = "medicine/comment/{medicineId}/like/{commentId}")
suspend fun unlikeComment(
@Path("medicineId", encoded = true) medicineId: Long,
@Path("commentId", encoded = true) commentId: Long,
): Response<LikeResponse>

/**
* 댓글 등록
Expand Down
Loading

0 comments on commit 60a7aef

Please sign in to comment.