Skip to content

Commit

Permalink
๐Ÿ› fix: ๋ฌดํ•œ์Šคํฌ๋กค ๊ตฌํ˜„ ๋ฌธ์ œ ์ˆ˜์ • (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
devyubin committed Feb 26, 2024
1 parent a15a706 commit 9990102
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 @@ -8,6 +8,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 @@ -17,6 +18,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 @@ -34,12 +36,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 @@ -83,10 +86,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 9990102

Please sign in to comment.