Skip to content

Commit

Permalink
feat: get products owned by normal user
Browse files Browse the repository at this point in the history
  • Loading branch information
lvolzdev committed Jun 21, 2024
1 parent 8225488 commit 9f18cb7
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -223,4 +223,25 @@ public GlobalResponse<Object> searchProductByName(

return ApiUtils.success("success", result);
}

@PostMapping("/owned")
@Operation(summary = "Get Products owned by user", description = "Return product information that user own.")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "OK"),
@ApiResponse(responseCode = "204", description = "No content")
})
public GlobalResponse<Object> getUserOwnedProducts(
@RequestBody Map<String, String> productsReqMap,
@RequestParam(required = false, defaultValue = "0", value = "pageNo") int pageNo,
@RequestParam(required = false, defaultValue = "10", value = "size") int size
) {
log.debug("Get products owned by user. productsReq: {}", productsReqMap);
log.debug("pageNum: {}, size: {}", pageNo, size);
Map<String, Object> result = new HashMap<>();

List<ProductDto.BasicRespDto> products = productService.getUserOwnedProducts(productsReqMap, pageNo, size);
result.put("products", products);

return ApiUtils.success("success", result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
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.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface ProductRepository extends JpaRepository<Product, Long> {
public interface ProductRepository extends JpaRepository<Product, Long>, JpaSpecificationExecutor<Product> {
@Query(value = "SELECT * FROM product p ORDER BY RAND() LIMIT :limit", nativeQuery = true)
List<Product> findRandomProducts(@Param("limit") int limit);

Expand All @@ -25,4 +26,11 @@ public interface ProductRepository extends JpaRepository<Product, Long> {
// 인기순 정렬
@Query("SELECT p FROM Product p JOIN p.boardCount b WHERE p.category.id = :categoryId ORDER BY b.boardCount DESC")
Page<Product> findByCategoryIdOrderByBoardCount(@Param("categoryId") Long categoryId, Pageable pageable);

// 보유상품 반환


// Page<Product> findByNameAndCorpName(String name, String corpName);
// @Query("SELECT p FROM Product p WHERE p.name IN :names AND p.corp.name IN :corpNames")
// Page<Product> findByNameInAndCorpNameIn(@Param("names") List<String> names, @Param("corpNames") List<String> corpNames, Pageable pageable);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.pda.productapplication.repository;

import com.pda.productapplication.entity.Corp;
import com.pda.productapplication.entity.Product;
import jakarta.persistence.criteria.Join;
import jakarta.persistence.criteria.JoinType;
import org.springframework.data.jpa.domain.Specification;

public class ProductSpecification {
// 상품명과 회사명을 기준으로 필터링
public static Specification<Product> equalProductName(String name) {
return (root, query, criteriaBuilder) ->
criteriaBuilder.equal(root.get("name"), name);
}

public static Specification<Product> equalCorpName(String corpName) {
return (root, query, criteriaBuilder) -> {
Join<Product, Corp> corpJoin = root.join("corp", JoinType.INNER);
return criteriaBuilder.equal(corpJoin.get("name"), corpName);
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.*;
import java.util.stream.Collectors;

@Service
Expand Down Expand Up @@ -342,6 +340,38 @@ public List<ProductDto.BasicRespDto> searchProductByName(String name, int pageNo
).collect(Collectors.toList());
}

/**
* Get user-owned products
* @param productsReqMap requested products (product name, corp name)
* @param pageNo page num
* @param size size
*/
public List<ProductDto.BasicRespDto> getUserOwnedProducts(Map<String, String> productsReqMap, int pageNo, int size) {
Pageable pageable = PageRequest.of(pageNo, size);
List<ProductDto.BasicRespDto> productDtoList = new ArrayList<>();

productsReqMap.forEach((name, corpName) -> {
Specification<Product> spec = Specification.where(ProductSpecification.equalProductName(name))
.and(ProductSpecification.equalCorpName(corpName));
List<Product> products = productRepository.findAll(spec, pageable).getContent(); // 필터링된 상품

log.debug("Filtered product: {}", products);

productDtoList.addAll(products.stream().map(product -> ProductDto.BasicRespDto.builder()
.id(product.getId())
.name(product.getName())
.corpName(product.getCorp().getName())
.corpImage(product.getCorp().getLogoImg())
.cardImage(product.getCardImg())
.tags(convertToList(product.getTags()))
.boardCount(product.getBoardCount().getBoardCount())
.createdTime(product.getCreatedAt())
.build()).toList());
});
return productDtoList;
}


public List<String> convertToList(String data) {
if (data == null || data.isEmpty()) {
return Collections.emptyList();
Expand Down

1 comment on commit 9f18cb7

@lvolzdev
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JpaSpecificationExecutor 사용

  • 상품이름 1차 필터링
  • 회사이름 2차 필터링

Please sign in to comment.