Skip to content

Commit

Permalink
[FEAT] ProfileCard 동적 쿼리 조회 메서드 구현, 커서 기반 페이지네이션 적용
Browse files Browse the repository at this point in the history
  • Loading branch information
JIN-076 committed Feb 18, 2024
1 parent 95f349d commit 65f0fdb
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<ProfileCard, Long>, ProfileCardRepositoryCustom {

}
Original file line number Diff line number Diff line change
@@ -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<ProfileCard> findByCondition(
Long cursorId, Long userId, Region region, Pageable pageable
);
}
Original file line number Diff line number Diff line change
@@ -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<ProfileCard> findByCondition(
Long cursorId, Long userId, Region region, Pageable pageable
) {
List<ProfileCard> 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<ProfileCard> profileCards, Pageable pageable) {
if (profileCards.size() > pageable.getPageSize()) {
profileCards.remove(pageable.getPageSize());
return true;
}
return false;
}
}

0 comments on commit 65f0fdb

Please sign in to comment.