diff --git a/src/main/java/io/oeid/mogakgo/domain/profile/infrastructure/ProfileCardJpaRepository.java b/src/main/java/io/oeid/mogakgo/domain/profile/infrastructure/ProfileCardJpaRepository.java new file mode 100644 index 00000000..bb45a235 --- /dev/null +++ b/src/main/java/io/oeid/mogakgo/domain/profile/infrastructure/ProfileCardJpaRepository.java @@ -0,0 +1,10 @@ +package io.oeid.mogakgo.domain.profile.infrastructure; + +import io.oeid.mogakgo.domain.profile.domain.entity.ProfileCard; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface ProfileCardJpaRepository extends JpaRepository, ProfileCardRepositoryCustom { + +} diff --git a/src/main/java/io/oeid/mogakgo/domain/profile/infrastructure/ProfileCardRepositoryCustom.java b/src/main/java/io/oeid/mogakgo/domain/profile/infrastructure/ProfileCardRepositoryCustom.java new file mode 100644 index 00000000..fdb6ba43 --- /dev/null +++ b/src/main/java/io/oeid/mogakgo/domain/profile/infrastructure/ProfileCardRepositoryCustom.java @@ -0,0 +1,13 @@ +package io.oeid.mogakgo.domain.profile.infrastructure; + +import io.oeid.mogakgo.domain.geo.domain.enums.Region; +import io.oeid.mogakgo.domain.profile.domain.entity.ProfileCard; +import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Slice; + +public interface ProfileCardRepositoryCustom { + + Slice findByCondition( + Long cursorId, Long userId, Region region, Pageable pageable + ); +} diff --git a/src/main/java/io/oeid/mogakgo/domain/profile/infrastructure/ProfileCardRepositoryCustomImpl.java b/src/main/java/io/oeid/mogakgo/domain/profile/infrastructure/ProfileCardRepositoryCustomImpl.java new file mode 100644 index 00000000..31bad7b1 --- /dev/null +++ b/src/main/java/io/oeid/mogakgo/domain/profile/infrastructure/ProfileCardRepositoryCustomImpl.java @@ -0,0 +1,57 @@ +package io.oeid.mogakgo.domain.profile.infrastructure; + +import static io.oeid.mogakgo.domain.profile.domain.entity.QProfileCard.profileCard; + +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.profile.domain.entity.ProfileCard; +import java.util.List; +import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Slice; +import org.springframework.data.domain.SliceImpl; +import org.springframework.stereotype.Repository; + +@Repository +@RequiredArgsConstructor +public class ProfileCardRepositoryCustomImpl implements ProfileCardRepositoryCustom { + + private final JPAQueryFactory jpaQueryFactory; + + @Override + public Slice findByCondition( + Long cursorId, Long userId, Region region, Pageable pageable + ) { + List result = jpaQueryFactory.selectFrom(profileCard) + .where( + cursorIdEq(cursorId), + userIdEq(userId), + regionEq(region) + ) + .limit(pageable.getPageSize() + 1) + .fetch(); + boolean hasNext = checkLastPage(result, pageable); + return new SliceImpl<>(result, pageable, hasNext); + } + + private BooleanExpression regionEq(Region region) { + return region != null ? profileCard.user.region.eq(region) : null; + } + + private BooleanExpression userIdEq(Long userId) { + return userId != null ? profileCard.user.id.eq(userId) : null; + } + + private BooleanExpression cursorIdEq(Long cursorId) { + return cursorId != null ? profileCard.id.gt(cursorId) : null; + } + + private boolean checkLastPage(List profileCards, Pageable pageable) { + if (profileCards.size() > pageable.getPageSize()) { + profileCards.remove(pageable.getPageSize()); + return true; + } + return false; + } +}