-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #245 from linglong67/feature/setting-querydsl
[feat] Querydsl 세팅 및 적용
- Loading branch information
Showing
13 changed files
with
167 additions
and
18 deletions.
There are no files selected for viewing
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
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
20 changes: 20 additions & 0 deletions
20
module-api/src/main/java/com/kernel360/review/dto/ReviewSearchDto.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,20 @@ | ||
package com.kernel360.review.dto; | ||
|
||
public record ReviewSearchDto( | ||
Long productNo, | ||
Long memberNo, | ||
String sortBy | ||
) { | ||
public static ReviewSearchDto of(Long productNo, Long memberNo, String sortBy) { | ||
return new ReviewSearchDto(productNo, memberNo, sortBy); | ||
} | ||
|
||
public static ReviewSearchDto byProductNo(Long productNo, String sortBy) { | ||
return ReviewSearchDto.of(productNo, null, sortBy); | ||
} | ||
|
||
// TODO: 추후 mypage 리뷰 관리에서 사용 예정 | ||
public static ReviewSearchDto byMemberNo(Long memberNo, String sortBy) { | ||
return ReviewSearchDto.of(null, memberNo, sortBy); | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
module-api/src/main/java/com/kernel360/review/repository/ReviewRepository.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,4 @@ | ||
package com.kernel360.review.repository; | ||
|
||
public interface ReviewRepository extends ReviewRepositoryJpa, ReviewRepositoryDsl { | ||
} |
10 changes: 10 additions & 0 deletions
10
module-api/src/main/java/com/kernel360/review/repository/ReviewRepositoryDsl.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 com.kernel360.review.repository; | ||
|
||
import com.kernel360.review.dto.ReviewSearchDto; | ||
import com.kernel360.review.entity.Review; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
|
||
public interface ReviewRepositoryDsl { | ||
Page<Review> findAllByCondition(ReviewSearchDto condition, Pageable pageable); | ||
} |
65 changes: 65 additions & 0 deletions
65
module-api/src/main/java/com/kernel360/review/repository/ReviewRepositoryImpl.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,65 @@ | ||
package com.kernel360.review.repository; | ||
|
||
import com.kernel360.review.dto.ReviewSearchDto; | ||
import com.kernel360.review.entity.Review; | ||
import com.querydsl.core.types.OrderSpecifier; | ||
import com.querydsl.core.types.dsl.BooleanExpression; | ||
import com.querydsl.jpa.impl.JPAQueryFactory; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.PageImpl; | ||
import org.springframework.data.domain.Pageable; | ||
|
||
import java.util.List; | ||
|
||
import static com.kernel360.review.entity.QReview.review; | ||
|
||
@RequiredArgsConstructor | ||
public class ReviewRepositoryImpl implements ReviewRepositoryDsl { | ||
|
||
private final JPAQueryFactory queryFactory; | ||
|
||
@Override | ||
public Page<Review> findAllByCondition(ReviewSearchDto condition, Pageable pageable) { | ||
List<Review> reviews = queryFactory | ||
.select(review) | ||
.from(review) | ||
.where( | ||
productNoEq(condition.productNo()), | ||
memberNoEq(condition.memberNo())) | ||
.offset(pageable.getOffset()) | ||
.limit(pageable.getPageSize()) | ||
.orderBy(sort(condition.sortBy())) | ||
.fetch(); | ||
|
||
Long totalCount = queryFactory | ||
.select(review.count()) | ||
.from(review) | ||
.where( | ||
productNoEq(condition.productNo()), | ||
memberNoEq(condition.memberNo())) | ||
.fetchOne(); | ||
|
||
return new PageImpl<>(reviews, pageable, totalCount); | ||
} | ||
|
||
private BooleanExpression productNoEq(Long productNo) { | ||
return productNo == null ? null : review.product.productNo.eq(productNo); | ||
} | ||
|
||
private BooleanExpression memberNoEq(Long memberNo) { | ||
return memberNo == null ? null : review.member.memberNo.eq(memberNo); | ||
} | ||
|
||
private static OrderSpecifier<? extends Number> sort(String sortBy) { | ||
if ("topRated".equals(sortBy)) { | ||
return review.starRating.desc(); | ||
} | ||
|
||
if ("lowRated".equals(sortBy)) { | ||
return review.starRating.asc(); | ||
} | ||
|
||
return review.reviewNo.desc(); | ||
} | ||
} |
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
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
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
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
15 changes: 15 additions & 0 deletions
15
module-domain/src/main/java/com/kernel360/config/QuerydslConfig.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,15 @@ | ||
package com.kernel360.config; | ||
|
||
import com.querydsl.jpa.impl.JPAQueryFactory; | ||
import jakarta.persistence.EntityManager; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
public class QuerydslConfig { | ||
|
||
@Bean | ||
public JPAQueryFactory queryFactory(EntityManager entityManager) { | ||
return new JPAQueryFactory(entityManager); | ||
} | ||
} |
12 changes: 0 additions & 12 deletions
12
module-domain/src/main/java/com/kernel360/review/repository/ReviewRepository.java
This file was deleted.
Oops, something went wrong.
8 changes: 8 additions & 0 deletions
8
module-domain/src/main/java/com/kernel360/review/repository/ReviewRepositoryJpa.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,8 @@ | ||
package com.kernel360.review.repository; | ||
|
||
import com.kernel360.review.entity.Review; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface ReviewRepositoryJpa extends JpaRepository<Review, Long> { | ||
Review findByReviewNo(Long reviewNo); | ||
} |