From 0f298cc7a144dabf6ee3d229f769d6f063bb644e Mon Sep 17 00:00:00 2001 From: Hyun Date: Tue, 18 Jul 2023 19:59:42 +0900 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20=EC=BB=A4=EB=AE=A4=EB=8B=88?= =?UTF-8?q?=ED=8B=B0=20=EA=B2=8C=EC=8B=9C=EA=B8=80=20=EC=A1=B0=ED=9A=8C=20?= =?UTF-8?q?=EB=A1=9C=EC=A7=81=20=EC=88=98=EC=A0=95=20=EB=B0=8F=20=EB=B2=84?= =?UTF-8?q?=EA=B7=B8=20=EC=88=98=EC=A0=95=20(#133)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * :sparkles: 커뮤니티 게시글 리스트 조회 로직 수정 (#129) * :sparkles: 커뮤니티 게시글 리스트 조회 response 다음 페이지 인덱스 추가 (#129) * :bug: 커뮤니티 게시글 싫어요 초기 생성 시 좋아요로 되는 문제 수정 (#129) --- .../web/in/dto/post/GetCommunityPostDto.kt | 30 +++++------ .../web/in/dto/post/SearchCommunityPostDto.kt | 36 ++++++------- .../web/out/CommunityPostPersistence.kt | 7 +-- .../web/out/CustomCommunityPostRepository.kt | 51 +++++++++++++++---- .../port/out/CommunityPostReader.kt | 7 +-- .../service/CommunityPostLikeService.kt | 2 +- .../service/CommunityPostService.kt | 11 ++-- .../{CommunityPost.kt => GetCommunityPost.kt} | 2 +- .../community/domain/SearchCommunityPost.kt | 20 ++++++++ .../web/in/CommunityPostControllerDocsTest.kt | 2 + 10 files changed, 111 insertions(+), 57 deletions(-) rename ahachul_backend/src/main/kotlin/backend/team/ahachul_backend/api/community/domain/{CommunityPost.kt => GetCommunityPost.kt} (95%) create mode 100644 ahachul_backend/src/main/kotlin/backend/team/ahachul_backend/api/community/domain/SearchCommunityPost.kt diff --git a/ahachul_backend/src/main/kotlin/backend/team/ahachul_backend/api/community/adapter/web/in/dto/post/GetCommunityPostDto.kt b/ahachul_backend/src/main/kotlin/backend/team/ahachul_backend/api/community/adapter/web/in/dto/post/GetCommunityPostDto.kt index 6d751a81..112fefa0 100644 --- a/ahachul_backend/src/main/kotlin/backend/team/ahachul_backend/api/community/adapter/web/in/dto/post/GetCommunityPostDto.kt +++ b/ahachul_backend/src/main/kotlin/backend/team/ahachul_backend/api/community/adapter/web/in/dto/post/GetCommunityPostDto.kt @@ -1,6 +1,6 @@ package backend.team.ahachul_backend.api.community.adapter.web.`in`.dto.post -import backend.team.ahachul_backend.api.community.domain.CommunityPost +import backend.team.ahachul_backend.api.community.domain.GetCommunityPost import backend.team.ahachul_backend.api.community.domain.model.CommunityCategoryType import backend.team.ahachul_backend.common.dto.ImageDto import backend.team.ahachul_backend.common.model.RegionType @@ -28,23 +28,23 @@ class GetCommunityPostDto { val images: List ) { companion object { - fun of(communityPost: CommunityPost, hashTags: List, views: Int, images: List): Response { + fun of(getCommunityPost: GetCommunityPost, hashTags: List, views: Int, images: List): Response { return Response( - id = communityPost.id, - title = communityPost.title, - content = communityPost.content, - categoryType = communityPost.categoryType, + id = getCommunityPost.id, + title = getCommunityPost.title, + content = getCommunityPost.content, + categoryType = getCommunityPost.categoryType, hashTags = hashTags, viewCnt = views, - likeCnt = communityPost.likeCnt.toInt(), - hateCnt = communityPost.hateCnt.toInt(), - likeYn = YNType.convert(communityPost.likeYn), - hateYn = YNType.convert(communityPost.hateYn), - regionType = communityPost.regionType, - subwayLineId = communityPost.subwayLineId, - createdAt = communityPost.createdAt, - createdBy = communityPost.createdBy, - writer = communityPost.writer, + likeCnt = getCommunityPost.likeCnt.toInt(), + hateCnt = getCommunityPost.hateCnt.toInt(), + likeYn = YNType.convert(getCommunityPost.likeYn), + hateYn = YNType.convert(getCommunityPost.hateYn), + regionType = getCommunityPost.regionType, + subwayLineId = getCommunityPost.subwayLineId, + createdAt = getCommunityPost.createdAt, + createdBy = getCommunityPost.createdBy, + writer = getCommunityPost.writer, images = images ) } diff --git a/ahachul_backend/src/main/kotlin/backend/team/ahachul_backend/api/community/adapter/web/in/dto/post/SearchCommunityPostDto.kt b/ahachul_backend/src/main/kotlin/backend/team/ahachul_backend/api/community/adapter/web/in/dto/post/SearchCommunityPostDto.kt index fc0bdc0f..b30cfd42 100644 --- a/ahachul_backend/src/main/kotlin/backend/team/ahachul_backend/api/community/adapter/web/in/dto/post/SearchCommunityPostDto.kt +++ b/ahachul_backend/src/main/kotlin/backend/team/ahachul_backend/api/community/adapter/web/in/dto/post/SearchCommunityPostDto.kt @@ -1,6 +1,6 @@ package backend.team.ahachul_backend.api.community.adapter.web.`in`.dto.post -import backend.team.ahachul_backend.api.community.domain.entity.CommunityPostEntity +import backend.team.ahachul_backend.api.community.domain.SearchCommunityPost import backend.team.ahachul_backend.api.community.domain.model.CommunityCategoryType import backend.team.ahachul_backend.common.dto.ImageDto import backend.team.ahachul_backend.common.model.RegionType @@ -28,12 +28,14 @@ class SearchCommunityPostDto { data class Response( val hasNext: Boolean, + val nextPageNum: Int?, val posts: List, ) { companion object { - fun of(hasNext: Boolean, posts: List): Response { + fun of(hasNext: Boolean, posts: List, currentPageNum: Int): Response { return Response( hasNext = hasNext, + nextPageNum = if (hasNext) currentPageNum + 1 else null, posts = posts ) } @@ -46,9 +48,9 @@ class SearchCommunityPostDto { val content: String, val categoryType: CommunityCategoryType, val hashTags: List, - val commentCnt: Int, + val commentCnt: Long, val viewCnt: Int, - val likeCnt: Int, + val likeCnt: Long, val regionType: RegionType, val subwayLineId: Long, val createdAt: LocalDateTime, @@ -57,21 +59,21 @@ class SearchCommunityPostDto { val image: ImageDto?, ) { companion object { - fun of(entity: CommunityPostEntity, image: ImageDto?, views: Int, commentCnt: Int, likeCnt: Int): CommunityPost { + fun of(searchCommunityPost: SearchCommunityPost, image: ImageDto?, views: Int, hashTags: List): CommunityPost { return CommunityPost( - id = entity.id, - title = entity.title, - content = entity.content, - categoryType = entity.categoryType, - hashTags = entity.communityPostHashTags.map { it.hashTag.name }, - commentCnt = commentCnt, + id = searchCommunityPost.id, + title = searchCommunityPost.title, + content = searchCommunityPost.content, + categoryType = searchCommunityPost.categoryType, + hashTags = hashTags, + commentCnt = searchCommunityPost.commentCnt, viewCnt = views, - likeCnt = likeCnt, - regionType = entity.regionType, - subwayLineId = entity.subwayLineEntity.id, - createdAt = entity.createdAt, - createdBy = entity.createdBy, - writer = entity.member!!.nickname!!, + likeCnt = searchCommunityPost.likeCnt, + regionType = searchCommunityPost.regionType, + subwayLineId = searchCommunityPost.subwayLineId, + createdAt = searchCommunityPost.createdAt, + createdBy = searchCommunityPost.createdBy, + writer = searchCommunityPost.writer, image = image, ) } diff --git a/ahachul_backend/src/main/kotlin/backend/team/ahachul_backend/api/community/adapter/web/out/CommunityPostPersistence.kt b/ahachul_backend/src/main/kotlin/backend/team/ahachul_backend/api/community/adapter/web/out/CommunityPostPersistence.kt index 5d0aee00..c308fdf0 100644 --- a/ahachul_backend/src/main/kotlin/backend/team/ahachul_backend/api/community/adapter/web/out/CommunityPostPersistence.kt +++ b/ahachul_backend/src/main/kotlin/backend/team/ahachul_backend/api/community/adapter/web/out/CommunityPostPersistence.kt @@ -3,7 +3,8 @@ package backend.team.ahachul_backend.api.community.adapter.web.out import backend.team.ahachul_backend.api.community.adapter.web.`in`.dto.post.SearchCommunityPostCommand import backend.team.ahachul_backend.api.community.application.port.out.CommunityPostReader import backend.team.ahachul_backend.api.community.application.port.out.CommunityPostWriter -import backend.team.ahachul_backend.api.community.domain.CommunityPost +import backend.team.ahachul_backend.api.community.domain.GetCommunityPost +import backend.team.ahachul_backend.api.community.domain.SearchCommunityPost import backend.team.ahachul_backend.api.community.domain.entity.CommunityPostEntity import backend.team.ahachul_backend.common.exception.AdapterException import backend.team.ahachul_backend.common.response.ResponseCode @@ -25,11 +26,11 @@ class CommunityPostPersistence( .orElseThrow { throw AdapterException(ResponseCode.INVALID_DOMAIN) } } - override fun getByCustom(postId: Long, memberId: String?): CommunityPost { + override fun getByCustom(postId: Long, memberId: String?): GetCommunityPost { return customRepository.getByCustom(postId, memberId) ?: throw AdapterException(ResponseCode.INVALID_DOMAIN) } - override fun searchCommunityPosts(command: SearchCommunityPostCommand): Slice { + override fun searchCommunityPosts(command: SearchCommunityPostCommand): Slice { return customRepository.searchCommunityPosts(command) } } \ No newline at end of file diff --git a/ahachul_backend/src/main/kotlin/backend/team/ahachul_backend/api/community/adapter/web/out/CustomCommunityPostRepository.kt b/ahachul_backend/src/main/kotlin/backend/team/ahachul_backend/api/community/adapter/web/out/CustomCommunityPostRepository.kt index 05882530..be5eab1c 100644 --- a/ahachul_backend/src/main/kotlin/backend/team/ahachul_backend/api/community/adapter/web/out/CustomCommunityPostRepository.kt +++ b/ahachul_backend/src/main/kotlin/backend/team/ahachul_backend/api/community/adapter/web/out/CustomCommunityPostRepository.kt @@ -1,8 +1,9 @@ package backend.team.ahachul_backend.api.community.adapter.web.out import backend.team.ahachul_backend.api.community.adapter.web.`in`.dto.post.SearchCommunityPostCommand -import backend.team.ahachul_backend.api.community.domain.CommunityPost -import backend.team.ahachul_backend.api.community.domain.entity.CommunityPostEntity +import backend.team.ahachul_backend.api.community.domain.GetCommunityPost +import backend.team.ahachul_backend.api.community.domain.SearchCommunityPost +import backend.team.ahachul_backend.api.community.domain.entity.QCommunityCommentEntity.communityCommentEntity import backend.team.ahachul_backend.api.community.domain.entity.QCommunityPostEntity.communityPostEntity import backend.team.ahachul_backend.api.community.domain.entity.QCommunityPostHashTagEntity.communityPostHashTagEntity import backend.team.ahachul_backend.api.community.domain.entity.QCommunityPostLikeEntity.communityPostLikeEntity @@ -16,6 +17,7 @@ import com.querydsl.core.types.ExpressionUtils.count import com.querydsl.core.types.OrderSpecifier import com.querydsl.core.types.Projections import com.querydsl.core.types.dsl.Expressions +import com.querydsl.core.types.dsl.NumberPath import com.querydsl.jpa.JPAExpressions import com.querydsl.jpa.impl.JPAQueryFactory import org.springframework.data.domain.Pageable @@ -28,10 +30,10 @@ class CustomCommunityPostRepository( private val queryFactory: JPAQueryFactory ) { - fun getByCustom(postId: Long, memberId: String?): CommunityPost? { + fun getByCustom(postId: Long, memberId: String?): GetCommunityPost? { return queryFactory.select( Projections.constructor( - CommunityPost::class.java, + GetCommunityPost::class.java, communityPostEntity.id, communityPostEntity.title, communityPostEntity.content, @@ -98,11 +100,40 @@ class CustomCommunityPostRepository( .fetchOne() } - fun searchCommunityPosts(command: SearchCommunityPostCommand): Slice { + fun searchCommunityPosts(command: SearchCommunityPostCommand): Slice { val pageable = command.pageable - var result = queryFactory.selectFrom(communityPostEntity) - .join(communityPostEntity.member, memberEntity).fetchJoin() - .join(communityPostEntity.subwayLineEntity, subwayLineEntity).fetchJoin() + + var result = queryFactory.select( + Projections.constructor( + SearchCommunityPost::class.java, + communityPostEntity.id, + communityPostEntity.title, + communityPostEntity.content, + communityPostEntity.categoryType, + communityPostEntity.regionType, + communityPostEntity.subwayLineEntity.id, + ExpressionUtils.`as`( + JPAExpressions.select(count(communityPostLikeEntity.id)) + .from(communityPostLikeEntity) + .where( + communityPostLikeEntity.communityPost.id.eq(communityPostEntity.id) + .and(communityPostLikeEntity.likeYn.eq(YNType.Y)) + ), + "likeCnt" + ), + JPAExpressions.select(communityCommentEntity.id.count()) + .from(communityCommentEntity) + .where( + communityCommentEntity.communityPost.id.eq(communityPostEntity.id) + ), + communityPostEntity.createdAt, + communityPostEntity.createdBy, + communityPostEntity.member.nickname, + ) + ) + .from(communityPostEntity) + .join(communityPostEntity.member, memberEntity) + .join(communityPostEntity.subwayLineEntity, subwayLineEntity) .where( categoryTypeEq(command.categoryType), subwayLineIdEq(command.subwayLineId), @@ -126,7 +157,7 @@ class CustomCommunityPostRepository( val property = pageable.sort.toList()[0].property val direction = pageable.sort.toList()[0].direction val path = when (property) { -// "likes" -> TODO + "likes" -> Expressions.numberPath(Long::class.java, "likeCnt") "createdAt" -> communityPostEntity.createdAt "views" -> communityPostEntity.views else -> communityPostEntity.createdAt @@ -141,7 +172,7 @@ class CustomCommunityPostRepository( } } - private fun hasNext(result: MutableList, pageable: Pageable): Boolean { + private fun hasNext(result: MutableList, pageable: Pageable): Boolean { return result.size > pageable.pageSize } diff --git a/ahachul_backend/src/main/kotlin/backend/team/ahachul_backend/api/community/application/port/out/CommunityPostReader.kt b/ahachul_backend/src/main/kotlin/backend/team/ahachul_backend/api/community/application/port/out/CommunityPostReader.kt index 428ada04..14fcc209 100644 --- a/ahachul_backend/src/main/kotlin/backend/team/ahachul_backend/api/community/application/port/out/CommunityPostReader.kt +++ b/ahachul_backend/src/main/kotlin/backend/team/ahachul_backend/api/community/application/port/out/CommunityPostReader.kt @@ -1,7 +1,8 @@ package backend.team.ahachul_backend.api.community.application.port.out import backend.team.ahachul_backend.api.community.adapter.web.`in`.dto.post.SearchCommunityPostCommand -import backend.team.ahachul_backend.api.community.domain.CommunityPost +import backend.team.ahachul_backend.api.community.domain.GetCommunityPost +import backend.team.ahachul_backend.api.community.domain.SearchCommunityPost import backend.team.ahachul_backend.api.community.domain.entity.CommunityPostEntity import org.springframework.data.domain.Slice @@ -9,7 +10,7 @@ interface CommunityPostReader { fun getCommunityPost(id: Long): CommunityPostEntity - fun getByCustom(postId: Long, memberId: String?): CommunityPost + fun getByCustom(postId: Long, memberId: String?): GetCommunityPost - fun searchCommunityPosts(command: SearchCommunityPostCommand): Slice + fun searchCommunityPosts(command: SearchCommunityPostCommand): Slice } \ No newline at end of file diff --git a/ahachul_backend/src/main/kotlin/backend/team/ahachul_backend/api/community/application/service/CommunityPostLikeService.kt b/ahachul_backend/src/main/kotlin/backend/team/ahachul_backend/api/community/application/service/CommunityPostLikeService.kt index 5e9b1d61..2289e126 100644 --- a/ahachul_backend/src/main/kotlin/backend/team/ahachul_backend/api/community/application/service/CommunityPostLikeService.kt +++ b/ahachul_backend/src/main/kotlin/backend/team/ahachul_backend/api/community/application/service/CommunityPostLikeService.kt @@ -71,7 +71,7 @@ class CommunityPostLikeService ( CommunityPostLikeEntity.of( communityPost = communityPostReader.getCommunityPost(postId), member = memberReader.getMember(memberId), - YNType.Y + YNType.N ) ) } diff --git a/ahachul_backend/src/main/kotlin/backend/team/ahachul_backend/api/community/application/service/CommunityPostService.kt b/ahachul_backend/src/main/kotlin/backend/team/ahachul_backend/api/community/application/service/CommunityPostService.kt index 101d7707..78dba05f 100644 --- a/ahachul_backend/src/main/kotlin/backend/team/ahachul_backend/api/community/application/service/CommunityPostService.kt +++ b/ahachul_backend/src/main/kotlin/backend/team/ahachul_backend/api/community/application/service/CommunityPostService.kt @@ -7,7 +7,6 @@ import backend.team.ahachul_backend.api.community.domain.entity.CommunityPostEnt import backend.team.ahachul_backend.api.community.domain.entity.CommunityPostFileEntity import backend.team.ahachul_backend.api.member.application.port.out.MemberReader import backend.team.ahachul_backend.common.dto.ImageDto -import backend.team.ahachul_backend.common.model.YNType import backend.team.ahachul_backend.common.persistence.SubwayLineReader import backend.team.ahachul_backend.common.support.ViewsSupport import backend.team.ahachul_backend.common.utils.RequestUtils @@ -24,8 +23,6 @@ class CommunityPostService( private val subwayLineReader: SubwayLineReader, private val communityPostHashTagReader: CommunityPostHashTagReader, private val communityPostFileReader: CommunityPostFileReader, - private val communityCommentReader: CommunityCommentReader, - private val communityPostLikeReader: CommunityPostLikeReader, private val communityPostHashTagService: CommunityPostHashTagService, private val communityPostFileService: CommunityPostFileService, @@ -39,17 +36,17 @@ class CommunityPostService( .map { val file = communityPostFileReader.findByPostId(it.id)?.file SearchCommunityPostDto.CommunityPost.of( - entity = it, + searchCommunityPost = it, image = file?.let { it1 -> ImageDto.of(it1.id, file.filePath) }, views = viewsSupport.get(it.id), - commentCnt = communityCommentReader.count(it.id), - likeCnt = communityPostLikeReader.count(it.id, YNType.Y) + hashTags = communityPostHashTagReader.findAllByPostId(it.id).map { it.hashTag.name } ) }.toList() return SearchCommunityPostDto.Response.of( hasNext = searchCommunityPosts.hasNext(), - posts = posts + posts = posts, + command.pageable.pageNumber, ) } diff --git a/ahachul_backend/src/main/kotlin/backend/team/ahachul_backend/api/community/domain/CommunityPost.kt b/ahachul_backend/src/main/kotlin/backend/team/ahachul_backend/api/community/domain/GetCommunityPost.kt similarity index 95% rename from ahachul_backend/src/main/kotlin/backend/team/ahachul_backend/api/community/domain/CommunityPost.kt rename to ahachul_backend/src/main/kotlin/backend/team/ahachul_backend/api/community/domain/GetCommunityPost.kt index 3752cdab..34fa07bc 100644 --- a/ahachul_backend/src/main/kotlin/backend/team/ahachul_backend/api/community/domain/CommunityPost.kt +++ b/ahachul_backend/src/main/kotlin/backend/team/ahachul_backend/api/community/domain/GetCommunityPost.kt @@ -4,7 +4,7 @@ import backend.team.ahachul_backend.api.community.domain.model.CommunityCategory import backend.team.ahachul_backend.common.model.RegionType import java.time.LocalDateTime -data class CommunityPost( +data class GetCommunityPost( val id: Long, val title: String, val content: String, diff --git a/ahachul_backend/src/main/kotlin/backend/team/ahachul_backend/api/community/domain/SearchCommunityPost.kt b/ahachul_backend/src/main/kotlin/backend/team/ahachul_backend/api/community/domain/SearchCommunityPost.kt new file mode 100644 index 00000000..77972417 --- /dev/null +++ b/ahachul_backend/src/main/kotlin/backend/team/ahachul_backend/api/community/domain/SearchCommunityPost.kt @@ -0,0 +1,20 @@ +package backend.team.ahachul_backend.api.community.domain + +import backend.team.ahachul_backend.api.community.domain.model.CommunityCategoryType +import backend.team.ahachul_backend.common.model.RegionType +import java.time.LocalDateTime + +data class SearchCommunityPost( + val id: Long, + val title: String, + val content: String, + val categoryType: CommunityCategoryType, + val regionType: RegionType, + val subwayLineId: Long, + val likeCnt: Long, + val commentCnt: Long, + val createdAt: LocalDateTime, + val createdBy: String, + val writer: String, +) { +} \ No newline at end of file diff --git a/ahachul_backend/src/test/kotlin/backend/team/ahachul_backend/api/community/adapter/web/in/CommunityPostControllerDocsTest.kt b/ahachul_backend/src/test/kotlin/backend/team/ahachul_backend/api/community/adapter/web/in/CommunityPostControllerDocsTest.kt index 02eeca9e..6b07e03d 100644 --- a/ahachul_backend/src/test/kotlin/backend/team/ahachul_backend/api/community/adapter/web/in/CommunityPostControllerDocsTest.kt +++ b/ahachul_backend/src/test/kotlin/backend/team/ahachul_backend/api/community/adapter/web/in/CommunityPostControllerDocsTest.kt @@ -35,6 +35,7 @@ class CommunityPostControllerDocsTest : CommonDocsTestConfig() { // given val response = SearchCommunityPostDto.Response( true, + 1, listOf( SearchCommunityPostDto.CommunityPost( id = 1, @@ -90,6 +91,7 @@ class CommunityPostControllerDocsTest : CommonDocsTestConfig() { responseFields( *commonResponseFields(), fieldWithPath("result.hasNext").type(JsonFieldType.BOOLEAN).description("다음 페이지 존재 여부"), + fieldWithPath("result.nextPageNum").type(JsonFieldType.NUMBER).description("다음 페이지 인덱스"), fieldWithPath("result.posts[].id").type(JsonFieldType.NUMBER).description("게시글 아이디"), fieldWithPath("result.posts[].title").type(JsonFieldType.STRING).description("게시글 제목"), fieldWithPath("result.posts[].content").type(JsonFieldType.STRING).description("게시글 내용"),