-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEAT] ProfileCard 동적 쿼리 조회 메서드 구현, 커서 기반 페이지네이션 적용
- Loading branch information
Showing
3 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
src/main/java/io/oeid/mogakgo/domain/profile/infrastructure/ProfileCardJpaRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/io/oeid/mogakgo/domain/profile/infrastructure/ProfileCardRepositoryCustom.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); | ||
} |
57 changes: 57 additions & 0 deletions
57
...n/java/io/oeid/mogakgo/domain/profile/infrastructure/ProfileCardRepositoryCustomImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |