Skip to content

Commit

Permalink
✨ Mission(#6) : Using QueryDSL for Mission-2
Browse files Browse the repository at this point in the history
  • Loading branch information
Jangwon0319 committed Nov 7, 2024
1 parent 503d772 commit 501b824
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/main/java/umc/spring/domain/Review.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,12 @@ public class Review extends BaseEntity {

@OneToMany(mappedBy = "review", cascade = CascadeType.ALL)
private List<ReviewImage> reviewImageList = new ArrayList<>();

public Review(Member member, Store store, String body, float score) {
this.member = member;
this.store = store;
this.body = body;
this.score = score;
}

}
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> {
}
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 {
}
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);
}
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();
}
}
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);
}
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);
}

}

0 comments on commit 501b824

Please sign in to comment.