-
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.
✨ Mission(#6) : Using QueryDSL for Mission-2
- Loading branch information
1 parent
503d772
commit 501b824
Showing
7 changed files
with
116 additions
and
0 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
7 changes: 7 additions & 0 deletions
7
src/main/java/umc/spring/repository/MemberRepository/MemberRepository.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,7 @@ | ||
package umc.spring.repository.MemberRepository; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import umc.spring.domain.Member; | ||
|
||
public interface MemberRepository extends JpaRepository<Member, Long> { | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/umc/spring/repository/ReviewRepository/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,7 @@ | ||
package umc.spring.repository.ReviewRepository; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import umc.spring.domain.Review; | ||
|
||
public interface ReviewRepository extends JpaRepository<Review, Long>, ReviewRepositoryCustom { | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/umc/spring/repository/ReviewRepository/ReviewRepositoryCustom.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,7 @@ | ||
package umc.spring.repository.ReviewRepository; | ||
|
||
import umc.spring.domain.Review; | ||
|
||
public interface ReviewRepositoryCustom { | ||
Review dynamicQueryCreateReview(Long memberId, Long storeId, String body, float score); | ||
} |
47 changes: 47 additions & 0 deletions
47
src/main/java/umc/spring/repository/ReviewRepository/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,47 @@ | ||
package umc.spring.repository.ReviewRepository; | ||
|
||
import com.querydsl.core.BooleanBuilder; | ||
import com.querydsl.jpa.impl.JPAQueryFactory; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Repository; | ||
import umc.spring.domain.QReview; | ||
import umc.spring.domain.Review; | ||
|
||
@Repository | ||
@RequiredArgsConstructor | ||
public class ReviewRepositoryImpl implements ReviewRepositoryCustom{ | ||
|
||
private final JPAQueryFactory jpaQueryFactory; | ||
private final QReview review = QReview.review; | ||
|
||
@Override | ||
public Review dynamicQueryCreateReview(Long memberId, Long storeId, String body, float score) { | ||
BooleanBuilder predicate = new BooleanBuilder(); | ||
|
||
|
||
if (memberId != null) { | ||
predicate.and(review.member.id.eq(memberId)); | ||
} | ||
|
||
|
||
if (storeId != null) { | ||
predicate.and(review.store.id.eq(storeId)); | ||
} | ||
|
||
|
||
if (body != null && !body.isEmpty()) { | ||
predicate.and(review.body.eq(body)); | ||
} | ||
|
||
|
||
if (score >= 0) { | ||
predicate.and(review.score.eq(score)); | ||
} | ||
|
||
|
||
return jpaQueryFactory | ||
.selectFrom(review) | ||
.where(predicate) | ||
.fetchOne(); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/umc/spring/service/ReviewService/ReviewQueryService.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,7 @@ | ||
package umc.spring.service.ReviewService; | ||
|
||
import umc.spring.domain.Review; | ||
|
||
public interface ReviewQueryService { | ||
Review createReview(Long memberId, Long storeId, String body, float score); | ||
} |
33 changes: 33 additions & 0 deletions
33
src/main/java/umc/spring/service/ReviewService/ReviewQueryServiceImpl.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,33 @@ | ||
package umc.spring.service.ReviewService; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import umc.spring.domain.Member; | ||
import umc.spring.domain.Review; | ||
import umc.spring.domain.Store; | ||
import umc.spring.repository.MemberRepository.MemberRepository; | ||
import umc.spring.repository.ReviewRepository.ReviewRepository; | ||
import umc.spring.repository.StoreRepository.StoreRepository; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Transactional(readOnly = true) | ||
public class ReviewQueryServiceImpl implements ReviewQueryService { | ||
|
||
private final ReviewRepository reviewRepository; | ||
private final MemberRepository memberRepository; | ||
private final StoreRepository storeRepository; | ||
|
||
@Override | ||
public Review createReview(Long memberId, Long storeId, String body, float score) { | ||
Member member = memberRepository.findById(memberId) | ||
.orElseThrow(() -> new IllegalArgumentException("Member not found with ID: " + memberId)); | ||
Store store = storeRepository.findById(storeId) | ||
.orElseThrow(() -> new IllegalArgumentException("Store not found with ID: " + storeId)); | ||
|
||
Review newReview = new Review(member, store, body, score); | ||
return reviewRepository.save(newReview); | ||
} | ||
|
||
} |