Skip to content

Commit

Permalink
#27 feat(product.service): 상품 검색 기능 작성
Browse files Browse the repository at this point in the history
  • Loading branch information
sim-mer committed Oct 24, 2024
1 parent 2c29a01 commit 1d2d527
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import com.helpmeCookies.product.entity.Product;
import com.helpmeCookies.product.repository.ProductImageRepository;
import com.helpmeCookies.product.repository.ProductRepository;
import com.helpmeCookies.product.dto.ProductPage;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -15,6 +17,12 @@ public class ProductService {
private final ProductRepository productRepository;
private final ProductImageRepository productImageRepository;

@Transactional(readOnly = true)
public ProductPage.Paging getProductsByPage(String query, Pageable pageable) {
var productPage = productRepository.findByNameWithIdx(query, pageable);
return ProductPage.Paging.from(productPage);
}

public Product save(ProductRequest productSaveRequest) {
//TODO ArtistInfo 코드 병합시 수정 예정
Product product = productSaveRequest.toEntity(null);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.helpmeCookies.product.service.dto;

import com.helpmeCookies.product.repository.dto.ProductSearch;
import java.util.List;
import org.springframework.data.domain.Page;

public class ProductPage {

public record Info(
Long id,
String name,
String artist,
Long price
) {
public static Info from(ProductSearch productSearch) {
return new Info(
productSearch.getId(),
productSearch.getName(),
productSearch.getArtist(),
productSearch.getPrice()
);
}

public static List<Info> of(List<ProductSearch> content) {
return content.stream()
.map(Info::from)
.toList();
}
}

public record Paging (
boolean hasNext,
List<Info> products
) {

public static Paging from(Page<ProductSearch> productPage) {
return new Paging(
productPage.hasNext(),
Info.of(productPage.getContent())
);
}
}

}

0 comments on commit 1d2d527

Please sign in to comment.