From 0c86e1ba3330cc77a16e52366b87eef13fe8512d Mon Sep 17 00:00:00 2001 From: jeongyeon0208 Date: Wed, 18 Dec 2024 14:31:13 +0900 Subject: [PATCH] =?UTF-8?q?#231=20Refactor:=20=EC=99=80=EC=9D=B8=20?= =?UTF-8?q?=EB=A6=AC=EB=B7=B0=20=EC=A0=84=EC=B2=B4=20=EC=A1=B0=ED=9A=8C=20?= =?UTF-8?q?=EC=8B=9C,=20=EC=99=80=EC=9D=B8=20=EB=A6=AC=EB=B7=B0=20?= =?UTF-8?q?=EC=A1=B0=ED=9A=8C=20=EB=A9=94=EC=84=9C=EB=93=9C=EC=99=80=20?= =?UTF-8?q?=EC=A2=8B=EC=95=84=EC=9A=94=20=EC=A1=B0=ED=9A=8C=20=EB=A9=94?= =?UTF-8?q?=EC=84=9C=EB=93=9C=20=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../repository/WineWishlistRepository.java | 2 +- .../WineWishlistRepositoryCustom.java | 6 ++++ .../WineWishlistRepositoryImpl.java | 33 +++++++++++++++++++ .../WineWishlistService.java | 4 --- .../WineWishlistServiceImpl.java | 5 --- .../wine/controller/WineController.java | 1 + .../dto/response/WineReviewResponseDTO.java | 6 +++- .../wine/repository/WineRepositoryCustom.java | 4 +-- .../wine/repository/WineRepositoryImpl.java | 13 ++------ .../drinkeg/wine/service/WineServiceImpl.java | 8 ++++- 10 files changed, 57 insertions(+), 25 deletions(-) create mode 100644 src/main/java/com/drinkeg/drinkeg/repository/WineWishlistRepositoryCustom.java create mode 100644 src/main/java/com/drinkeg/drinkeg/repository/WineWishlistRepositoryImpl.java diff --git a/src/main/java/com/drinkeg/drinkeg/repository/WineWishlistRepository.java b/src/main/java/com/drinkeg/drinkeg/repository/WineWishlistRepository.java index 03a86581..7448f251 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 00000000..8053ce2e --- /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 00000000..583a6aa4 --- /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 89437c6f..d05401b2 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 b6b9d3b5..eff97166 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 417cbec3..def49340 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 a304977a..3f87ac33 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 1b2880e8..06519e04 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 932f28d3..f514703e 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 fd678db4..33288dd2 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