Skip to content

Commit 20643bb

Browse files
authored
Merge pull request #132 from NMNB-Team/v1/refactor/#131-video-like-response
[refactor] 영상 랜덤 조회 응답에 좋아요 값 추가
2 parents 2d9a73d + 520b0ef commit 20643bb

4 files changed

Lines changed: 17 additions & 6 deletions

File tree

nmnb-application/src/main/kotlin/nmnb/application/domain/like/service/LikeService.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ import nmnb.domain.user.User
66
interface LikeService {
77
fun likeOrUnlike(user: User, request: PostLikeServiceRequest)
88
fun deleteByPostId(postId: Long)
9+
fun isLikedByUser(userId: String, postId: Long): Boolean
910
}

nmnb-application/src/main/kotlin/nmnb/application/domain/like/service/LikeServiceImpl.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ class LikeServiceImpl(
3333
likeRepository.deleteAllByPostId(postId)
3434
}
3535

36+
override fun isLikedByUser(userId: String, postId: Long): Boolean {
37+
val likeCacheKey = LikeCacheKey(userId, postId)
38+
return redisTemplate.hasKey(likeCacheKey.key)
39+
}
40+
3641
private fun setLikeCache(likeCacheKey: LikeCacheKey) {
3742
redisTemplate.opsForValue().set(likeCacheKey.key, "1")
3843
}

nmnb-application/src/main/kotlin/nmnb/application/domain/post/service/PostServiceImpl.kt

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class PostServiceImpl(
3434
val startIndex = request.cursor + 1
3535
val extractedIds = RandomSelector.extractPageIds(shuffledPostIds, startIndex, request.size)
3636

37-
return toPostPageResponse(extractedIds, shuffledPostIds, startIndex)
37+
return toPostPageResponse(extractedIds, shuffledPostIds, startIndex, userId)
3838
}
3939

4040
@Transactional
@@ -85,7 +85,7 @@ class PostServiceImpl(
8585
val content = if (hasNext) posts.dropLast(1) else posts
8686
val nextCursorId = if (hasNext) content.lastOrNull()?.id else -1
8787

88-
val postsResponse = content.map { post -> PostInfoResponse.of(post) }
88+
val postsResponse = content.map { post -> PostInfoResponse.of(post, false) }
8989
return MyPostPageResponse.of(postsResponse, hasNext, nextCursorId)
9090
}
9191

@@ -126,18 +126,21 @@ class PostServiceImpl(
126126
pageIds: List<Long>,
127127
shuffledPostIds: List<Long>,
128128
startIndex: Int,
129+
userId: String?,
129130
): PostPageResponse {
130-
val postsInfoResponse = mapPostsToResponse(pageIds)
131+
val postsInfoResponse = mapPostsToResponse(pageIds, userId)
131132
val hasNext = shuffledPostIds.size > (startIndex + pageIds.size)
132133
val nextCursor = if (!hasNext) INITIAL_CURSOR else startIndex + pageIds.size - 1
133134

134135
return PostPageResponse.of(postsInfoResponse, hasNext, nextCursor)
135136
}
136137

137-
private fun mapPostsToResponse(pageIds: List<Long>): List<PostInfoResponse> {
138+
private fun mapPostsToResponse(pageIds: List<Long>, userId: String?): List<PostInfoResponse> {
138139
val postsMap = fetchPostsByIds(pageIds)
139140
return pageIds.map { id ->
140-
PostInfoResponse.of(postsMap[id]!!)
141+
val post = postsMap[id]!!
142+
val isLiked = userId?.let { likeService.isLikedByUser(it, id) } ?: false
143+
PostInfoResponse.of(post, isLiked)
141144
}
142145
}
143146

nmnb-application/src/main/kotlin/nmnb/application/domain/post/service/dto/response/PostPageResponse.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,18 @@ data class PostInfoResponse(
4646
val thumbnailUrl: String,
4747
val description: String?,
4848
val userInfo: UserInfoResponse,
49+
val isLiked: Boolean,
4950
) {
5051
companion object {
51-
fun of(post: Post): PostInfoResponse {
52+
fun of(post: Post, isLiked: Boolean = false): PostInfoResponse {
5253
return PostInfoResponse(
5354
userId = post.user.id,
5455
postId = post.id,
5556
url = post.url,
5657
thumbnailUrl = post.thumbnailUrl,
5758
description = post.description,
5859
userInfo = UserInfoResponse.of(post.user),
60+
isLiked = isLiked,
5961
)
6062
}
6163
}

0 commit comments

Comments
 (0)