Skip to content

Commit

Permalink
refactor: 상품 랭킹 로직 수정 (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
Go-Jaecheol authored Feb 19, 2024
1 parent 9f2b410 commit 4ca009e
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import com.funeat.review.persistence.ReviewRepository;
import com.funeat.review.persistence.ReviewTagRepository;
import com.funeat.tag.domain.Tag;
import java.time.LocalDateTime;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -119,9 +118,7 @@ public ProductResponse findProductDetail(final Long productId) {
}

public RankingProductsResponse getTop3Products() {
final LocalDateTime endDateTime = LocalDateTime.now();
final LocalDateTime startDateTime = endDateTime.minusWeeks(2L);
final List<ProductReviewCountDto> productsAndReviewCounts = productRepository.findAllByAverageRatingGreaterThan3(startDateTime, endDateTime);
final List<ProductReviewCountDto> productsAndReviewCounts = productRepository.findAllByAverageRatingGreaterThan3();
final Comparator<ProductReviewCountDto> rankingScoreComparator = Comparator.comparing(
(ProductReviewCountDto it) -> it.getProduct().calculateRankingScore(it.getReviewCount())
).reversed();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
import com.funeat.common.repository.BaseRepository;
import com.funeat.product.domain.Product;
import com.funeat.product.dto.ProductReviewCountDto;
import java.time.LocalDateTime;
import java.util.List;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
Expand All @@ -16,10 +14,8 @@ public interface ProductRepository extends BaseRepository<Product, Long> {
+ "FROM Product p "
+ "LEFT JOIN Review r ON r.product.id = p.id "
+ "WHERE p.averageRating > 3.0 "
+ "AND r.createdAt BETWEEN :startDateTime AND :endDateTime "
+ "GROUP BY p.id")
List<ProductReviewCountDto> findAllByAverageRatingGreaterThan3(final LocalDateTime startDateTime,
final LocalDateTime endDateTime);
List<ProductReviewCountDto> findAllByAverageRatingGreaterThan3();

@Query("SELECT p FROM Product p "
+ "WHERE p.name LIKE CONCAT('%', :name, '%') "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import static com.funeat.fixture.MemberFixture.멤버_멤버1_생성;
import static com.funeat.fixture.MemberFixture.멤버_멤버2_생성;
import static com.funeat.fixture.MemberFixture.멤버_멤버3_생성;
import static com.funeat.fixture.PageFixture.페이지요청_기본_생성;
import static com.funeat.fixture.ProductFixture.상품_망고빙수_가격5000원_평점4점_생성;
import static com.funeat.fixture.ProductFixture.상품_삼각김밥_가격1000원_평점3점_생성;
import static com.funeat.fixture.ProductFixture.상품_삼각김밥_가격2000원_평점4점_생성;
Expand All @@ -20,7 +19,6 @@
import com.funeat.common.RepositoryTest;
import com.funeat.product.dto.ProductReviewCountDto;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -62,39 +60,7 @@ class findAllByAverageRatingGreaterThan3_성공_테스트 {
final var expected = List.of(productReviewCountDto1, productReviewCountDto2);

// when
final var startDateTime = LocalDateTime.now().minusWeeks(2L);
final var endDateTime = LocalDateTime.now();
final var actual = productRepository.findAllByAverageRatingGreaterThan3(startDateTime, endDateTime);

// then
assertThat(actual).usingRecursiveComparison()
.isEqualTo(expected);
}

@Test
void 기간_안에_리뷰가_존재하는_상품이_없으면_빈_리스트를_반환한다() {
// given
final var category = 카테고리_간편식사_생성();
단일_카테고리_저장(category);

final var product1 = 상품_삼각김밥_가격1000원_평점3점_생성(category);
final var product2 = 상품_삼각김밥_가격2000원_평점4점_생성(category);
복수_상품_저장(product1, product2);

final var member1 = 멤버_멤버1_생성();
final var member2 = 멤버_멤버2_생성();
복수_멤버_저장(member1, member2);

final var review1 = 리뷰_이미지test5_평점5점_재구매X_생성(member1, product1, 0L, LocalDateTime.now().minusDays(15L));
final var review2 = 리뷰_이미지test5_평점5점_재구매X_생성(member2, product2, 0L, LocalDateTime.now().minusWeeks(3L));
복수_리뷰_저장(review1, review2);

final var expected = Collections.emptyList();

// when
final var startDateTime = LocalDateTime.now().minusWeeks(2L);
final var endDateTime = LocalDateTime.now();
final var actual = productRepository.findAllByAverageRatingGreaterThan3(startDateTime, endDateTime);
final var actual = productRepository.findAllByAverageRatingGreaterThan3();

// then
assertThat(actual).usingRecursiveComparison()
Expand Down

0 comments on commit 4ca009e

Please sign in to comment.