Skip to content

Commit

Permalink
refactor:: 제품리스트 요청 api 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
HyunJunSon committed Feb 21, 2024
1 parent 0b69a54 commit f869a5f
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@ Page<ProductDto> sort(ProductService productService, Pageable pageable) {
RECOMMENDATION_PRODUCT_ORDER("recommend-order") {
@Override
Page<ProductDto> sort(ProductService productService, Pageable pageable) {
//Fixme :: 향후 Like Table 구현후, 정렬메소드 변경이 필요합니다.(임시로 violationProduct 리턴으로 구현)

return productService.getViolationProducts(pageable);
return productService.getFavoriteProducts(pageable);
}
},
RECENT_PRODUCT_ORDER("recent-order") {
Expand All @@ -38,7 +37,7 @@ Page<ProductDto> sort(ProductService productService, Pageable pageable) {
}
};

private String orderType;
private final String orderType;

Sort(String orderType) {
this.orderType = orderType;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
package com.kernel360.product.service;

import com.kernel360.exception.BusinessException;
import com.kernel360.likes.repository.LikeRepository;
import com.kernel360.main.code.ProductsErrorCode;
import com.kernel360.main.dto.RecommendProductsDto;
import com.kernel360.product.dto.ProductDetailDto;
import com.kernel360.product.dto.ProductDto;
import com.kernel360.product.entity.Product;
import com.kernel360.product.entity.SafetyStatus;
import com.kernel360.product.repository.ProductRepository;

import java.util.List;

import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -19,14 +24,15 @@
public class ProductService {

private final ProductRepository productRepository;
private final LikeRepository likeRepository;

@Transactional(readOnly = true)
public List<ProductDto> getProducts() {

return productRepository.findAll()
.stream()
.map(ProductDto::from)
.toList();
.stream()
.map(ProductDto::from)
.toList();
}

@Transactional(readOnly = true)
Expand All @@ -38,11 +44,8 @@ public Page<ProductDto> getProductsByKeyword(String keyword, Pageable pageable)

@Transactional(readOnly = true)
public Page<ProductDto> getProductListOrderByViewCount(Pageable pageable) {
Page<Product> products = productRepository.findTop5ByOrderByProductNameDesc(pageable);
// Page<Product> products = productRepository.findAllByOrderByViewCountDesc();
//FIXME:: viewCount 값이 존재하지 않아,제품이룸순 데이터를 샘플로 전달한 후 향후 변경

return products.map(ProductDto::from);
return productRepository.findAllByOrderByViewCountDesc(pageable).map(ProductDto::from);
}

@Transactional(readOnly = true)
Expand All @@ -55,13 +58,8 @@ public Page<RecommendProductsDto> getRecommendProducts(Pageable pageable) {
@Transactional(readOnly = true)
public Page<ProductDto> getViolationProducts(Pageable pageable) {

return productRepository.findAllByOrderByCreatedAtDesc(pageable)
.map(ProductDto::from);
//FIXME :: 데이터가 없어서 최근데이터로 대신 리턴
// return productRepository.findAllBySafetyStatusEquals(SafetyStatus.DANGER)
// .stream()
// .map(ProductDto::from)
// .toList();
return productRepository.findAllBySafetyStatusEquals(SafetyStatus.DANGER, pageable)
.map(ProductDto::from);
}

@Transactional(readOnly = true)
Expand All @@ -75,7 +73,7 @@ public Page<ProductDto> getRecentProducts(Pageable pageable) {
@Transactional(readOnly = true)
public ProductDetailDto getProductById(Long id) {

return productRepository.findById(id)
return productRepository.findById(id)
.map(ProductDetailDto::from)
.orElseThrow(() -> new BusinessException(ProductsErrorCode.NOT_FOUND_PRODUCT));
}
Expand All @@ -84,11 +82,29 @@ public ProductDetailDto getProductById(Long id) {
public void updateViewCount(Long id) {
productRepository.updateViewCount(id);
}

@Transactional(readOnly = true)
public Page<ProductDetailDto> getProductByOCR(String reportNo, Pageable pageable) {

return productRepository.findProductByReportNumberEquals(reportNo, pageable)
return productRepository.findProductByReportNumberEquals(reportNo, pageable)
.map(ProductDetailDto::from);
}

public Page<ProductDto> getFavoriteProducts(Pageable pageable) {
Page<Object[]> results = likeRepository.findTop20ByProductNoOrderByLikeCountDesc(pageable);

List<ProductDto> productDtos = results.getContent().stream()
.map(result -> {
Long productNo = (Long) result[0];
Product product = productRepository.findById(productNo)
.orElseThrow(() -> new BusinessException(ProductsErrorCode.NOT_FOUND_PRODUCT));
return ProductDto.from(product);
})
.toList();

return new PageImpl<>(productDtos, pageable, results.getTotalElements());
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.Optional;
Expand All @@ -12,6 +13,9 @@
@Repository
public interface LikeRepository extends JpaRepository<Like, Long> {
Optional<Like> findByMemberNoAndProductNo(Long memberNo, Long productNo);

Page<Like> findAllByMemberNo(Long memberNo, Pageable pageable);

@Query(value = "SELECT l.productNo, COUNT(l) AS likeCount FROM Like l GROUP BY l.productNo ORDER BY likeCount DESC")
Page<Object[]> findTop20ByProductNoOrderByLikeCountDesc(Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class Member extends BaseEntity {
@Column(name = "member_no", nullable = false)
private Long memberNo;

@Column(name = "id", nullable = false)
@Column(name = "id", nullable = false, updatable = false)
private String id;

@OneToOne(mappedBy = "member", cascade = CascadeType.ALL, fetch = FetchType.LAZY, optional = false)
Expand Down

0 comments on commit f869a5f

Please sign in to comment.