Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
12 commits
Select commit Hold shift + click to select a range
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 @@ -23,16 +23,27 @@ public record OrderCreated(
Long orderId,
Long userId,
Integer originalTotalPrice,
Integer discountPrice
Integer discountPrice,
List<OrderItemInfo> orderItems
) {
public static OrderCreated from(Order order, String loginId) {
List<OrderItemInfo> orderItemInfos = order.getOrderItems().stream()
.map(item -> new OrderItemInfo(
item.getProductId(),
item.getProductName(),
item.getPrice(),
item.getQuantity()
))
.toList();

return new OrderCreated(
loginId,
order.getOrderKey(),
order.getId(),
order.getUserId(),
order.getOriginalTotalPrice(),
order.getDiscountPrice()
order.getDiscountPrice(),
orderItemInfos
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import com.loopers.domain.product.Product;
import com.loopers.domain.product.ProductService;
import com.loopers.infrastructure.cache.ProductCacheService;
import com.loopers.infrastructure.cache.RankingCacheService;
import com.loopers.support.error.CoreException;
import com.loopers.support.error.ErrorType;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand All @@ -20,6 +22,7 @@ public class ProductFacade {
private final ProductService productService;
private final BrandService brandService;
private final ProductCacheService productCacheService;
private final RankingCacheService rankingCacheService;

public List<ProductInfo> getProducts(ProductCommand.GetProductsCommand command) {
return productCacheService.getProductList(
Expand Down Expand Up @@ -57,12 +60,30 @@ public List<ProductInfo> getProducts(ProductCommand.GetProductsCommand command)
}

public ProductInfo getProduct(Long productId) {
return productCacheService.getProduct(productId, () -> {
ProductInfo productInfo = productCacheService.getProduct(productId, () -> {
Product product = productService.findProductById(productId)
.orElseThrow(() -> new CoreException(ErrorType.NOT_FOUND, "μƒν’ˆμ„ 찾을 수 μ—†μŠ΅λ‹ˆλ‹€."));
String brandName = brandService.findBrandNameById(product.getBrandId());
return ProductInfo.from(product, brandName);
});

Long rank = rankingCacheService.getProductRank(LocalDate.now(), productId);

if (rank != null) {
return new ProductInfo(
productInfo.id(),
productInfo.name(),
productInfo.brandId(),
productInfo.brandName(),
productInfo.price(),
productInfo.likeCount(),
productInfo.stock(),
productInfo.createdAt(),
rank
);
}

return productInfo;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ public record ProductInfo(
Integer price,
Integer likeCount,
Integer stock,
ZonedDateTime createdAt
ZonedDateTime createdAt,
Long rank
) {
public static ProductInfo from(Product product, String brandName) {
return new ProductInfo(
Expand All @@ -22,7 +23,22 @@ public static ProductInfo from(Product product, String brandName) {
product.getPrice().getPrice(),
product.getLikeCount().getCount(),
product.getStock().getQuantity(),
product.getCreatedAt()
product.getCreatedAt(),
null
);
}

public static ProductInfo from(Product product, String brandName, Long rank) {
return new ProductInfo(
product.getId(),
product.getName(),
product.getBrandId(),
brandName,
product.getPrice().getPrice(),
product.getLikeCount().getCount(),
product.getStock().getQuantity(),
product.getCreatedAt(),
rank
);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.loopers.application.ranking;

import java.time.LocalDate;

public class RankingCommand {

public record GetDailyRankingCommand(
LocalDate date,
int page,
int size
) {
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.loopers.application.ranking;

import com.loopers.domain.brand.BrandService;
import com.loopers.domain.product.Product;
import com.loopers.domain.product.ProductService;
import com.loopers.domain.ranking.Ranking;
import com.loopers.domain.ranking.RankingService;
import java.util.Objects;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RequiredArgsConstructor
@Component
@Slf4j
public class RankingFacade {

private final RankingService rankingService;
private final ProductService productService;
private final BrandService brandService;

public RankingInfo getDailyRanking(RankingCommand.GetDailyRankingCommand command) {
List<Ranking> rankings = rankingService.getRanking(
command.date(),
command.page(),
command.size()
);

if (rankings.isEmpty()) {
return new RankingInfo(List.of());
}

List<Long> productIds = rankings.stream()
.map(Ranking::productId)
.collect(Collectors.toList());

List<Product> products = productService.findProductsByIds(productIds);
Map<Long, Product> productMap = products.stream()
.collect(Collectors.toMap(Product::getId, product -> product));

List<Long> brandIds = products.stream()
.map(Product::getBrandId)
.distinct()
.collect(Collectors.toList());
Map<Long, String> brandNameMap = brandService.findBrandNamesByIds(brandIds);

List<RankingInfo.RankingItemInfo> rankingItemInfos = rankings.stream()
.map(ranking -> {
Product product = productMap.get(ranking.productId());
if (product == null) {
log.warn("λž­ν‚Ήμ— μžˆλŠ” μƒν’ˆμ΄ DB에 μ‘΄μž¬ν•˜μ§€ μ•ŠμŒ: productId={}", ranking.productId());
return null;
}

String brandName = brandNameMap.getOrDefault(product.getBrandId(), "μ•Œ 수 μ—†μŒ");

return new RankingInfo.RankingItemInfo(
ranking.productId(),
product.getName(),
brandName,
product.getPrice().getPrice(),
product.getLikeCount().getCount(),
ranking.rank(),
ranking.score()
);
})
.filter(Objects::nonNull)
.collect(Collectors.toList());

return new RankingInfo(rankingItemInfos);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.loopers.application.ranking;

import java.util.List;

public record RankingInfo(
List<RankingItemInfo> items
) {
public record RankingItemInfo(
Long productId,
String productName,
String brandName,
Integer price,
Integer likeCount,
Long rank,
Double score
) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Table;
import lombok.Builder;
import lombok.Getter;

@Entity
Expand All @@ -17,4 +18,19 @@ public class Brand extends BaseEntity {
@Column(nullable = false)
private String description;

public Brand() {
}

@Builder
public Brand(String name, String description) {
this.name = name;
this.description = description;
}

public static Brand createBrand(String name, String description) {
return Brand.builder()
.name(name)
.description(description)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ public interface ProductRepository {

List<Product> findProductsByLikesDesc(Long brandId, int page, int size);

List<Product> findProductsByIds(List<Long> productIds);

}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ public List<Product> findProductsByLikesDesc(Long brandId, int page, int size) {
return productRepository.findProductsByLikesDesc(brandId, page, size);
}

@Transactional(readOnly = true)
public List<Product> findProductsByIds(List<Long> productIds) {
return productRepository.findProductsByIds(productIds);
}

@Transactional
public Product increaseLikeCount(Long productId) {
Product product = productRepository.findProductById(productId)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.loopers.domain.ranking;

public record Ranking(
Long productId,
Long rank,
Double score
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.loopers.domain.ranking;

public record RankingItem(
Long productId,
Double score
) {
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.loopers.domain.ranking;

import com.loopers.infrastructure.cache.RankingCacheService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.IntStream;

@RequiredArgsConstructor
@Service
public class RankingService {

private final RankingCacheService rankingCacheService;

public List<Ranking> getRanking(LocalDate date, int page, int size) {
long start = (long) (page - 1) * size;
long end = start + size - 1;

List<RankingItem> rankingItems = rankingCacheService.getRankingRange(date, start, end);

if (rankingItems.isEmpty()) {
return new ArrayList<>();
}

return IntStream.range(0, rankingItems.size())
.mapToObj(i -> {
RankingItem item = rankingItems.get(i);
long rank = start + 1 + i;
return new Ranking(item.productId(), rank, item.score());
})
.toList();
}
}

Loading