Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#231 Refactor: 와인 정보/리뷰 보기 수정, WineWishlist 리패키징 #232

Open
wants to merge 7 commits into
base: dev
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
import com.drinkeg.drinkeg.apipayLoad.ApiResponse;
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 com.drinkeg.drinkeg.dto.loginDTO.commonDTO.PrincipalDetail;
import com.drinkeg.drinkeg.wine.dto.response.WineReviewResponseDTO;
import com.drinkeg.drinkeg.wine.service.WineService;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -35,21 +36,23 @@ public ApiResponse<List<SearchWineResponseDTO>> searchWine(@AuthenticationPrinci
// 선택한 와인 정보 출력
@GetMapping("/{wineId}")
@Operation(summary = "선택 와인 정보 열람", description = "선택한 와인의 정보를 wineResponseDTO에 담아 반환")
public ApiResponse<WineResponseWithThreeReviewsDTO> showWine(@PathVariable("wineId") Long wineId) {
public ApiResponse<WineResponseWithThreeReviewsDTO> showWine(@AuthenticationPrincipal PrincipalDetail principalDetail,
@PathVariable("wineId") Long wineId) {

WineResponseWithThreeReviewsDTO wineResponseWithThreeReviewsDTO = wineService.getWineResponseByWineId(wineId);
WineResponseWithThreeReviewsDTO wineResponseWithThreeReviewsDTO = wineService.getWineResponseByWineId(wineId, principalDetail);

return ApiResponse.onSuccess(wineResponseWithThreeReviewsDTO);
}

// 와인 리뷰 보기
@GetMapping("/review/{wineId}")
@Operation(summary = "선택 와인 리뷰 열람", description = "선택한 와인 리뷰를 List로 반환")
public ApiResponse<List<WineReviewResponseDTO>> showWineReview(@PathVariable("wineId") Long wineId) {
public ApiResponse<WineReviewResponseDTO> showWineReview(@AuthenticationPrincipal PrincipalDetail principalDetail,
@PathVariable("wineId") Long wineId, @RequestParam Boolean orderByLatest) {

List<WineReviewResponseDTO> wineReviews = wineService.getWineReviewsByWineId(wineId);
WineReviewResponseDTO wineReviewResponseDTO = wineService.getWineReviewsByWineId(wineId, principalDetail, orderByLatest);

return ApiResponse.onSuccess(wineReviews);
return ApiResponse.onSuccess(wineReviewResponseDTO);
}

// 와인 이미지 업로드
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@ public class WineResponseDTO {

private float avgSatisfaction;

private boolean isLiked;


@QueryProjection
public WineResponseDTO(
Long wineId, String name, String imageUrl, int price, String sort, String area, float satisfaction,
float avgSugarContent, float avgAcidity, float avgTannin, float avgBody, float avgAlcohol,
WineNoteNose wineNoteNose, float avgSatisfaction) {
WineNoteNose wineNoteNose, float avgSatisfaction, boolean isLiked) {

this.wineId = wineId;
this.name = name;
Expand All @@ -54,6 +56,8 @@ public WineResponseDTO(

this.wineNoteNose = wineNoteNose;
this.avgSatisfaction = avgSatisfaction;

this.isLiked = isLiked;
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.drinkeg.drinkeg.wine.dto.response;

import com.querydsl.core.annotations.QueryProjection;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

Expand All @@ -11,10 +10,10 @@
@NoArgsConstructor
public class WineResponseWithThreeReviewsDTO {
WineResponseDTO wineResponseDTO;
List<WineReviewResponseDTO> recentReviews;
List<WineReviewDTO> recentReviews;

@QueryProjection
public WineResponseWithThreeReviewsDTO(WineResponseDTO wineResponseDTO, List<WineReviewResponseDTO> recentReviews) {
public WineResponseWithThreeReviewsDTO(WineResponseDTO wineResponseDTO, List<WineReviewDTO> recentReviews) {
this.wineResponseDTO = wineResponseDTO;
this.recentReviews = recentReviews;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.drinkeg.drinkeg.wine.dto.response;

import com.querydsl.core.annotations.QueryProjection;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;

@Getter
@NoArgsConstructor
public class WineReviewDTO {

// 회원이 설정한 이름
private String name;

private String review;

private float satisfaction;

private LocalDateTime createdAt;


@QueryProjection
public WineReviewDTO(String name, String review, float satisfaction, LocalDateTime createdAt) {
this.name = name;
this.review = review;
this.satisfaction = satisfaction;
this.createdAt = createdAt;

}

}
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
package com.drinkeg.drinkeg.wine.dto.response;

import com.drinkeg.drinkeg.tastingNote.dto.response.TastingNotePreviewResponseDTO;
import com.querydsl.core.annotations.QueryProjection;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.List;

@Getter
@NoArgsConstructor
@Builder
public class WineReviewResponseDTO {

// 회원이 설정한 이름
private String name;

private String review;

private float satisfaction;

List<WineReviewDTO> wineReviews;
boolean isLiked;

@QueryProjection
public WineReviewResponseDTO(String name, String review, float satisfaction) {
this.name = name;
this.review = review;
this.satisfaction = satisfaction;
public WineReviewResponseDTO(List<WineReviewDTO> wineReviews, boolean isLiked) {
this.wineReviews = wineReviews;
this.isLiked = isLiked;
}

public static WineReviewResponseDTO create(List<WineReviewDTO> wineReviews, boolean isLiked){
return WineReviewResponseDTO.builder()
.wineReviews(wineReviews)
.isLiked(isLiked)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
import java.util.List;

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

WineResponseWithThreeReviewsDTO findWineResponseByWineId(Long wineId, Long memberID);
WineReviewResponseDTO findWineReviewsAndLikeStatusByWineIdAndMemberId(Long wineId, Long memberId, boolean orderByLatest);

WineResponseWithThreeReviewsDTO findWineResponseByWineId(Long wineId);
List<RecommendWineDTO> findRecommendWines(Member member);
List<SearchWineResponseDTO> findWinesWithLikeStatus(String searchName, Long memberId);
}
jeongyeon0208 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.drinkeg.drinkeg.dto.HomeDTO.RecommendWineDTO;
import com.drinkeg.drinkeg.wine.dto.response.*;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.types.Projections;
import com.querydsl.core.types.dsl.CaseBuilder;
import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.RequiredArgsConstructor;
Expand All @@ -27,23 +26,37 @@ public class WineRepositoryImpl implements WineRepositoryCustom {
private final JPAQueryFactory queryFactory;

@Override
public List<WineReviewResponseDTO> findWineReviewsById(Long wineId) {
return queryFactory
.select(new QWineReviewResponseDTO(
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()
);

Optional<List<WineReviewDTO>> recentReviews = Optional.ofNullable(queryFactory
.select(new QWineReviewDTO(
tastingNote.member.name,
tastingNote.review,
tastingNote.satisfaction
tastingNote.satisfaction,
tastingNote.createdAt
))
.from(tastingNote)
.join(tastingNote.wine, wine)
.where(wine.id.eq(wineId))
.fetch();
.where(tastingNote.wine.id.eq(wineId))
.orderBy(orderByLatest? tastingNote.createdAt.desc()
: tastingNote.satisfaction.desc()) // 최신순 정렬
.fetch());

return WineReviewResponseDTO.create(recentReviews.orElse(null), isLiked.orElse(false));
}



@Override
public WineResponseWithThreeReviewsDTO findWineResponseByWineId(Long wineId) {
public WineResponseWithThreeReviewsDTO findWineResponseByWineId(Long wineId, Long memberId) {

// Wine 데이터를 가져옴
WineResponseDTO wineResponseDTO = queryFactory
Expand All @@ -64,19 +77,23 @@ public WineResponseWithThreeReviewsDTO findWineResponseByWineId(Long wineId) {

wine.wineNote.wineNoteNose,

wine.wineNote.avgSatisfaction.as("avgSatisfaction")
wine.wineNote.avgSatisfaction.as("avgSatisfaction"),
wineWishlist.id.isNotNull().as("isLiked") // memberId와 wineId에 따라 isLiked 여부
))
.from(wine)
.leftJoin(wine.wineNote)
.leftJoin(wineWishlist)
.on(wineWishlist.wine.eq(wine).and(wineWishlist.member.id.eq(memberId)))
.where(wine.id.eq(wineId))
.fetchOne();

// 최근 생성된 3개의 TastingNote
Optional<List<WineReviewResponseDTO>> recentReviews = Optional.ofNullable(queryFactory
.select(new QWineReviewResponseDTO(
Optional<List<WineReviewDTO>> recentReviews = Optional.ofNullable(queryFactory
.select(new QWineReviewDTO(
tastingNote.member.name,
tastingNote.review,
tastingNote.satisfaction
tastingNote.satisfaction,
tastingNote.createdAt
))
.from(tastingNote)
.where(tastingNote.wine.id.eq(wineId))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package com.drinkeg.drinkeg.wine.service;

import com.drinkeg.drinkeg.domain.Member;
import com.drinkeg.drinkeg.wine.domain.Wine;
import com.drinkeg.drinkeg.dto.HomeDTO.HomeResponseDTO;
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.*;
import com.drinkeg.drinkeg.dto.loginDTO.commonDTO.PrincipalDetail;

import java.io.IOException;
Expand All @@ -17,9 +14,9 @@ public interface WineService {

public Wine findWineById(Long wineId);

public WineResponseWithThreeReviewsDTO getWineResponseByWineId(Long wineId);
public WineResponseWithThreeReviewsDTO getWineResponseByWineId(Long wineId, PrincipalDetail principalDetail);

public List<WineReviewResponseDTO> getWineReviewsByWineId(Long wineId);
public WineReviewResponseDTO getWineReviewsByWineId(Long wineId, PrincipalDetail principalDetail, boolean orderByLatest);

public HomeResponseDTO getHomeResponse(PrincipalDetail principalDetail);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
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.dto.loginDTO.commonDTO.PrincipalDetail;
import com.drinkeg.drinkeg.exception.GeneralException;
import com.drinkeg.drinkeg.wine.dto.response.WineReviewResponseDTO;
import com.drinkeg.drinkeg.wine.repository.WineRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -51,13 +51,21 @@ public Wine findWineById(Long wineId) {
}

@Override
public WineResponseWithThreeReviewsDTO getWineResponseByWineId(Long wineId){
return wineRepository.findWineResponseByWineId(wineId);
public WineResponseWithThreeReviewsDTO getWineResponseByWineId(Long wineId, PrincipalDetail principalDetail){
// 회원을 조회한다.
Member member = memberRepository.findByUsername(principalDetail.getUsername()).orElseThrow(
() -> new GeneralException(ErrorStatus.MEMBER_NOT_FOUND));

return wineRepository.findWineResponseByWineId(wineId, member.getId());
}

@Override
public List<WineReviewResponseDTO> getWineReviewsByWineId(Long wineId){
return wineRepository.findWineReviewsById(wineId);
public WineReviewResponseDTO getWineReviewsByWineId(Long wineId, PrincipalDetail principalDetail, boolean orderByLatest){
// 회원을 조회한다.
Member member = memberRepository.findByUsername(principalDetail.getUsername()).orElseThrow(
() -> new GeneralException(ErrorStatus.MEMBER_NOT_FOUND));

return wineRepository.findWineReviewsAndLikeStatusByWineIdAndMemberId(wineId, member.getId(), orderByLatest);
}

@Override
Expand Down