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

[BE] fix: 로그인된 회원 기준으로 좋아요 조회되게 수정 #293

Merged
merged 2 commits into from
Aug 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,20 @@ public static ReviewFavorite createReviewFavoriteByMemberAndReview(final Member
reviewFavorite.review.getReviewFavorites().add(reviewFavorite);
reviewFavorite.member.getReviewFavorites().add(reviewFavorite);
reviewFavorite.favorite = favorite;
reviewFavorite.review.addFavoriteCount();
return reviewFavorite;
}

public void updateChecked(final Boolean favorite) {
this.favorite = favorite;
if (favorite) {
if (!this.favorite && favorite) {
this.review.addFavoriteCount();
this.favorite = favorite;
return;
}
this.review.minusFavoriteCount();

if (this.favorite && !favorite) {
this.review.minusFavoriteCount();
this.favorite = favorite;
}
wugawuga marked this conversation as resolved.
Show resolved Hide resolved
}

public Long getId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,18 @@ private ReviewFavorite saveReviewFavorite(final Member member, final Review revi
return reviewFavoriteRepository.save(reviewFavorite);
}

public SortingReviewsResponse sortingReviews(final Long productId,
final Pageable pageable) {
public SortingReviewsResponse sortingReviews(final Long productId, final Pageable pageable, final Long memberId) {
final Member member = memberRepository.findById(memberId)
.orElseThrow(IllegalArgumentException::new);

final Product product = productRepository.findById(productId)
.orElseThrow(IllegalArgumentException::new);

final Page<Review> reviewPage = reviewRepository.findReviewsByProduct(pageable, product);

final SortingReviewsPageDto pageDto = SortingReviewsPageDto.toDto(reviewPage);
final List<SortingReviewDto> reviewDtos = reviewPage.stream()
.map(SortingReviewDto::toDto)
.map(review -> SortingReviewDto.toDto(review, member))
.collect(Collectors.toList());

return SortingReviewsResponse.toResponse(pageDto, reviewDtos);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public ResponseEntity<Void> toggleLikeReview(@PathVariable Long reviewId,
public ResponseEntity<SortingReviewsResponse> getSortingReviews(@AuthenticationPrincipal LoginInfo loginInfo,
@PathVariable Long productId,
@PageableDefault Pageable pageable) {
final SortingReviewsResponse response = reviewService.sortingReviews(productId, pageable);
final SortingReviewsResponse response = reviewService.sortingReviews(productId, pageable, loginInfo.getId());

return ResponseEntity.ok(response);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.funeat.review.presentation.dto;

import com.funeat.member.domain.Member;
import com.funeat.member.domain.favorite.ReviewFavorite;
import com.funeat.review.domain.Review;
import com.funeat.review.domain.ReviewTag;
Expand Down Expand Up @@ -39,7 +40,7 @@ public SortingReviewDto(final Long id, final String userName, final String profi
this.createdAt = createdAt;
}

public static SortingReviewDto toDto(final Review review) {
public static SortingReviewDto toDto(final Review review, final Member member) {
return new SortingReviewDto(
review.getId(),
review.getMember().getNickname(),
Expand All @@ -50,7 +51,7 @@ public static SortingReviewDto toDto(final Review review) {
review.getContent(),
review.getReBuy(),
review.getFavoriteCount(),
findReviewFavoriteChecked(review),
findReviewFavoriteChecked(review, member),
review.getCreatedAt()
);
}
Expand All @@ -62,11 +63,11 @@ private static List<TagDto> findTagDtos(final Review review) {
.collect(Collectors.toList());
}

private static boolean findReviewFavoriteChecked(final Review review) {
private static boolean findReviewFavoriteChecked(final Review review, final Member member) {
return review.getReviewFavorites()
.stream()
.filter(reviewFavorite -> reviewFavorite.getReview().equals(review))
.filter(reviewFavorite -> reviewFavorite.getMember().equals(review.getMember()))
.filter(reviewFavorite -> reviewFavorite.getMember().equals(member))
.findFirst()
.map(ReviewFavorite::getFavorite)
.orElse(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ class 좋아요_기준_내림차순으로_리뷰_목록_조회 {

// then
STATUS_CODE를_검증한다(response, 정상_처리);
정렬된_리뷰_목록_조회_결과를_검증한다(response, sortingReviews, pageDto);
정렬된_리뷰_목록_조회_결과를_검증한다(response, sortingReviews, pageDto, member1);
}

@Test
Expand Down Expand Up @@ -196,7 +196,7 @@ class 좋아요_기준_내림차순으로_리뷰_목록_조회 {

// then
STATUS_CODE를_검증한다(response, 정상_처리);
정렬된_리뷰_목록_조회_결과를_검증한다(response, sortingReviews, pageDto);
정렬된_리뷰_목록_조회_결과를_검증한다(response, sortingReviews, pageDto, member1);
}
}

Expand Down Expand Up @@ -233,7 +233,7 @@ class 평점_기준_오름차순으로_리뷰_목록을_조회 {

// then
STATUS_CODE를_검증한다(response, 정상_처리);
정렬된_리뷰_목록_조회_결과를_검증한다(response, sortingReviews, page);
정렬된_리뷰_목록_조회_결과를_검증한다(response, sortingReviews, page, member1);
}

@Test
Expand Down Expand Up @@ -268,7 +268,7 @@ class 평점_기준_오름차순으로_리뷰_목록을_조회 {

// then
STATUS_CODE를_검증한다(response, 정상_처리);
정렬된_리뷰_목록_조회_결과를_검증한다(response, sortingReviews, page);
정렬된_리뷰_목록_조회_결과를_검증한다(response, sortingReviews, page, member1);
}
}

Expand Down Expand Up @@ -305,7 +305,7 @@ class 최신순으로_리뷰_목록을_조회 {

// then
STATUS_CODE를_검증한다(response, 정상_처리);
정렬된_리뷰_목록_조회_결과를_검증한다(response, sortingReviews, page);
정렬된_리뷰_목록_조회_결과를_검증한다(response, sortingReviews, page, member1);
}
}

Expand Down Expand Up @@ -342,7 +342,7 @@ class 평점_기준_내림차순으로_리뷰_목록_조회 {

// then
STATUS_CODE를_검증한다(response, 정상_처리);
정렬된_리뷰_목록_조회_결과를_검증한다(response, sortingReviews, page);
정렬된_리뷰_목록_조회_결과를_검증한다(response, sortingReviews, page, member1);
}

@Test
Expand Down Expand Up @@ -377,7 +377,7 @@ class 평점_기준_내림차순으로_리뷰_목록_조회 {

// then
STATUS_CODE를_검증한다(response, 정상_처리);
정렬된_리뷰_목록_조회_결과를_검증한다(response, sortingReviews, page);
정렬된_리뷰_목록_조회_결과를_검증한다(response, sortingReviews, page, member1);
}
}

Expand Down Expand Up @@ -458,32 +458,29 @@ class 평점_기준_내림차순으로_리뷰_목록_조회 {
.extract();
}

private void 정렬된_리뷰_목록_조회_결과를_검증한다(final ExtractableResponse<Response> response,
final List<Review> reviews,
final SortingReviewsPageDto pageDto) {
private void 정렬된_리뷰_목록_조회_결과를_검증한다(final ExtractableResponse<Response> response, final List<Review> reviews,
final SortingReviewsPageDto pageDto, final Member member) {
페이지를_검증한다(response, pageDto);
리뷰_목록을_검증한다(response, reviews);
리뷰_목록을_검증한다(response, reviews, member);
}

private void 페이지를_검증한다(final ExtractableResponse<Response> response,
final SortingReviewsPageDto expected) {
private void 페이지를_검증한다(final ExtractableResponse<Response> response, final SortingReviewsPageDto expected) {
final var actual = response.jsonPath().getObject("page", SortingReviewsPageDto.class);
assertThat(actual).usingRecursiveComparison().isEqualTo(expected);
}

private void 리뷰_목록을_검증한다(final ExtractableResponse<Response> response,
final List<Review> reviews) {
private void 리뷰_목록을_검증한다(final ExtractableResponse<Response> response, final List<Review> reviews,
final Member member) {
final List<SortingReviewDto> expected = reviews.stream()
.map(SortingReviewDto::toDto)
.map(review -> SortingReviewDto.toDto(review, member))
.collect(Collectors.toList());
final List<SortingReviewDto> actual = response.jsonPath().getList("reviews", SortingReviewDto.class);
assertThat(actual).usingRecursiveComparison()
.ignoringFields("id")
.isEqualTo(expected);
}

private void 리뷰_랭킹_조회_결과를_검증한다(final ExtractableResponse<Response> response,
final List<Review> reviews) {
private void 리뷰_랭킹_조회_결과를_검증한다(final ExtractableResponse<Response> response, final List<Review> reviews) {
final var expected = reviews.stream()
.map(RankingReviewDto::toDto)
.collect(Collectors.toList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import com.funeat.common.DataClearExtension;
import com.funeat.member.domain.Member;
import com.funeat.member.domain.favorite.ReviewFavorite;
import com.funeat.member.persistence.MemberRepository;
import com.funeat.member.persistence.ReviewFavoriteRepository;
import com.funeat.product.domain.Product;
Expand Down Expand Up @@ -120,12 +119,8 @@ void init() {
final var reviewResult = reviewRepository.findAll().get(0);

// then
final var expected = ReviewFavorite.createReviewFavoriteByMemberAndReview(member, savedReview, true);
assertThat(reviewResult.getFavoriteCount()).isEqualTo(1L);
assertThat(reviewFavoriteResult).usingRecursiveComparison()
.ignoringExpectedNullFields()
.comparingOnlyFields("member", "review", "checked")
.isEqualTo(expected);
assertThat(reviewFavoriteResult.getFavorite()).isTrue();
}

@Test
Expand Down Expand Up @@ -154,12 +149,8 @@ void init() {
final var reviewResult = reviewRepository.findAll().get(0);

// then
final var expected = ReviewFavorite.createReviewFavoriteByMemberAndReview(member, savedReview, false);
assertThat(reviewResult.getFavoriteCount()).isEqualTo(0L);
assertThat(reviewFavoriteResult).usingRecursiveComparison()
.ignoringExpectedNullFields()
.comparingOnlyFields("member", "review", "checked")
.isEqualTo(expected);
assertThat(reviewFavoriteResult.getFavorite()).isFalse();
}

private MockMultipartFile 리뷰_페이크_사진_요청() {
Expand Down Expand Up @@ -204,11 +195,11 @@ class sortingReviews_페이징_테스트 {

final var pageable = PageRequest.of(0, 2, Sort.by("favoriteCount").descending());
final var expected = Stream.of(review1, review3)
.map(SortingReviewDto::toDto)
.map(review -> SortingReviewDto.toDto(review, member1))
.collect(Collectors.toList());

// when
final var actual = reviewService.sortingReviews(product.getId(), pageable)
final var actual = reviewService.sortingReviews(product.getId(), pageable, member1.getId())
.getReviews();

// then
Expand All @@ -235,11 +226,11 @@ class sortingReviews_페이징_테스트 {

final var pageable = PageRequest.of(0, 2, Sort.by("rating").ascending());
final var expected = Stream.of(review1, review3)
.map(SortingReviewDto::toDto)
.map(review -> SortingReviewDto.toDto(review, member1))
.collect(Collectors.toList());

// when
final var actual = reviewService.sortingReviews(product.getId(), pageable)
final var actual = reviewService.sortingReviews(product.getId(), pageable, member1.getId())
.getReviews();

// then
Expand All @@ -266,11 +257,11 @@ class sortingReviews_페이징_테스트 {

final var pageable = PageRequest.of(0, 2, Sort.by("rating").descending());
final var expected = Stream.of(review2, review3)
.map(SortingReviewDto::toDto)
.map(review -> SortingReviewDto.toDto(review, member1))
.collect(Collectors.toList());

// when
final var actual = reviewService.sortingReviews(product.getId(), pageable)
final var actual = reviewService.sortingReviews(product.getId(), pageable, member1.getId())
.getReviews();

// then
Expand All @@ -297,11 +288,11 @@ class sortingReviews_페이징_테스트 {

final var pageable = PageRequest.of(0, 2, Sort.by("createdAt").descending());
final var expected = Stream.of(review3, review2)
.map(SortingReviewDto::toDto)
.map(review -> SortingReviewDto.toDto(review, member1))
.collect(Collectors.toList());

// when
final var actual = reviewService.sortingReviews(product.getId(), pageable)
final var actual = reviewService.sortingReviews(product.getId(), pageable, member1.getId())
.getReviews();

// then
Expand Down
Loading