Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

O2 f 114/feat/search product #22

Merged
merged 19 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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 @@ -4,6 +4,7 @@
import com.dailyon.productservice.category.dto.request.UpdateCategoryRequest;
import com.dailyon.productservice.category.dto.response.CreateCategoryResponse;
import com.dailyon.productservice.category.dto.response.ReadAllCategoryListResponse;
import com.dailyon.productservice.category.dto.response.ReadChildrenCategoryListResponse;
import com.dailyon.productservice.category.service.CategoryService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
Expand All @@ -30,9 +31,10 @@ public ResponseEntity<ReadAllCategoryListResponse> readAllCategories(@RequestHea
return ResponseEntity.status(HttpStatus.OK).body(categoryService.readAllCategories());
}

/*
TODO : 리프 카테고리 조회 -> 관리자 사용
*/
@GetMapping("/categories/leaf")
public ResponseEntity<ReadChildrenCategoryListResponse> readLeafCategories(@RequestHeader String role) {
return ResponseEntity.status(HttpStatus.OK).body(categoryService.readLeafCategories());
}

@PutMapping("/categories/{categoryId}")
public ResponseEntity<Void> updateCategory(@RequestHeader String role,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.dailyon.productservice.category.entity.Category;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;
Expand All @@ -13,4 +14,36 @@ public interface CategoryJpaRepository extends JpaRepository<Category, Long> {
Optional<Category> findByIdAndDeletedIsFalse(Long id);
List<Category> findByDeletedIsFalse();
List<Category> findByDeletedIsFalseAndMasterCategory_Id(Long masterCategoryId);

@Query(nativeQuery = true, value =
"WITH RECURSIVE LeafCategory(id, master_category_id, name) AS (" +
"SELECT c.id, c.master_category_id, c.name " +
"FROM category AS c " +
"WHERE c.id NOT IN (" +
"SELECT DISTINCT c.master_category_id " +
"FROM category AS c " +
"WHERE c.master_category_id IS NOT NULL AND c.is_deleted = false) " +
"UNION ALL " +
"SELECT c.id, c.master_category_id, c.name " +
"FROM category AS c " +
"INNER JOIN LeafCategory AS lc ON c.master_category_id = lc.id " +
"WHERE c.is_deleted = false) " +
"SELECT * FROM LeafCategory")
List<Category> findLeafCategories();

@Query(nativeQuery = true, value =
"WITH RECURSIVE CategoryTree(id) AS (" +
"SELECT c.id " +
"FROM category AS c " +
"WHERE c.id = :categoryId AND c.is_deleted = false " +
"UNION ALL " +
"SELECT c.id " +
"FROM category AS c " +
"INNER JOIN CategoryTree AS ct " +
"ON c.master_category_id = ct.id " +
"WHERE c.is_deleted = false)" +
"SELECT c.* FROM category AS c " +
"INNER JOIN CategoryTree AS ct ON c.id = ct.id AND " +
"c.is_deleted = false")
List<Category> findAllChildCategories(Long categoryId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ public interface CategoryRepository {
Optional<Category> findById(Long id);
List<Category> findAll();
List<Category> findByMasterCategoryId(Long masterCategoryId);
List<Category> findLeafCategories();
List<Category> findAllChildCategories(Long categoryId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,14 @@ public List<Category> findAll() {
public List<Category> findByMasterCategoryId(Long masterCategoryId) {
return categoryJpaRepository.findByDeletedIsFalseAndMasterCategory_Id(masterCategoryId);
}

@Override
public List<Category> findLeafCategories() {
return categoryJpaRepository.findLeafCategories();
}

@Override
public List<Category> findAllChildCategories(Long categoryId) {
return categoryJpaRepository.findAllChildCategories(categoryId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,8 @@ public void updateCategoryName(Long id, UpdateCategoryRequest updateCategoryRequ

category.updateName(updateCategoryRequest.getName());
}

public ReadChildrenCategoryListResponse readLeafCategories() {
return ReadChildrenCategoryListResponse.fromEntity(categoryRepository.findLeafCategories());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

import com.dailyon.productservice.common.enums.Gender;
import com.dailyon.productservice.common.enums.ProductType;
import com.dailyon.productservice.product.dto.response.ReadOOTDSearchSliceResponse;
import com.dailyon.productservice.product.dto.response.ReadProductDetailResponse;
import com.dailyon.productservice.product.dto.response.ReadProductSliceResponse;
import com.dailyon.productservice.product.service.ProductService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
Expand All @@ -24,12 +23,25 @@ ResponseEntity<ReadProductDetailResponse> readProductDetail(@PathVariable Long p
}

@GetMapping
ResponseEntity<ReadProductSliceResponse> readProductSlice(@RequestParam(required = false) Long brandId,
ResponseEntity<ReadProductSliceResponse> readProductSlice(@RequestParam Long lastId,
@RequestParam(required = false) Long brandId,
@RequestParam(required = false) Long categoryId,
@RequestParam(required = false) Gender gender,
@RequestParam ProductType type,
@RequestParam(required = false) String query,
@PageableDefault(page = 0, size = 8) Pageable pageable) {
return ResponseEntity.status(HttpStatus.OK).body(productService.readProductSlice(brandId, categoryId, gender, type, query, pageable));
@RequestParam ProductType type) {
return ResponseEntity.status(HttpStatus.OK).body(productService.readProductSlice(lastId, brandId, categoryId, gender, type));
}

@GetMapping("/search")
ResponseEntity<ReadProductSliceResponse> searchProducts(@RequestParam Long lastId,
@RequestParam(required = false) String query,
@RequestParam(required = false) String code) {
return ResponseEntity.status(HttpStatus.OK).body(productService.searchProductSlice(lastId, query, code));
}

@GetMapping("/search/ootd")
ResponseEntity<ReadOOTDSearchSliceResponse> searchProductsFromOOTD(@RequestParam Long lastId,
@RequestParam(required = false) String query,
@RequestParam(required = false) String code) {
return ResponseEntity.status(HttpStatus.OK).body(productService.searchFromOOTD(lastId, query, code));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
public class ProductFeignController {
private final ProductService productService;

@GetMapping("/post-image/products")
@GetMapping("/products/post-image")
ResponseEntity<ReadOOTDProductListResponse> readOOTDProductDetail(@RequestParam List<Long> id) {
return ResponseEntity.status(HttpStatus.OK).body(productService.readOOTDProductDetails(id));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.dailyon.productservice.product.dto.response;

import com.dailyon.productservice.product.entity.Product;
import com.dailyon.productservice.productstock.entity.ProductStock;
import lombok.Builder;
import lombok.Getter;

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

@Getter
@Builder
public class ReadOOTDSearchResponse {
private Long id;
private String name;
private String imgUrl;
private String brandName;
private List<String> sizeNames;

public static ReadOOTDSearchResponse fromEntity(Product product) {
return ReadOOTDSearchResponse.builder()
.id(product.getId())
.name(product.getName())
.imgUrl(product.getImgUrl())
.brandName(product.getBrand().getName())
.sizeNames(product.getProductStocks().stream()
.map(productStock -> productStock.getProductSize().getName())
.collect(Collectors.toList()))
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.dailyon.productservice.product.dto.response;

import com.dailyon.productservice.product.entity.Product;
import lombok.Builder;
import lombok.Getter;
import org.springframework.data.domain.Slice;

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

@Getter
@Builder
public class ReadOOTDSearchSliceResponse {
private boolean hasNext;
private List<ReadOOTDSearchResponse> products;

public static ReadOOTDSearchSliceResponse fromEntity(Slice<Product> products) {
return ReadOOTDSearchSliceResponse.builder()
.hasNext(products.hasNext())
.products(products.stream()
.map(ReadOOTDSearchResponse::fromEntity)
.collect(Collectors.toList()))
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
package com.dailyon.productservice.product.repository;

import com.dailyon.productservice.category.entity.Category;
import com.dailyon.productservice.common.enums.Gender;
import com.dailyon.productservice.common.enums.ProductType;
import com.dailyon.productservice.product.entity.Product;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;

import java.util.List;

public interface ProductCustomRepository {
Slice<Product> findProductSlice(Long brandId, Long categoryId,
Gender gender, ProductType productType,
String query, Pageable pageable);
Slice<Product> findProductSlice(Long lastId, Long brandId, List<Category> childCategories,
Gender gender, ProductType productType);

Page<Product> findProductPage(Long brandId, Long categoryId, ProductType type, Pageable pageable);

Slice<Product> searchProducts(Long lastId, String query, String code);

Slice<Product> searchProductsFromOOTD(Long lastId, String query, String code);
}
Loading