Skip to content

Commit

Permalink
#231 Refactor: 와인 리뷰 전체 조회 시, 와인 리뷰 조회 메서드와 좋아요 조회 메서드 분리
Browse files Browse the repository at this point in the history
  • Loading branch information
jeongyeon0208 committed Dec 18, 2024
1 parent da23806 commit 0c86e1b
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import java.util.List;


public interface WineWishlistRepository extends JpaRepository<WineWishlist, Long> {
public interface WineWishlistRepository extends JpaRepository<WineWishlist, Long>, WineWishlistRepositoryCustom {

List<WineWishlist> findAllByMember(Member member);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.drinkeg.drinkeg.repository;

public interface WineWishlistRepositoryCustom {

Boolean existsByMemberIdAndWineId(Long memberId, Long wineId);
}
Original file line number Diff line number Diff line change
@@ -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<Boolean> 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);
}
}
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -13,6 +11,4 @@ public interface WineWishlistService {
List<WineWishlistResponseDTO> getAllWineWishlistByMember(String username);

void deleteWineWishlistById(Long wineWishlistId, String username);

public boolean isLiked(Member member, Wine wine);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public ApiResponse<WineReviewResponseDTO> showWineReview(@AuthenticationPrincipa

WineReviewResponseDTO wineReviewResponseDTO = wineService.getWineReviewsByWineId(wineId, principalDetail, orderByLatest);


return ApiResponse.onSuccess(wineReviewResponseDTO);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ public class WineReviewResponseDTO {
@QueryProjection
public WineReviewResponseDTO(List<WineReviewDTO> wineReviews, boolean isLiked) {
this.wineReviews = wineReviews;
this.isLiked = isLiked;
}

public static WineReviewResponseDTO create(List<WineReviewDTO> wineReviews, boolean isLiked){
Expand All @@ -28,4 +27,9 @@ public static WineReviewResponseDTO create(List<WineReviewDTO> wineReviews, bool
.isLiked(isLiked)
.build();
}

// 좋아요 여부 업데이트
public void setLiked(boolean isLiked) {
this.isLiked = isLiked;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
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;

public interface WineRepositoryCustom {
List<SearchWineResponseDTO> findWinesWithLikeStatus(String searchName, Long memberId);

WineResponseWithThreeReviewsDTO findWineResponseByWineId(Long wineId, Long memberID);
WineReviewResponseDTO findWineReviewsAndLikeStatusByWineIdAndMemberId(Long wineId, Long memberId, boolean orderByLatest);
List<WineReviewDTO> findWineReviewsByWineIdAndMemberId(Long wineId, boolean orderByLatest);

List<RecommendWineDTO> findRecommendWines(Member member);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Boolean> 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<WineReviewDTO> findWineReviewsByWineIdAndMemberId(Long wineId, boolean orderByLatest) {

Optional<List<WineReviewDTO>> recentReviews = Optional.ofNullable(queryFactory
.select(new QWineReviewDTO(
Expand All @@ -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);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
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;
import com.drinkeg.drinkeg.wine.dto.response.SearchWineResponseDTO;
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;
Expand All @@ -31,6 +33,7 @@ public class WineServiceImpl implements WineService {
private final MemberRepository memberRepository;

private final S3Service s3Service;
private final WineWishlistRepository wineWishlistRepository;

@Override
public List<SearchWineResponseDTO> searchWinesByName(String searchName, PrincipalDetail principalDetail) {
Expand Down Expand Up @@ -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<WineReviewDTO> wineReviews = wineRepository.findWineReviewsByWineIdAndMemberId(wineId, orderByLatest);
boolean liked = wineWishlistRepository.existsByMemberIdAndWineId(member.getId(), wineId);

return WineReviewResponseDTO.create(wineReviews, liked);
}

@Override
Expand Down

0 comments on commit 0c86e1b

Please sign in to comment.