点赞和评论

This commit is contained in:
2024-07-30 14:28:13 +08:00
parent 406caa3702
commit 0730fdea68
10 changed files with 357 additions and 93 deletions

View File

@@ -12,6 +12,7 @@ interface CommentService {
suspend fun getComments(pageNumber: Int, postId: Int? = null): ListContainer<Comment>
suspend fun createComment(postId: Int, content: String, authorId: Int): Comment
suspend fun likeComment(commentId: Int)
suspend fun dislikeComment(commentId: Int)
}
@@ -25,7 +26,7 @@ data class Comment(
val postId: Int = 0,
val avatar: String,
val author: Int,
val liked: Boolean,
var liked: Boolean,
)
class CommentPagingSource(
@@ -82,6 +83,12 @@ class TestCommentServiceImpl : CommentService {
)
}
rawList = rawList.sortedBy { -it.id }
rawList.forEach {
val myLikeIdList = TestDatabase.likeCommentList.filter { it.second == 1 }.map { it.first }
if (myLikeIdList.contains(it.id)) {
it.liked = true
}
}
val currentSublist = rawList.subList(from, min(to, rawList.size))
return ListContainer(
total = rawList.size,
@@ -121,7 +128,18 @@ class TestCommentServiceImpl : CommentService {
it
}
}
TestDatabase.likeCommentList += Pair(commentId, 1)
}
override suspend fun dislikeComment(commentId: Int) {
TestDatabase.comment = TestDatabase.comment.map {
if (it.id == commentId) {
it.copy(likes = it.likes - 1)
} else {
it
}
}
TestDatabase.likeCommentList = TestDatabase.likeCommentList.filter { it.first != commentId }
}
companion object {

View File

@@ -10,6 +10,7 @@ import kotlin.math.min
interface MomentService {
suspend fun getMomentById(id: Int): MomentItem
suspend fun likeMoment(id: Int)
suspend fun dislikeMoment(id: Int)
suspend fun getMoments(
pageNumber: Int,
author: Int? = null,
@@ -81,6 +82,10 @@ class TestMomentServiceImpl() : MomentService {
testMomentBackend.likeMoment(id)
}
override suspend fun dislikeMoment(id: Int) {
testMomentBackend.dislikeMoment(id)
}
}
class TestMomentBackend(
@@ -113,6 +118,12 @@ class TestMomentBackend(
)
}
val currentSublist = rawList.subList(from, min(to, rawList.size))
currentSublist.forEach {
val myLikeIdList = TestDatabase.likeMomentList.filter { it.second == 1 }.map { it.first }
if (myLikeIdList.contains(it.id)) {
it.liked = true
}
}
// delay
kotlinx.coroutines.delay(loadDelay)
return ListContainer(
@@ -124,7 +135,14 @@ class TestMomentBackend(
}
suspend fun getMomentById(id: Int): MomentItem {
return TestDatabase.momentData[id]
var moment = TestDatabase.momentData.first {
it.id == id
}
val isLike = TestDatabase.likeMomentList.any {
it.first == id && it.second == 1
}
moment = moment.copy(liked = isLike)
return moment
}
suspend fun likeMoment(id: Int) {
@@ -133,6 +151,17 @@ class TestMomentBackend(
}
val newMoment = oldMoment.copy(likeCount = oldMoment.likeCount + 1)
TestDatabase.updateMomentById(id, newMoment)
TestDatabase.likeMomentList += Pair(id, 1)
}
suspend fun dislikeMoment(id: Int) {
val oldMoment = TestDatabase.momentData.first {
it.id == id
}
val newMoment = oldMoment.copy(likeCount = oldMoment.likeCount - 1)
TestDatabase.updateMomentById(id, newMoment)
TestDatabase.likeMomentList = TestDatabase.likeMomentList.filter {
it.first != id
}
}
}