diff --git a/src/main/java/trothly/trothcam/domain/history/HistoryRepository.java b/src/main/java/trothly/trothcam/domain/history/HistoryRepository.java index ec584ce..323e645 100644 --- a/src/main/java/trothly/trothcam/domain/history/HistoryRepository.java +++ b/src/main/java/trothly/trothcam/domain/history/HistoryRepository.java @@ -11,5 +11,5 @@ public interface HistoryRepository extends JpaRepository { List findAllByProductIdOrderBySoldAtDesc(Long productId); - Optional findTopByProduct_IdOrderBySoldAt(Long productId); + Optional findTopByProduct_IdOrderBySoldAt(Long productId); } diff --git a/src/main/java/trothly/trothcam/service/web/ProductService.java b/src/main/java/trothly/trothcam/service/web/ProductService.java index 0095da2..fa3a127 100644 --- a/src/main/java/trothly/trothcam/service/web/ProductService.java +++ b/src/main/java/trothly/trothcam/service/web/ProductService.java @@ -56,12 +56,16 @@ public List findPublicProducts(String webId) throws BaseExceptio List result = findProducts.stream() .map(p -> { - LocalDateTime soldAt = historyRepository.findTopByProduct_IdOrderBySoldAt(p.getId()) - .orElse(p.getLastModifiedAt()); + Optional history = historyRepository.findTopByProduct_IdOrderBySoldAt(p.getId()); boolean isLiked = likeProductRepository.existsByProduct_IdAndMember_Id(p.getId(), p.getMember().getId()); + if(history.isEmpty()) + return new ProductsResDto(p.getId(), p.getTitle(), p.getMember().getWebId(), + p.getLastModifiedAt(), p.getPrice(), isLiked); + return new ProductsResDto(p.getId(), p.getTitle(), p.getMember().getWebId(), - soldAt, p.getPrice(), isLiked); + history.get().getSoldAt(), p.getPrice(), isLiked); + }) .collect(Collectors.toList());