Skip to content

Commit

Permalink
fix: 상품에 사진이 있는 리뷰가 하나도 없을 경우 기본 이미지 설정
Browse files Browse the repository at this point in the history
  • Loading branch information
hanueleee committed Oct 19, 2023
1 parent 515a94d commit 6baab7d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
4 changes: 4 additions & 0 deletions backend/src/main/java/com/funeat/product/domain/Category.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ public Category(final String name, final CategoryType type, final String image)
this.image = image;
}

public boolean isFood() {
return type == CategoryType.FOOD;
}

public Long getId() {
return id;
}
Expand Down
19 changes: 17 additions & 2 deletions backend/src/main/java/com/funeat/product/domain/Product.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import org.springframework.beans.factory.annotation.Value;

@Entity
public class Product {
Expand All @@ -28,6 +29,8 @@ public class Product {

private Double averageRating = 0.0;

private Long reviewCount = 0L;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "category_id")
private Category category;
Expand All @@ -38,7 +41,11 @@ public class Product {
@OneToMany(mappedBy = "product")
private List<ProductRecipe> productRecipes;

private Long reviewCount = 0L;
@Value("${cloud.aws.image.food}")
private String basicFoodImage;

@Value("${cloud.aws.image.store}")
private String basicStoreImage;

protected Product() {
}
Expand Down Expand Up @@ -107,7 +114,15 @@ public Double calculateRankingScore(final Long reviewCount) {
return averageRating - (averageRating - 3.0) * factor;
}

public void updateImage(final String topFavoriteImage) {
public void updateBasicImage() {
if (category.isFood()) {
this.image = basicFoodImage;
return;
}
this.image = basicStoreImage;
}

public void updateFavoriteImage(final String topFavoriteImage) {
this.image = topFavoriteImage;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,12 @@ public void updateProductImage(final Long productId) {
final PageRequest pageRequest = PageRequest.of(FIRST_PAGE, ONE);

final List<Review> topFavoriteReview = reviewRepository.findPopularReviewWithImage(productId, pageRequest);
if (!topFavoriteReview.isEmpty()) {
final String topFavoriteReviewImage = topFavoriteReview.get(START_INDEX).getImage();
product.updateImage(topFavoriteReviewImage);
if (topFavoriteReview.isEmpty()) {
product.updateBasicImage();
return;
}
final String topFavoriteReviewImage = topFavoriteReview.get(START_INDEX).getImage();
product.updateFavoriteImage(topFavoriteReviewImage);
}

public SortingReviewsResponse sortingReviews(final Long productId, final Long memberId,
Expand Down

0 comments on commit 6baab7d

Please sign in to comment.