팔로우 한 크리에이터의 7일 이내의 커뮤니티 게시물 10개 가져오기 API 추가

This commit is contained in:
Klaus 2023-12-21 22:35:52 +09:00
parent d1d9ef5d24
commit e4ca84252d
3 changed files with 117 additions and 0 deletions

View File

@ -179,4 +179,20 @@ class CreatorCommunityController(private val service: CreatorCommunityService) {
)
)
}
@GetMapping("/latest")
fun getLatestPostListFromCreatorsYouFollow(
@RequestParam timezone: String,
@AuthenticationPrincipal(expression = "#this == 'anonymousUser' ? null : member") member: Member?
) = run {
if (member == null) throw SodaException("로그인 정보를 확인해주세요.")
ApiResponse.ok(
service.getLatestPostListFromCreatorsYouFollow(
timezone = timezone,
memberId = member.id!!,
isAdult = member.auth != null
)
)
}
}

View File

@ -1,9 +1,12 @@
package kr.co.vividnext.sodalive.explorer.profile.creatorCommunity
import com.querydsl.core.types.dsl.Expressions
import com.querydsl.jpa.impl.JPAQueryFactory
import kr.co.vividnext.sodalive.explorer.profile.creatorCommunity.QCreatorCommunity.creatorCommunity
import kr.co.vividnext.sodalive.member.QMember.member
import kr.co.vividnext.sodalive.member.following.QCreatorFollowing.creatorFollowing
import org.springframework.data.jpa.repository.JpaRepository
import java.time.LocalDateTime
interface CreatorCommunityRepository : JpaRepository<CreatorCommunity, Long>, CreatorCommunityQueryRepository
@ -18,6 +21,7 @@ interface CreatorCommunityQueryRepository {
): List<CreatorCommunity>
fun findByIdAndActive(postId: Long, isAdult: Boolean): CreatorCommunity?
fun getLatestPostListFromCreatorsYouFollow(memberId: Long, isAdult: Boolean): List<CreatorCommunity>
}
class CreatorCommunityQueryRepositoryImpl(private val queryFactory: JPAQueryFactory) : CreatorCommunityQueryRepository {
@ -67,4 +71,43 @@ class CreatorCommunityQueryRepositoryImpl(private val queryFactory: JPAQueryFact
.where(where)
.fetchFirst()
}
override fun getLatestPostListFromCreatorsYouFollow(memberId: Long, isAdult: Boolean): List<CreatorCommunity> {
val creatorCommunity = QCreatorCommunity.creatorCommunity
val latest = QCreatorCommunity.creatorCommunity
var where = creatorCommunity.isActive.isTrue
.and(creatorCommunity.createdAt.after(LocalDateTime.now().minusDays(7)))
if (!isAdult) {
where = where.and(creatorCommunity.isAdult.isFalse)
}
val subQuery = queryFactory
.select(latest.member.id, latest.createdAt.max().`as`("maxCreatedAt"))
.from(latest)
.groupBy(latest.member.id)
where = where.and(
Expressions.list(creatorCommunity.member.id, creatorCommunity.createdAt).`in`(subQuery)
)
val memberSubQuery = queryFactory
.select(creatorFollowing.creator.id)
.from(creatorFollowing)
.where(creatorFollowing.member.id.eq(memberId))
where = where.and(
creatorCommunity.member.id.`in`(memberSubQuery)
)
return queryFactory
.selectFrom(creatorCommunity)
.innerJoin(creatorCommunity.member, member)
.innerJoin(member)
.where(where)
.orderBy(creatorCommunity.createdAt.desc())
.limit(10)
.fetch()
}
}

View File

@ -353,4 +353,62 @@ class CreatorCommunityService(
val totalCount = commentRepository.commentReplyCountByCommentId(commentId)
return GetCommunityPostCommentListResponse(totalCount = totalCount, items = commentList)
}
fun getLatestPostListFromCreatorsYouFollow(
timezone: String,
memberId: Long,
isAdult: Boolean
): List<GetCommunityPostListResponse> {
val postList = repository.getLatestPostListFromCreatorsYouFollow(memberId = memberId, isAdult = isAdult)
return postList
.asSequence()
.map {
val isLike =
likeRepository.findByPostIdAndMemberId(postId = it.id!!, memberId = memberId)?.isActive ?: false
val likeCount = likeRepository.totalCountCommunityPostLikeByPostId(it.id!!)
val commentCount = if (it.isCommentAvailable) {
commentRepository.totalCountCommentByPostId(postId = it.id!!)
} else {
0
}
val commentList = commentRepository.findByPostId(
cloudFrontHost = imageHost,
timezone = timezone,
id = it.id!!,
offset = 0,
limit = 1
)
val firstComment = if (commentList.isNotEmpty()) {
commentList[0]
} else {
null
}
GetCommunityPostListResponse(
postId = it.id!!,
creatorNickname = it.member!!.nickname,
creatorProfileUrl = if (it.member!!.profileImage != null) {
"$imageHost/${it.member!!.profileImage}"
} else {
"$imageHost/profile/default-profile.png"
},
imageUrl = if (it.imagePath != null) {
"$imageHost/${it.imagePath!!}"
} else {
null
},
content = it.content,
date = getTimeAgoString(it.createdAt!!),
isCommentAvailable = it.isCommentAvailable,
isAdult = it.isAdult,
isLike = isLike,
likeCount = likeCount,
commentCount = commentCount,
firstComment = firstComment
)
}
.toList()
}
}