From 0a53dd5bad05f389dc0242309f6449d70b97bec5 Mon Sep 17 00:00:00 2001 From: mingmingmon Date: Sun, 22 Dec 2024 12:50:18 +0900 Subject: [PATCH] =?UTF-8?q?refactor(BoardHashtagRepository):=20SQL?= =?UTF-8?q?=EB=AC=B8=EC=97=90=20=EA=B0=9C=ED=96=89=EC=B6=94=EA=B0=80,=20cu?= =?UTF-8?q?stomRepository=EC=97=90=EC=84=9C=20JPQL=EB=A1=9C=20=EC=9E=91?= =?UTF-8?q?=EC=84=B1=ED=95=9C=20=EB=82=B4=EC=9A=A9=EC=9D=84=20=EC=96=B4?= =?UTF-8?q?=EB=85=B8=ED=85=8C=EC=9D=B4=EC=85=98=EC=9C=BC=EB=A1=9C=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BoardHashtagPersistenceAdapter.java | 4 +-- .../persistence/BoardHashtagRepository.java | 17 +++++++++-- .../BoardHashtagRepositoryCustom.java | 9 ------ .../BoardHashtagRepositoryImpl.java | 29 ------------------- .../port/out/RetrieveBoardHashtagPort.java | 2 +- .../BoardsByHashtagRetrievalService.java | 2 +- .../ExternalRetrieveBoardHashtagUseCase.java | 2 +- .../ExternalBoardHashtagRetrieveService.java | 4 +-- 8 files changed, 22 insertions(+), 47 deletions(-) delete mode 100644 src/main/java/page/clab/api/domain/community/board/adapter/out/persistence/BoardHashtagRepositoryCustom.java delete mode 100644 src/main/java/page/clab/api/domain/community/board/adapter/out/persistence/BoardHashtagRepositoryImpl.java diff --git a/src/main/java/page/clab/api/domain/community/board/adapter/out/persistence/BoardHashtagPersistenceAdapter.java b/src/main/java/page/clab/api/domain/community/board/adapter/out/persistence/BoardHashtagPersistenceAdapter.java index 39d4836b0..3c4391243 100644 --- a/src/main/java/page/clab/api/domain/community/board/adapter/out/persistence/BoardHashtagPersistenceAdapter.java +++ b/src/main/java/page/clab/api/domain/community/board/adapter/out/persistence/BoardHashtagPersistenceAdapter.java @@ -38,7 +38,7 @@ public List getAllIncludingDeletedByBoardId(Long boardId) { .collect(Collectors.toList()); } - public List getBoardIdsByHashTagId(List hashtagIds, Pageable pageable) { - return repository.getBoardIdsByHashTagId(hashtagIds, pageable); + public List getBoardIdsByHashTagId(List hashtagIds) { + return repository.getBoardIdsByHashTagId(hashtagIds, (long)hashtagIds.size()); } } diff --git a/src/main/java/page/clab/api/domain/community/board/adapter/out/persistence/BoardHashtagRepository.java b/src/main/java/page/clab/api/domain/community/board/adapter/out/persistence/BoardHashtagRepository.java index 41b9d9889..84ee1ca14 100644 --- a/src/main/java/page/clab/api/domain/community/board/adapter/out/persistence/BoardHashtagRepository.java +++ b/src/main/java/page/clab/api/domain/community/board/adapter/out/persistence/BoardHashtagRepository.java @@ -1,16 +1,29 @@ package page.clab.api.domain.community.board.adapter.out.persistence; import java.util.List; +import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import org.springframework.data.querydsl.QuerydslPredicateExecutor; +import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; @Repository -public interface BoardHashtagRepository extends JpaRepository, BoardHashtagRepositoryCustom, QuerydslPredicateExecutor { +public interface BoardHashtagRepository extends JpaRepository{ List findAllByBoardId(Long boardId); - @Query(value = "SELECT b.* FROM board_hashtag b WHERE b.board_id = :boardId", nativeQuery = true) + @Query(value = "SELECT b.* " + + "FROM board_hashtag b " + + "WHERE b.board_id = :boardId", + nativeQuery = true) List findAllIncludingDeletedByBoardId(Long boardId); + + @Query("SELECT DISTINCT b.boardId " + + "FROM BoardHashtagJpaEntity b " + + "WHERE b.hashtagId IN :hashtagIds " + + "AND b.isDeleted = false " + + "GROUP BY b.boardId " + + "HAVING COUNT(b.hashtagId) = :size") + List getBoardIdsByHashTagId(@Param("hashtagIds") List hashtagIds, @Param("size") Long size); } diff --git a/src/main/java/page/clab/api/domain/community/board/adapter/out/persistence/BoardHashtagRepositoryCustom.java b/src/main/java/page/clab/api/domain/community/board/adapter/out/persistence/BoardHashtagRepositoryCustom.java deleted file mode 100644 index 97644c1c6..000000000 --- a/src/main/java/page/clab/api/domain/community/board/adapter/out/persistence/BoardHashtagRepositoryCustom.java +++ /dev/null @@ -1,9 +0,0 @@ -package page.clab.api.domain.community.board.adapter.out.persistence; - -import java.util.List; -import org.springframework.data.domain.Page; -import org.springframework.data.domain.Pageable; - -public interface BoardHashtagRepositoryCustom { - List getBoardIdsByHashTagId(List hashtagIds, Pageable pageable); -} diff --git a/src/main/java/page/clab/api/domain/community/board/adapter/out/persistence/BoardHashtagRepositoryImpl.java b/src/main/java/page/clab/api/domain/community/board/adapter/out/persistence/BoardHashtagRepositoryImpl.java deleted file mode 100644 index e2a23c601..000000000 --- a/src/main/java/page/clab/api/domain/community/board/adapter/out/persistence/BoardHashtagRepositoryImpl.java +++ /dev/null @@ -1,29 +0,0 @@ -package page.clab.api.domain.community.board.adapter.out.persistence; - -import com.querydsl.jpa.JPQLQuery; -import com.querydsl.jpa.impl.JPAQueryFactory; -import java.util.List; -import lombok.RequiredArgsConstructor; -import org.springframework.data.domain.Pageable; -import org.springframework.stereotype.Repository; - -@Repository -@RequiredArgsConstructor -public class BoardHashtagRepositoryImpl implements BoardHashtagRepositoryCustom { - - private final JPAQueryFactory queryFactory; - - @Override - public List getBoardIdsByHashTagId(List hashtagIds, Pageable pageable) { - QBoardHashtagJpaEntity boardHashtag = QBoardHashtagJpaEntity.boardHashtagJpaEntity; - - JPQLQuery query = queryFactory.selectDistinct(boardHashtag.boardId) - .from(boardHashtag) - .where(boardHashtag.hashtagId.in(hashtagIds) - .and(boardHashtag.isDeleted.eq(false))) - .groupBy(boardHashtag.boardId) - .having(boardHashtag.hashtagId.count().eq((long) hashtagIds.size())); - - return query.fetch(); - } -} diff --git a/src/main/java/page/clab/api/domain/community/board/application/port/out/RetrieveBoardHashtagPort.java b/src/main/java/page/clab/api/domain/community/board/application/port/out/RetrieveBoardHashtagPort.java index b34250bb8..93a12dc34 100644 --- a/src/main/java/page/clab/api/domain/community/board/application/port/out/RetrieveBoardHashtagPort.java +++ b/src/main/java/page/clab/api/domain/community/board/application/port/out/RetrieveBoardHashtagPort.java @@ -10,5 +10,5 @@ public interface RetrieveBoardHashtagPort { List getAllIncludingDeletedByBoardId(Long boardId); - List getBoardIdsByHashTagId(List hashtagIds, Pageable pageable); + List getBoardIdsByHashTagId(List hashtagIds); } diff --git a/src/main/java/page/clab/api/domain/community/board/application/service/BoardsByHashtagRetrievalService.java b/src/main/java/page/clab/api/domain/community/board/application/service/BoardsByHashtagRetrievalService.java index 2053635c5..052e0b793 100644 --- a/src/main/java/page/clab/api/domain/community/board/application/service/BoardsByHashtagRetrievalService.java +++ b/src/main/java/page/clab/api/domain/community/board/application/service/BoardsByHashtagRetrievalService.java @@ -39,7 +39,7 @@ public PagedResponseDto retrieveBoardsByHashtag(List boardIds = externalRetrieveBoardHashtagUseCase.getBoardIdsByHashTagId(hashtagIds, pageable); + List boardIds = externalRetrieveBoardHashtagUseCase.getBoardIdsByHashTagId(hashtagIds); List boards = boardIds.stream() .map(externalRetrieveBoardUseCase::getById) diff --git a/src/main/java/page/clab/api/external/community/board/application/port/ExternalRetrieveBoardHashtagUseCase.java b/src/main/java/page/clab/api/external/community/board/application/port/ExternalRetrieveBoardHashtagUseCase.java index 3fbe05d3c..1764ada44 100644 --- a/src/main/java/page/clab/api/external/community/board/application/port/ExternalRetrieveBoardHashtagUseCase.java +++ b/src/main/java/page/clab/api/external/community/board/application/port/ExternalRetrieveBoardHashtagUseCase.java @@ -15,5 +15,5 @@ public interface ExternalRetrieveBoardHashtagUseCase { List getAllIncludingDeletedByBoardId(Long boardId); - List getBoardIdsByHashTagId(List hashtagIds, Pageable pageable); + List getBoardIdsByHashTagId(List hashtagIds); } diff --git a/src/main/java/page/clab/api/external/community/board/application/service/ExternalBoardHashtagRetrieveService.java b/src/main/java/page/clab/api/external/community/board/application/service/ExternalBoardHashtagRetrieveService.java index e5346fb84..805c53580 100644 --- a/src/main/java/page/clab/api/external/community/board/application/service/ExternalBoardHashtagRetrieveService.java +++ b/src/main/java/page/clab/api/external/community/board/application/service/ExternalBoardHashtagRetrieveService.java @@ -49,7 +49,7 @@ public List getAllIncludingDeletedByBoardId(Long boardId) { } @Override - public List getBoardIdsByHashTagId(List hashtagIds, Pageable pageable) { - return retrieveBoardHashtagPort.getBoardIdsByHashTagId(hashtagIds, pageable); + public List getBoardIdsByHashTagId(List hashtagIds) { + return retrieveBoardHashtagPort.getBoardIdsByHashTagId(hashtagIds); } }