From c8e9c6d26ba135b85de9ffb33b64987dbf39e571 Mon Sep 17 00:00:00 2001 From: wugawuga Date: Thu, 3 Aug 2023 13:38:07 +0900 Subject: [PATCH 1/2] =?UTF-8?q?fix:=20=EB=A6=AC=EB=B7=B0=20=EC=A2=8B?= =?UTF-8?q?=EC=95=84=EC=9A=94=20=EC=B2=AB=20=EC=83=9D=EC=84=B1=EC=8B=9C=20?= =?UTF-8?q?=EC=B9=B4=EC=9A=B4=ED=8A=B8=20+1,=20=EB=B6=84=EA=B8=B0=EC=B2=98?= =?UTF-8?q?=EB=A6=AC=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../member/domain/favorite/ReviewFavorite.java | 11 +++++++---- .../review/application/ReviewServiceTest.java | 13 ++----------- 2 files changed, 9 insertions(+), 15 deletions(-) diff --git a/backend/src/main/java/com/funeat/member/domain/favorite/ReviewFavorite.java b/backend/src/main/java/com/funeat/member/domain/favorite/ReviewFavorite.java index 4d703a32b..2bf37ec1d 100644 --- a/backend/src/main/java/com/funeat/member/domain/favorite/ReviewFavorite.java +++ b/backend/src/main/java/com/funeat/member/domain/favorite/ReviewFavorite.java @@ -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; + } } public Long getId() { diff --git a/backend/src/test/java/com/funeat/review/application/ReviewServiceTest.java b/backend/src/test/java/com/funeat/review/application/ReviewServiceTest.java index 100280d35..6c2de658f 100644 --- a/backend/src/test/java/com/funeat/review/application/ReviewServiceTest.java +++ b/backend/src/test/java/com/funeat/review/application/ReviewServiceTest.java @@ -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; @@ -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 @@ -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 리뷰_페이크_사진_요청() { From 0f16e8bc3e8836669a48c3e7ddcc3e0887f46333 Mon Sep 17 00:00:00 2001 From: wugawuga Date: Thu, 3 Aug 2023 13:51:16 +0900 Subject: [PATCH 2/2] =?UTF-8?q?fix:=20=ED=95=B4=EB=8B=B9=20=EB=A1=9C?= =?UTF-8?q?=EA=B7=B8=EC=9D=B8=20=EB=90=9C=20=ED=9A=8C=EC=9B=90=20=EA=B8=B0?= =?UTF-8?q?=EC=A4=80=EC=9C=BC=EB=A1=9C=20=EB=A6=AC=EB=B7=B0=20=EC=A2=8B?= =?UTF-8?q?=EC=95=84=EC=9A=94=20=EC=A1=B0=ED=9A=8C=EB=90=98=EA=B2=8C=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../review/application/ReviewService.java | 8 +++-- .../presentation/ReviewApiController.java | 2 +- .../presentation/dto/SortingReviewDto.java | 9 ++--- .../review/ReviewAcceptanceTest.java | 33 +++++++++---------- .../review/application/ReviewServiceTest.java | 16 ++++----- 5 files changed, 34 insertions(+), 34 deletions(-) diff --git a/backend/src/main/java/com/funeat/review/application/ReviewService.java b/backend/src/main/java/com/funeat/review/application/ReviewService.java index aad5de8b5..0fb9e2472 100644 --- a/backend/src/main/java/com/funeat/review/application/ReviewService.java +++ b/backend/src/main/java/com/funeat/review/application/ReviewService.java @@ -106,8 +106,10 @@ 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); @@ -115,7 +117,7 @@ public SortingReviewsResponse sortingReviews(final Long productId, final SortingReviewsPageDto pageDto = SortingReviewsPageDto.toDto(reviewPage); final List reviewDtos = reviewPage.stream() - .map(SortingReviewDto::toDto) + .map(review -> SortingReviewDto.toDto(review, member)) .collect(Collectors.toList()); return SortingReviewsResponse.toResponse(pageDto, reviewDtos); diff --git a/backend/src/main/java/com/funeat/review/presentation/ReviewApiController.java b/backend/src/main/java/com/funeat/review/presentation/ReviewApiController.java index 6c0329957..d50526079 100644 --- a/backend/src/main/java/com/funeat/review/presentation/ReviewApiController.java +++ b/backend/src/main/java/com/funeat/review/presentation/ReviewApiController.java @@ -55,7 +55,7 @@ public ResponseEntity toggleLikeReview(@PathVariable Long reviewId, public ResponseEntity 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); } diff --git a/backend/src/main/java/com/funeat/review/presentation/dto/SortingReviewDto.java b/backend/src/main/java/com/funeat/review/presentation/dto/SortingReviewDto.java index 8280591fa..60e06c41b 100644 --- a/backend/src/main/java/com/funeat/review/presentation/dto/SortingReviewDto.java +++ b/backend/src/main/java/com/funeat/review/presentation/dto/SortingReviewDto.java @@ -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; @@ -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(), @@ -50,7 +51,7 @@ public static SortingReviewDto toDto(final Review review) { review.getContent(), review.getReBuy(), review.getFavoriteCount(), - findReviewFavoriteChecked(review), + findReviewFavoriteChecked(review, member), review.getCreatedAt() ); } @@ -62,11 +63,11 @@ private static List 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); diff --git a/backend/src/test/java/com/funeat/acceptance/review/ReviewAcceptanceTest.java b/backend/src/test/java/com/funeat/acceptance/review/ReviewAcceptanceTest.java index a69b02c1a..dcd0aab0f 100644 --- a/backend/src/test/java/com/funeat/acceptance/review/ReviewAcceptanceTest.java +++ b/backend/src/test/java/com/funeat/acceptance/review/ReviewAcceptanceTest.java @@ -161,7 +161,7 @@ class 좋아요_기준_내림차순으로_리뷰_목록_조회 { // then STATUS_CODE를_검증한다(response, 정상_처리); - 정렬된_리뷰_목록_조회_결과를_검증한다(response, sortingReviews, pageDto); + 정렬된_리뷰_목록_조회_결과를_검증한다(response, sortingReviews, pageDto, member1); } @Test @@ -196,7 +196,7 @@ class 좋아요_기준_내림차순으로_리뷰_목록_조회 { // then STATUS_CODE를_검증한다(response, 정상_처리); - 정렬된_리뷰_목록_조회_결과를_검증한다(response, sortingReviews, pageDto); + 정렬된_리뷰_목록_조회_결과를_검증한다(response, sortingReviews, pageDto, member1); } } @@ -233,7 +233,7 @@ class 평점_기준_오름차순으로_리뷰_목록을_조회 { // then STATUS_CODE를_검증한다(response, 정상_처리); - 정렬된_리뷰_목록_조회_결과를_검증한다(response, sortingReviews, page); + 정렬된_리뷰_목록_조회_결과를_검증한다(response, sortingReviews, page, member1); } @Test @@ -268,7 +268,7 @@ class 평점_기준_오름차순으로_리뷰_목록을_조회 { // then STATUS_CODE를_검증한다(response, 정상_처리); - 정렬된_리뷰_목록_조회_결과를_검증한다(response, sortingReviews, page); + 정렬된_리뷰_목록_조회_결과를_검증한다(response, sortingReviews, page, member1); } } @@ -305,7 +305,7 @@ class 최신순으로_리뷰_목록을_조회 { // then STATUS_CODE를_검증한다(response, 정상_처리); - 정렬된_리뷰_목록_조회_결과를_검증한다(response, sortingReviews, page); + 정렬된_리뷰_목록_조회_결과를_검증한다(response, sortingReviews, page, member1); } } @@ -342,7 +342,7 @@ class 평점_기준_내림차순으로_리뷰_목록_조회 { // then STATUS_CODE를_검증한다(response, 정상_처리); - 정렬된_리뷰_목록_조회_결과를_검증한다(response, sortingReviews, page); + 정렬된_리뷰_목록_조회_결과를_검증한다(response, sortingReviews, page, member1); } @Test @@ -377,7 +377,7 @@ class 평점_기준_내림차순으로_리뷰_목록_조회 { // then STATUS_CODE를_검증한다(response, 정상_처리); - 정렬된_리뷰_목록_조회_결과를_검증한다(response, sortingReviews, page); + 정렬된_리뷰_목록_조회_결과를_검증한다(response, sortingReviews, page, member1); } } @@ -458,23 +458,21 @@ class 평점_기준_내림차순으로_리뷰_목록_조회 { .extract(); } - private void 정렬된_리뷰_목록_조회_결과를_검증한다(final ExtractableResponse response, - final List reviews, - final SortingReviewsPageDto pageDto) { + private void 정렬된_리뷰_목록_조회_결과를_검증한다(final ExtractableResponse response, final List reviews, + final SortingReviewsPageDto pageDto, final Member member) { 페이지를_검증한다(response, pageDto); - 리뷰_목록을_검증한다(response, reviews); + 리뷰_목록을_검증한다(response, reviews, member); } - private void 페이지를_검증한다(final ExtractableResponse response, - final SortingReviewsPageDto expected) { + private void 페이지를_검증한다(final ExtractableResponse response, final SortingReviewsPageDto expected) { final var actual = response.jsonPath().getObject("page", SortingReviewsPageDto.class); assertThat(actual).usingRecursiveComparison().isEqualTo(expected); } - private void 리뷰_목록을_검증한다(final ExtractableResponse response, - final List reviews) { + private void 리뷰_목록을_검증한다(final ExtractableResponse response, final List reviews, + final Member member) { final List expected = reviews.stream() - .map(SortingReviewDto::toDto) + .map(review -> SortingReviewDto.toDto(review, member)) .collect(Collectors.toList()); final List actual = response.jsonPath().getList("reviews", SortingReviewDto.class); assertThat(actual).usingRecursiveComparison() @@ -482,8 +480,7 @@ class 평점_기준_내림차순으로_리뷰_목록_조회 { .isEqualTo(expected); } - private void 리뷰_랭킹_조회_결과를_검증한다(final ExtractableResponse response, - final List reviews) { + private void 리뷰_랭킹_조회_결과를_검증한다(final ExtractableResponse response, final List reviews) { final var expected = reviews.stream() .map(RankingReviewDto::toDto) .collect(Collectors.toList()); diff --git a/backend/src/test/java/com/funeat/review/application/ReviewServiceTest.java b/backend/src/test/java/com/funeat/review/application/ReviewServiceTest.java index 6c2de658f..28cde9a21 100644 --- a/backend/src/test/java/com/funeat/review/application/ReviewServiceTest.java +++ b/backend/src/test/java/com/funeat/review/application/ReviewServiceTest.java @@ -195,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 @@ -226,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 @@ -257,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 @@ -288,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