From d1d9ef5d24c03270ce9a698ef71d902985676ce6 Mon Sep 17 00:00:00 2001 From: Klaus Date: Thu, 21 Dec 2023 01:24:27 +0900 Subject: [PATCH] =?UTF-8?q?=EC=BB=A4=EB=AE=A4=EB=8B=88=ED=8B=B0=20?= =?UTF-8?q?=EA=B2=8C=EC=8B=9C=EB=AC=BC=20=EC=A1=B0=ED=9A=8C=20=EC=83=81?= =?UTF-8?q?=EC=84=B8=20API=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CreatorCommunityController.kt | 18 ++++++ .../CreatorCommunityRepository.kt | 16 +++-- .../CreatorCommunityService.kt | 58 ++++++++++++++++++- .../GetCommunityPostListResponse.kt | 2 + 4 files changed, 87 insertions(+), 7 deletions(-) diff --git a/src/main/kotlin/kr/co/vividnext/sodalive/explorer/profile/creatorCommunity/CreatorCommunityController.kt b/src/main/kotlin/kr/co/vividnext/sodalive/explorer/profile/creatorCommunity/CreatorCommunityController.kt index 3d42e9d..7aa36dc 100644 --- a/src/main/kotlin/kr/co/vividnext/sodalive/explorer/profile/creatorCommunity/CreatorCommunityController.kt +++ b/src/main/kotlin/kr/co/vividnext/sodalive/explorer/profile/creatorCommunity/CreatorCommunityController.kt @@ -85,6 +85,24 @@ class CreatorCommunityController(private val service: CreatorCommunityService) { ) } + @GetMapping("/{id}") + fun getCommunityPostDetail( + @PathVariable("id") postId: Long, + @RequestParam timezone: String, + @AuthenticationPrincipal(expression = "#this == 'anonymousUser' ? null : member") member: Member? + ) = run { + if (member == null) throw SodaException("로그인 정보를 확인해주세요.") + + ApiResponse.ok( + service.getCommunityPostDetail( + postId = postId, + memberId = member.id!!, + timezone = timezone, + isAdult = member.auth != null + ) + ) + } + @PostMapping("/like") fun communityPostLike( @RequestBody request: PostCommunityPostLikeRequest, diff --git a/src/main/kotlin/kr/co/vividnext/sodalive/explorer/profile/creatorCommunity/CreatorCommunityRepository.kt b/src/main/kotlin/kr/co/vividnext/sodalive/explorer/profile/creatorCommunity/CreatorCommunityRepository.kt index 10f99df..c0d3d35 100644 --- a/src/main/kotlin/kr/co/vividnext/sodalive/explorer/profile/creatorCommunity/CreatorCommunityRepository.kt +++ b/src/main/kotlin/kr/co/vividnext/sodalive/explorer/profile/creatorCommunity/CreatorCommunityRepository.kt @@ -17,7 +17,7 @@ interface CreatorCommunityQueryRepository { isAdult: Boolean ): List - fun findByIdAndActive(postId: Long): CreatorCommunity? + fun findByIdAndActive(postId: Long, isAdult: Boolean): CreatorCommunity? } class CreatorCommunityQueryRepositoryImpl(private val queryFactory: JPAQueryFactory) : CreatorCommunityQueryRepository { @@ -54,13 +54,17 @@ class CreatorCommunityQueryRepositoryImpl(private val queryFactory: JPAQueryFact .fetch() } - override fun findByIdAndActive(postId: Long): CreatorCommunity? { + override fun findByIdAndActive(postId: Long, isAdult: Boolean): CreatorCommunity? { + var where = creatorCommunity.id.eq(postId) + .and(creatorCommunity.isActive.isTrue) + + if (!isAdult) { + where = where.and(creatorCommunity.isAdult.isFalse) + } + return queryFactory .selectFrom(creatorCommunity) - .where( - creatorCommunity.id.eq(postId) - .and(creatorCommunity.isActive.isTrue) - ) + .where(where) .fetchFirst() } } diff --git a/src/main/kotlin/kr/co/vividnext/sodalive/explorer/profile/creatorCommunity/CreatorCommunityService.kt b/src/main/kotlin/kr/co/vividnext/sodalive/explorer/profile/creatorCommunity/CreatorCommunityService.kt index 9c71bab..5c7bf25 100644 --- a/src/main/kotlin/kr/co/vividnext/sodalive/explorer/profile/creatorCommunity/CreatorCommunityService.kt +++ b/src/main/kotlin/kr/co/vividnext/sodalive/explorer/profile/creatorCommunity/CreatorCommunityService.kt @@ -178,6 +178,8 @@ class CreatorCommunityService( }, content = it.content, date = getTimeAgoString(it.createdAt!!), + isCommentAvailable = it.isCommentAvailable, + isAdult = it.isAdult, isLike = isLike, likeCount = likeCount, commentCount = commentCount, @@ -187,6 +189,60 @@ class CreatorCommunityService( .toList() } + fun getCommunityPostDetail( + postId: Long, + memberId: Long, + timezone: String, + isAdult: Boolean + ): GetCommunityPostListResponse { + val post = repository.findByIdAndActive(postId, isAdult = isAdult) + ?: throw SodaException("잘못된 요청입니다.\n다시 시도해 주세요.") + + val isLike = likeRepository.findByPostIdAndMemberId(postId = post.id!!, memberId = memberId)?.isActive ?: false + val likeCount = likeRepository.totalCountCommunityPostLikeByPostId(post.id!!) + val commentCount = if (post.isCommentAvailable) { + commentRepository.totalCountCommentByPostId(postId = post.id!!) + } else { + 0 + } + val commentList = commentRepository.findByPostId( + cloudFrontHost = imageHost, + timezone = timezone, + id = post.id!!, + offset = 0, + limit = 1 + ) + + val firstComment = if (commentList.isNotEmpty()) { + commentList[0] + } else { + null + } + + return GetCommunityPostListResponse( + postId = post.id!!, + creatorNickname = post.member!!.nickname, + creatorProfileUrl = if (post.member!!.profileImage != null) { + "$imageHost/${post.member!!.profileImage}" + } else { + "$imageHost/profile/default-profile.png" + }, + imageUrl = if (post.imagePath != null) { + "$imageHost/${post.imagePath!!}" + } else { + null + }, + content = post.content, + date = getTimeAgoString(post.createdAt!!), + isCommentAvailable = post.isCommentAvailable, + isAdult = post.isAdult, + isLike = isLike, + likeCount = likeCount, + commentCount = commentCount, + firstComment = firstComment + ) + } + private fun getTimeAgoString(dateTime: LocalDateTime): String { val now = LocalDateTime.now() val duration = Duration.between(dateTime, now) @@ -208,7 +264,7 @@ class CreatorCommunityService( postLike = CreatorCommunityLike() postLike.member = member - val post = repository.findByIdAndActive(request.postId) + val post = repository.findByIdAndActive(request.postId, isAdult = member.auth != null) ?: throw SodaException("잘못된 요청입니다.\n다시 시도해 주세요.") postLike.creatorCommunity = post diff --git a/src/main/kotlin/kr/co/vividnext/sodalive/explorer/profile/creatorCommunity/GetCommunityPostListResponse.kt b/src/main/kotlin/kr/co/vividnext/sodalive/explorer/profile/creatorCommunity/GetCommunityPostListResponse.kt index 8693eb1..b147761 100644 --- a/src/main/kotlin/kr/co/vividnext/sodalive/explorer/profile/creatorCommunity/GetCommunityPostListResponse.kt +++ b/src/main/kotlin/kr/co/vividnext/sodalive/explorer/profile/creatorCommunity/GetCommunityPostListResponse.kt @@ -10,6 +10,8 @@ data class GetCommunityPostListResponse @QueryProjection constructor( val imageUrl: String?, val content: String, val date: String, + val isCommentAvailable: Boolean, + val isAdult: Boolean, val isLike: Boolean, val likeCount: Int, val commentCount: Int,