From ecb78647cd750caf3eff9386f0ca5c59f9d6b2c1 Mon Sep 17 00:00:00 2001 From: JIN-076 <57834671+JIN-076@users.noreply.github.com> Date: Mon, 19 Feb 2024 01:37:24 +0900 Subject: [PATCH] =?UTF-8?q?[FEAT]=20=ED=94=84=EB=A1=9C=EC=A0=9D=ED=8A=B8?= =?UTF-8?q?=20=EC=B9=B4=EB=93=9C=20=EB=8F=99=EC=A0=81=20=EC=BF=BC=EB=A6=AC?= =?UTF-8?q?=20=EC=A1=B0=ED=9A=8C=20=EB=A1=9C=EC=A7=81=20=EA=B5=AC=ED=98=84?= =?UTF-8?q?,=20=EC=BB=A4=EC=84=9C=20=EA=B8=B0=EB=B0=98=20=ED=8E=98?= =?UTF-8?q?=EC=9D=B4=EC=A7=80=EB=84=A4=EC=9D=B4=EC=85=98=20=EC=A0=81?= =?UTF-8?q?=EC=9A=A9=20(#74)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * [FEAT] 프로젝트 카드 동적 쿼리 조회 로직 구현, 커서 기반 페이지네이션 적용 * [REFACT] 불필요한 프로젝트 카드 조회 메서드 제거, Slice 생성 로직 수정 * [FIX] cursorId에 대해 더 큰 값을 가져오도록 오류 수정 (eq -> gt) --- .../infrastructure/ProjectJpaRepository.java | 2 +- .../ProjectRepositoryCustom.java | 14 +++++ .../ProjectRepositoryCustomImpl.java | 63 +++++++++++++++++++ 3 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 src/main/java/io/oeid/mogakgo/domain/project/infrastructure/ProjectRepositoryCustom.java create mode 100644 src/main/java/io/oeid/mogakgo/domain/project/infrastructure/ProjectRepositoryCustomImpl.java diff --git a/src/main/java/io/oeid/mogakgo/domain/project/infrastructure/ProjectJpaRepository.java b/src/main/java/io/oeid/mogakgo/domain/project/infrastructure/ProjectJpaRepository.java index 283dad49..52b1d5b5 100644 --- a/src/main/java/io/oeid/mogakgo/domain/project/infrastructure/ProjectJpaRepository.java +++ b/src/main/java/io/oeid/mogakgo/domain/project/infrastructure/ProjectJpaRepository.java @@ -7,7 +7,7 @@ import org.springframework.stereotype.Repository; @Repository -public interface ProjectJpaRepository extends JpaRepository { +public interface ProjectJpaRepository extends JpaRepository, ProjectRepositoryCustom { @Query("select p from Project p where p.id = :id and p.deletedAt is null") @Override diff --git a/src/main/java/io/oeid/mogakgo/domain/project/infrastructure/ProjectRepositoryCustom.java b/src/main/java/io/oeid/mogakgo/domain/project/infrastructure/ProjectRepositoryCustom.java new file mode 100644 index 00000000..205cfc0e --- /dev/null +++ b/src/main/java/io/oeid/mogakgo/domain/project/infrastructure/ProjectRepositoryCustom.java @@ -0,0 +1,14 @@ +package io.oeid.mogakgo.domain.project.infrastructure; + +import io.oeid.mogakgo.domain.geo.domain.enums.Region; +import io.oeid.mogakgo.domain.project.domain.entity.Project; +import io.oeid.mogakgo.domain.project.domain.entity.enums.ProjectStatus; +import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Slice; + +public interface ProjectRepositoryCustom { + + Slice findByCondition( + Long cursorId, Long userId, Region region, ProjectStatus projectStatus, Pageable pageable + ); +} diff --git a/src/main/java/io/oeid/mogakgo/domain/project/infrastructure/ProjectRepositoryCustomImpl.java b/src/main/java/io/oeid/mogakgo/domain/project/infrastructure/ProjectRepositoryCustomImpl.java new file mode 100644 index 00000000..d33a7b93 --- /dev/null +++ b/src/main/java/io/oeid/mogakgo/domain/project/infrastructure/ProjectRepositoryCustomImpl.java @@ -0,0 +1,63 @@ +package io.oeid.mogakgo.domain.project.infrastructure; + +import static io.oeid.mogakgo.domain.project.domain.entity.QProject.project; + +import com.querydsl.core.types.dsl.BooleanExpression; +import com.querydsl.jpa.impl.JPAQueryFactory; +import io.oeid.mogakgo.domain.geo.domain.enums.Region; +import io.oeid.mogakgo.domain.project.domain.entity.Project; +import io.oeid.mogakgo.domain.project.domain.entity.enums.ProjectStatus; +import org.springframework.data.domain.Pageable; +import java.util.List; +import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.Slice; +import org.springframework.data.domain.SliceImpl; +import org.springframework.stereotype.Repository; + +@Repository +@RequiredArgsConstructor +public class ProjectRepositoryCustomImpl implements ProjectRepositoryCustom { + + private final JPAQueryFactory jpaQueryFactory; + + @Override + public Slice findByCondition( + Long cursorId, Long userId, Region region, ProjectStatus projectStatus, Pageable pageable + ) { + List result = jpaQueryFactory.selectFrom(project) + .where( + cursorIdEq(cursorId), + userIdEq(userId), + regionEq(region), + projectStatusEq(projectStatus) + ) + .limit(pageable.getPageSize() + 1) + .fetch(); + boolean hasNext = checkLastPage(result, pageable); + return new SliceImpl<>(result, pageable, hasNext); + } + + private BooleanExpression userIdEq(Long userId) { + return userId != null ? project.creator.id.eq(userId) : null; + } + + private BooleanExpression regionEq(Region region) { + return region != null ? project.creatorInfo.region.eq(region) : null; + } + + private BooleanExpression projectStatusEq(ProjectStatus projectStatus) { + return projectStatus != null ? project.projectStatus.eq(projectStatus) : null; + } + + private BooleanExpression cursorIdEq(Long cursorId) { + return cursorId != null ? project.id.gt(cursorId) : null; + } + + private boolean checkLastPage(List projects, Pageable pageable) { + if (projects.size() > pageable.getPageSize()) { + projects.remove(pageable.getPageSize()); + return true; + } + return false; + } +}