Skip to content

Commit

Permalink
Merge pull request #67 from kotlin-project-team/feature/comment
Browse files Browse the repository at this point in the history
🐛  fix: 무한스크롤 구현 문제 수정 (#51)
  • Loading branch information
yu-heejin authored Aug 7, 2024
2 parents d729c8f + 9990102 commit 4762e58
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.kotlin.study.dongambackend.domain.comment.dto.request.CommentCreateRe
import com.kotlin.study.dongambackend.domain.comment.dto.request.CommentReportRequest
import com.kotlin.study.dongambackend.domain.comment.dto.request.CommentUpdateRequest
import com.kotlin.study.dongambackend.domain.comment.dto.response.CommentResponse
import com.kotlin.study.dongambackend.domain.comment.dto.response.FindAllCommentResponse
import com.kotlin.study.dongambackend.domain.comment.service.CommentService
import com.kotlin.study.dongambackend.domain.user.entity.User
import org.slf4j.LoggerFactory
Expand Down Expand Up @@ -67,9 +68,9 @@ class CommentController(private val commentService: CommentService) {
@PathVariable postId: Long,
@RequestParam lastCommentId: Long,
pageable: Pageable
): ResponseEntity<List<CommentResponse>> {
val commentsSlice = commentService.getAllComment(lastCommentId, pageable)
val comments = commentsSlice.content // Slice에서 content만 추출
return ResponseEntity.ok().body(comments)
): ResponseEntity<BaseResponse<List<FindAllCommentResponse>>> {
val commentsSlice = commentService.getAllComment(postId, lastCommentId, pageable)
val comments = commentsSlice.content
return BaseResponse(ResponseStatusType.SUCCESS, comments).convert()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.kotlin.study.dongambackend.domain.comment.dto.response

import com.kotlin.study.dongambackend.domain.post.dto.response.UserInformation
import com.querydsl.core.annotations.QueryProjection

data class FindAllCommentResponse @QueryProjection constructor(
val id: Long,
val user: UserInformation,
val postId: Long,
val content: String?
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.kotlin.study.dongambackend.domain.comment.dto.response

data class GetAllCommentResponse (
val comments: List<FindAllCommentResponse>,
val commentCount: Int
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package com.kotlin.study.dongambackend.domain.comment.mapper

import com.kotlin.study.dongambackend.domain.comment.dto.request.CommentCreateRequest
import com.kotlin.study.dongambackend.domain.comment.dto.response.CommentResponse
import com.kotlin.study.dongambackend.domain.comment.dto.response.FindAllCommentResponse
import com.kotlin.study.dongambackend.domain.comment.dto.response.GetAllCommentResponse
import com.kotlin.study.dongambackend.domain.comment.entity.Comment
import com.kotlin.study.dongambackend.domain.post.entity.Post
import com.kotlin.study.dongambackend.domain.user.entity.User
Expand All @@ -10,18 +12,11 @@ import org.springframework.stereotype.Component
@Component
class CommentMapper {

fun convertCommentsToResponses(results: List<Comment>): List<CommentResponse> {
return results.map { comment ->
CommentResponse(
id = comment.id,
userId = comment.userId,
postId = comment.postId,
content = comment.content
)
}
}

fun convertCreateCommentReqDtoToEntity(user: User, post: Post, createRequest: CommentCreateRequest): Comment {
return Comment(user, post, createRequest.content)
}

fun toGetAllCommentResponse(comments: List<FindAllCommentResponse>, postCount: Int): GetAllCommentResponse {
return GetAllCommentResponse(comments, postCount)
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package com.kotlin.study.dongambackend.domain.comment.repository

import com.kotlin.study.dongambackend.domain.comment.dto.response.CommentResponse
import com.kotlin.study.dongambackend.domain.comment.entity.Comment
import com.kotlin.study.dongambackend.domain.comment.dto.response.FindAllCommentResponse
import com.kotlin.study.dongambackend.domain.comment.dto.response.QFindAllCommentResponse
import com.kotlin.study.dongambackend.domain.comment.entity.QComment
import com.kotlin.study.dongambackend.domain.comment.mapper.CommentMapper
import com.kotlin.study.dongambackend.domain.comment.service.CommentService
import com.kotlin.study.dongambackend.domain.post.dto.response.QUserInformation
import com.querydsl.jpa.impl.JPAQueryFactory
import org.springframework.data.domain.*
import org.springframework.stereotype.Repository
Expand All @@ -13,14 +12,35 @@ import org.springframework.stereotype.Repository
class CommentQueryDslRepository(val queryDslFactory: JPAQueryFactory) {
val qComment = QComment.comment;

fun searchCommentsBySlice(lastCommentId: Long, pageable: Pageable): List<Comment> {
return queryDslFactory.selectFrom(qComment)
fun searchCommentsByPostId(postId: Long, lastCommentId: Long, pageable: Pageable): List<FindAllCommentResponse> {
// 기본 쿼리 생성
val query = queryDslFactory
.select(
QFindAllCommentResponse(
qComment.id,
QUserInformation(
qComment.userId.id,
qComment.userId.studentId,
qComment.userId.nickname
),
qComment.postId.id,
qComment.content
)
)
.from(qComment)
.where(
qComment.isDeleted.eq(false),
qComment.id.gt(lastCommentId)
qComment.postId.id.eq(postId)
)
.orderBy(qComment.id.asc())
.limit((pageable.pageSize + 1).toLong())

// lastCommentId가 0보다 클 경우, 해당 조건을 추가
if (lastCommentId > 0) {
query.where(qComment.id.lt(lastCommentId))
}

// 최신 댓글부터 조회하고, 페이지 사이즈에 맞게 제한
return query.orderBy(qComment.id.desc())
.limit(pageable.pageSize.toLong())
.fetch()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.kotlin.study.dongambackend.domain.comment.dto.request.CommentCreateRe
import com.kotlin.study.dongambackend.domain.comment.dto.request.CommentReportRequest
import com.kotlin.study.dongambackend.domain.comment.dto.request.CommentUpdateRequest
import com.kotlin.study.dongambackend.domain.comment.dto.response.CommentResponse
import com.kotlin.study.dongambackend.domain.comment.dto.response.FindAllCommentResponse
import com.kotlin.study.dongambackend.domain.comment.entity.Comment
import com.kotlin.study.dongambackend.domain.comment.entity.ReportComment
import com.kotlin.study.dongambackend.domain.comment.mapper.CommentMapper
Expand All @@ -15,6 +16,7 @@ import com.kotlin.study.dongambackend.domain.comment.repository.CommentRepositor
import com.kotlin.study.dongambackend.domain.post.repository.PostRepository
import com.kotlin.study.dongambackend.domain.user.repository.UserRepository
import lombok.extern.slf4j.Slf4j
import org.slf4j.LoggerFactory
import org.springframework.data.domain.Pageable
import org.springframework.data.domain.Slice
import org.springframework.data.domain.SliceImpl
Expand All @@ -32,12 +34,13 @@ class CommentService(
private val userRepository: UserRepository,
private val postRepository: PostRepository
) {

@Transactional(readOnly = true)
fun getAllComment(commentId: Long, pageable: Pageable): Slice<CommentResponse> {
val comments = commentQueryDslRepository.searchCommentsBySlice(commentId, pageable)
val commentResponses = commentMapper.convertCommentsToResponses(comments)
fun getAllComment(postId: Long, commentId: Long, pageable: Pageable): Slice<FindAllCommentResponse> {
val comments = commentQueryDslRepository.searchCommentsByPostId(postId, commentId, pageable)
// val commentResponses = commentMapper.convertCommentsToResponses(comments)

return checkLastPage(pageable, commentResponses)
return checkLastPage(pageable, comments)
}

fun createComment(commentCreateRequest: CommentCreateRequest, postId: Long, userId: Long?): Long? {
Expand Down Expand Up @@ -81,10 +84,11 @@ class CommentService(
// -- 메서드 --

// 무한 스크롤
private fun checkLastPage(pageable: Pageable, results: List<CommentResponse>): Slice<CommentResponse> {
private fun checkLastPage(pageable: Pageable, results: List<FindAllCommentResponse>): Slice<FindAllCommentResponse> {
val hasNext = results.size > pageable.pageSize
val mutableResults = if (hasNext) results.subList(0, pageable.pageSize) else results.toMutableList()

return SliceImpl(mutableResults, pageable, hasNext)
}

}

0 comments on commit 4762e58

Please sign in to comment.