Skip to content

Commit

Permalink
#231 Refactor: 리뷰, 좋아요 없을 때 null 처리
Browse files Browse the repository at this point in the history
  • Loading branch information
jeongyeon0208 committed Dec 15, 2024
1 parent 22bef0d commit dfef40d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;

@Getter
@NoArgsConstructor
public class WineReviewDTO {
Expand All @@ -15,12 +17,16 @@ public class WineReviewDTO {

private float satisfaction;

private LocalDateTime createdAt;


@QueryProjection
public WineReviewDTO(String name, String review, float satisfaction) {
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
Expand Up @@ -28,26 +28,28 @@ public class WineRepositoryImpl implements WineRepositoryCustom {
@Override
public WineReviewResponseDTO findWineReviewsAndLikeStatusByWineIdAndMemberId(Long wineId, Long memberId) {

Boolean isLiked = queryFactory
.select(wineWishlist.id.isNotNull().or(wineWishlist.id.isNull())) // memberId와 wineId에 따라 isLiked 여부
.from(wineWishlist)
.where(wineWishlist.wine.id.eq(wineId).and(wineWishlist.member.id.eq(memberId)))
.fetchOne();

// 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()
);

// 최근 생성된 3개의 TastingNote
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))
.orderBy(tastingNote.createdAt.desc()) // 최신순 정렬
.fetch());

return WineReviewResponseDTO.create(recentReviews.get(), Boolean.TRUE.equals(isLiked));
return WineReviewResponseDTO.create(recentReviews.orElse(null), isLiked.orElse(false));
}


Expand Down Expand Up @@ -89,7 +91,8 @@ public WineResponseWithThreeReviewsDTO findWineResponseByWineId(Long wineId, Lon
.select(new QWineReviewDTO(
tastingNote.member.name,
tastingNote.review,
tastingNote.satisfaction
tastingNote.satisfaction,
tastingNote.createdAt
))
.from(tastingNote)
.where(tastingNote.wine.id.eq(wineId))
Expand Down

0 comments on commit dfef40d

Please sign in to comment.