diff --git a/src/main/java/com/drinkeg/drinkeg/repository/WineWishlistRepository.java b/src/main/java/com/drinkeg/drinkeg/repository/WineWishlistRepository.java index 03a8658..7448f25 100644 --- a/src/main/java/com/drinkeg/drinkeg/repository/WineWishlistRepository.java +++ b/src/main/java/com/drinkeg/drinkeg/repository/WineWishlistRepository.java @@ -8,7 +8,7 @@ import java.util.List; -public interface WineWishlistRepository extends JpaRepository { +public interface WineWishlistRepository extends JpaRepository, WineWishlistRepositoryCustom { List findAllByMember(Member member); diff --git a/src/main/java/com/drinkeg/drinkeg/repository/WineWishlistRepositoryCustom.java b/src/main/java/com/drinkeg/drinkeg/repository/WineWishlistRepositoryCustom.java new file mode 100644 index 0000000..8053ce2 --- /dev/null +++ b/src/main/java/com/drinkeg/drinkeg/repository/WineWishlistRepositoryCustom.java @@ -0,0 +1,6 @@ +package com.drinkeg.drinkeg.repository; + +public interface WineWishlistRepositoryCustom { + + Boolean existsByMemberIdAndWineId(Long memberId, Long wineId); +} diff --git a/src/main/java/com/drinkeg/drinkeg/repository/WineWishlistRepositoryImpl.java b/src/main/java/com/drinkeg/drinkeg/repository/WineWishlistRepositoryImpl.java new file mode 100644 index 0000000..583a6aa --- /dev/null +++ b/src/main/java/com/drinkeg/drinkeg/repository/WineWishlistRepositoryImpl.java @@ -0,0 +1,33 @@ +package com.drinkeg.drinkeg.repository; + +import com.querydsl.jpa.impl.JPAQueryFactory; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Repository; +import org.springframework.transaction.annotation.Transactional; + +import java.util.Optional; + +import static com.drinkeg.drinkeg.domain.QWineWishlist.wineWishlist; + +@Repository +@RequiredArgsConstructor +@Transactional +public class WineWishlistRepositoryImpl implements WineWishlistRepositoryCustom { + + private final JPAQueryFactory queryFactory; + + + @Override + public Boolean existsByMemberIdAndWineId(Long memberId, Long wineId) { + // wineWishlist에 해당하는 데이터가 존재하면 true, 존재하지 않으면 false + Optional isLiked = Optional.ofNullable( + queryFactory + .select(wineWishlist.id.isNotNull()) // wineWishlist가 존재하는지 여부를 체크 + .from(wineWishlist) + .where(wineWishlist.wine.id.eq(wineId).and(wineWishlist.member.id.eq(memberId))) + .fetchOne() + ); + + return isLiked.orElse(false); + } +} diff --git a/src/main/java/com/drinkeg/drinkeg/service/wineWishlistService/WineWishlistService.java b/src/main/java/com/drinkeg/drinkeg/service/wineWishlistService/WineWishlistService.java index 89437c6..d05401b 100644 --- a/src/main/java/com/drinkeg/drinkeg/service/wineWishlistService/WineWishlistService.java +++ b/src/main/java/com/drinkeg/drinkeg/service/wineWishlistService/WineWishlistService.java @@ -1,7 +1,5 @@ package com.drinkeg.drinkeg.service.wineWishlistService; -import com.drinkeg.drinkeg.domain.Member; -import com.drinkeg.drinkeg.wine.domain.Wine; import com.drinkeg.drinkeg.dto.WineWishlistDTO.request.WineWishlistRequestDTO; import com.drinkeg.drinkeg.dto.WineWishlistDTO.response.WineWishlistResponseDTO; @@ -13,6 +11,4 @@ public interface WineWishlistService { List getAllWineWishlistByMember(String username); void deleteWineWishlistById(Long wineWishlistId, String username); - - public boolean isLiked(Member member, Wine wine); } diff --git a/src/main/java/com/drinkeg/drinkeg/service/wineWishlistService/WineWishlistServiceImpl.java b/src/main/java/com/drinkeg/drinkeg/service/wineWishlistService/WineWishlistServiceImpl.java index b6b9d3b..eff9716 100644 --- a/src/main/java/com/drinkeg/drinkeg/service/wineWishlistService/WineWishlistServiceImpl.java +++ b/src/main/java/com/drinkeg/drinkeg/service/wineWishlistService/WineWishlistServiceImpl.java @@ -61,9 +61,4 @@ public void deleteWineWishlistById(Long wineWishlistId, String username) { wineWishlistRepository.deleteById(wineWishlistId); } - - @Override - public boolean isLiked(Member member, Wine wine) { - return wineWishlistRepository.existsByMemberAndWine(member, wine); - } } diff --git a/src/main/java/com/drinkeg/drinkeg/wine/controller/WineController.java b/src/main/java/com/drinkeg/drinkeg/wine/controller/WineController.java index 417cbec..def4934 100644 --- a/src/main/java/com/drinkeg/drinkeg/wine/controller/WineController.java +++ b/src/main/java/com/drinkeg/drinkeg/wine/controller/WineController.java @@ -52,6 +52,7 @@ public ApiResponse showWineReview(@AuthenticationPrincipa WineReviewResponseDTO wineReviewResponseDTO = wineService.getWineReviewsByWineId(wineId, principalDetail, orderByLatest); + return ApiResponse.onSuccess(wineReviewResponseDTO); } diff --git a/src/main/java/com/drinkeg/drinkeg/wine/dto/response/WineReviewResponseDTO.java b/src/main/java/com/drinkeg/drinkeg/wine/dto/response/WineReviewResponseDTO.java index a304977..3f87ac3 100644 --- a/src/main/java/com/drinkeg/drinkeg/wine/dto/response/WineReviewResponseDTO.java +++ b/src/main/java/com/drinkeg/drinkeg/wine/dto/response/WineReviewResponseDTO.java @@ -19,7 +19,6 @@ public class WineReviewResponseDTO { @QueryProjection public WineReviewResponseDTO(List wineReviews, boolean isLiked) { this.wineReviews = wineReviews; - this.isLiked = isLiked; } public static WineReviewResponseDTO create(List wineReviews, boolean isLiked){ @@ -28,4 +27,9 @@ public static WineReviewResponseDTO create(List wineReviews, bool .isLiked(isLiked) .build(); } + + // 좋아요 여부 업데이트 + public void setLiked(boolean isLiked) { + this.isLiked = isLiked; + } } diff --git a/src/main/java/com/drinkeg/drinkeg/wine/repository/WineRepositoryCustom.java b/src/main/java/com/drinkeg/drinkeg/wine/repository/WineRepositoryCustom.java index 1b2880e..06519e0 100644 --- a/src/main/java/com/drinkeg/drinkeg/wine/repository/WineRepositoryCustom.java +++ b/src/main/java/com/drinkeg/drinkeg/wine/repository/WineRepositoryCustom.java @@ -4,7 +4,7 @@ import com.drinkeg.drinkeg.dto.HomeDTO.RecommendWineDTO; import com.drinkeg.drinkeg.wine.dto.response.SearchWineResponseDTO; import com.drinkeg.drinkeg.wine.dto.response.WineResponseWithThreeReviewsDTO; -import com.drinkeg.drinkeg.wine.dto.response.WineReviewResponseDTO; +import com.drinkeg.drinkeg.wine.dto.response.WineReviewDTO; import java.util.List; @@ -12,7 +12,7 @@ public interface WineRepositoryCustom { List findWinesWithLikeStatus(String searchName, Long memberId); WineResponseWithThreeReviewsDTO findWineResponseByWineId(Long wineId, Long memberID); - WineReviewResponseDTO findWineReviewsAndLikeStatusByWineIdAndMemberId(Long wineId, Long memberId, boolean orderByLatest); + List findWineReviewsByWineIdAndMemberId(Long wineId, boolean orderByLatest); List findRecommendWines(Member member); } diff --git a/src/main/java/com/drinkeg/drinkeg/wine/repository/WineRepositoryImpl.java b/src/main/java/com/drinkeg/drinkeg/wine/repository/WineRepositoryImpl.java index 932f28d..f514703 100644 --- a/src/main/java/com/drinkeg/drinkeg/wine/repository/WineRepositoryImpl.java +++ b/src/main/java/com/drinkeg/drinkeg/wine/repository/WineRepositoryImpl.java @@ -26,16 +26,7 @@ public class WineRepositoryImpl implements WineRepositoryCustom { private final JPAQueryFactory queryFactory; @Override - public WineReviewResponseDTO findWineReviewsAndLikeStatusByWineIdAndMemberId(Long wineId, Long memberId, boolean orderByLatest) { - - // wineWishlist에 해당하는 데이터가 존재하면 true, 존재하지 않으면 false - Optional isLiked = Optional.ofNullable( - queryFactory - .select(wineWishlist.id.isNotNull()) // wineWishlist가 존재하는지 여부를 체크 - .from(wineWishlist) - .where(wineWishlist.wine.id.eq(wineId).and(wineWishlist.member.id.eq(memberId))) - .fetchOne() - ); + public List findWineReviewsByWineIdAndMemberId(Long wineId, boolean orderByLatest) { Optional> recentReviews = Optional.ofNullable(queryFactory .select(new QWineReviewDTO( @@ -50,7 +41,7 @@ public WineReviewResponseDTO findWineReviewsAndLikeStatusByWineIdAndMemberId(Lon : tastingNote.satisfaction.desc()) // 최신순 정렬 .fetch()); - return WineReviewResponseDTO.create(recentReviews.orElse(null), isLiked.orElse(false)); + return recentReviews.orElse(null); } diff --git a/src/main/java/com/drinkeg/drinkeg/wine/service/WineServiceImpl.java b/src/main/java/com/drinkeg/drinkeg/wine/service/WineServiceImpl.java index fd678db..33288dd 100644 --- a/src/main/java/com/drinkeg/drinkeg/wine/service/WineServiceImpl.java +++ b/src/main/java/com/drinkeg/drinkeg/wine/service/WineServiceImpl.java @@ -4,6 +4,7 @@ import com.drinkeg.drinkeg.apipayLoad.code.status.ErrorStatus; import com.drinkeg.drinkeg.domain.Member; import com.drinkeg.drinkeg.repository.MemberRepository; +import com.drinkeg.drinkeg.repository.WineWishlistRepository; import com.drinkeg.drinkeg.wine.domain.Wine; import com.drinkeg.drinkeg.dto.HomeDTO.HomeResponseDTO; import com.drinkeg.drinkeg.dto.HomeDTO.RecommendWineDTO; @@ -11,6 +12,7 @@ import com.drinkeg.drinkeg.wine.dto.response.WineResponseWithThreeReviewsDTO; import com.drinkeg.drinkeg.dto.loginDTO.commonDTO.PrincipalDetail; import com.drinkeg.drinkeg.exception.GeneralException; +import com.drinkeg.drinkeg.wine.dto.response.WineReviewDTO; import com.drinkeg.drinkeg.wine.dto.response.WineReviewResponseDTO; import com.drinkeg.drinkeg.wine.repository.WineRepository; import lombok.RequiredArgsConstructor; @@ -31,6 +33,7 @@ public class WineServiceImpl implements WineService { private final MemberRepository memberRepository; private final S3Service s3Service; + private final WineWishlistRepository wineWishlistRepository; @Override public List searchWinesByName(String searchName, PrincipalDetail principalDetail) { @@ -65,7 +68,10 @@ public WineReviewResponseDTO getWineReviewsByWineId(Long wineId, PrincipalDetail Member member = memberRepository.findByUsername(principalDetail.getUsername()).orElseThrow( () -> new GeneralException(ErrorStatus.MEMBER_NOT_FOUND)); - return wineRepository.findWineReviewsAndLikeStatusByWineIdAndMemberId(wineId, member.getId(), orderByLatest); + List wineReviews = wineRepository.findWineReviewsByWineIdAndMemberId(wineId, orderByLatest); + boolean liked = wineWishlistRepository.existsByMemberIdAndWineId(member.getId(), wineId); + + return WineReviewResponseDTO.create(wineReviews, liked); } @Override