selling 엔티티에서 inventoryDiv = 1, sellingStatus = 11, productId = 각 상품id
이렇게 3가지 데이터만 받아오면 되었기 때문에 dto를 따로 만들까 하다가 뭐 묶어오거나 따로 sorting하지 않는데 굳이 싶어서 findBy로 처리함
//Selling 엔티티
@JsonBackReference
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "product_id", nullable = false)
private Product product;
이때 product로 해야하는지 product_id로 해야하는지 productId나 productid로 해도 되는지 의문이었음
이전에 분명이 productId로 했다가 안되서 product_id식으로 구리게 작업했던 기억이 있는데..
결론적으로는 productId로 했으면 됐음
// SellingRepository
List<Selling> findByInventoryDivAndSellingStatusAndProductId(Integer inventoryDiv, Integer sellingStatus, Long productid);
그리고 이걸 처음에는 service에서 selling 객체를 그대로 읽어오도록 했는데
public List<Selling> findByFastProduct(Integer inventoryDiv, Integer sellingStatus, Long productid) {
return sellingRepository.findByInventoryDivAndSellingStatusAndProductId(1, 11, Long productid);
}
// 빠른 배송 상품 존재 여부 확인
public boolean hasStorageProduct(Long productId) {
List<Selling> sellings = sellingRepository.findByInventoryDivAndSellingStatusAndProductId(1, 11, productId);
return sellings != null && !sellings.isEmpty();
}
귯!
그럼 이제 이걸 각 메서드를 부를때(발매일순 조회, 인기상품순 조회 등등..)에 엮어줘야 하는데...
// 메인 - 발매일 순 조회 & 빠른배송 여부 확인
@GetMapping("/api/get/product/new")
public ResponseEntity<Map<String, Object>> getProductNew() {
try {
Map<String, Object> result = new HashMap<>();
List<ProductNewDTO> lists = productService.getProductNew();
result.put("lists", lists);
for (ProductNewDTO productNewDTO : lists) {
boolean hasStorageProduct = sellingService.hasStorageProduct(productNewDTO.getId());
result.put(productNewDTO.getId().toString(), hasStorageProduct);
}
return new ResponseEntity<>(result, HttpStatus.OK);
} catch (Exception e) {
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
}
}
발매일 순으로 받아온 값을 lists에 배열로 저장하고
이 배열에 대해 for문을 돌리면서
getId로 productId값을 가져오고
이걸 매개변수로 hasStorageProduct 메서드를 실행시켜 true, false 여부를 productid값과 함께 출력하도록함
// 메인 - 발매일 순 조회 & 빠른배송 여부 확인
@GetMapping("/api/get/product/new")
public ResponseEntity<Map<String, Object>> getProductNew() {
try {
Map<String, Object> result = new HashMap<>();
List<ProductNewDTO> lists = productService.getProductNew();
List<Map<String, Object>> storage = new ArrayList<>();
for (ProductNewDTO productNewDTO : lists) {
boolean hasStorageProduct = sellingService.hasStorageProduct(productNewDTO.getId());
Map<String, Object> storageMap = new HashMap<>();
storageMap.put("id", productNewDTO.getId());
storageMap.put("hasStorage", hasStorageProduct);
storage.add(storageMap);
}
result.put("lists", lists);
result.put("storage", storage);
return new ResponseEntity<>(result, HttpStatus.OK);
} catch (Exception e) {
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
}
}
map 안에 또 map을 만들어서 id와 hsaStorage 값을 가지도록 했는데 사실 데이터에 접근하기는 이게 더 불편한것 같기도한데.. 여러개로 쌓여있으니까 . 근데 이게 읽어오기가 더 편하고 직관적이라 일단 이렇게 하기로함