From 72aa6575e4d9490256a1db0a39747e13d1efdebd Mon Sep 17 00:00:00 2001 From: Kim So Yeon Date: Fri, 7 Feb 2025 14:25:03 +0900 Subject: [PATCH] =?UTF-8?q?:sparkles:=20feat:=20=EC=82=AC=EC=9A=A9?= =?UTF-8?q?=EC=9E=90=EC=9D=98=20=EB=A1=9C=EA=B7=B8=EC=9D=B8=20=EC=97=AC?= =?UTF-8?q?=EB=B6=80=EC=97=90=20=EB=94=B0=EB=A5=B8=20=EC=A2=8B=EC=95=84?= =?UTF-8?q?=EC=9A=94=20=EC=97=AC=EB=B6=80=20=EB=B0=98=ED=99=98=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/benchmark/PagingBenchmark.java | 3 +- .../domain/product/api/ProductController.java | 22 ++++---- .../product/service/ProductService.java | 54 +++++++++++++++---- 3 files changed, 57 insertions(+), 22 deletions(-) diff --git a/src/jmh/java/com/benchmark/PagingBenchmark.java b/src/jmh/java/com/benchmark/PagingBenchmark.java index e1381f2..45c078d 100644 --- a/src/jmh/java/com/benchmark/PagingBenchmark.java +++ b/src/jmh/java/com/benchmark/PagingBenchmark.java @@ -77,7 +77,8 @@ public void testOffsetLastPaging(Blackhole blackhole) { @Benchmark public void testNoOffsetPaging(Blackhole blackhole) { Integer lastId = 149990; - List noOffsetProducts = productService.getProductsByShopType(shopType, lastId, pageSize); + List noOffsetProducts = productService.getProductsByShopType(shopType, lastId, pageSize, + null); blackhole.consume(noOffsetProducts); } } \ No newline at end of file diff --git a/src/main/java/com/pricewagon/pricewagon/domain/product/api/ProductController.java b/src/main/java/com/pricewagon/pricewagon/domain/product/api/ProductController.java index 9bd0cdb..3148f65 100644 --- a/src/main/java/com/pricewagon/pricewagon/domain/product/api/ProductController.java +++ b/src/main/java/com/pricewagon/pricewagon/domain/product/api/ProductController.java @@ -41,10 +41,11 @@ public class ProductController { public List getProductsByShopType( @PathVariable ShopType shopType, @RequestParam(required = false) Integer lastId, - @RequestParam(defaultValue = "10") int size + @RequestParam(defaultValue = "10") int size, + @AuthenticationPrincipal UserDetails userDetails ) { - return productService.getProductsByShopType(shopType, lastId, size); + return productService.getProductsByShopType(shopType, lastId, size, userDetails); } @Operation(summary = "개별 상품 정보 조회", description = "특정 상품에 대한 정보") @@ -54,11 +55,7 @@ public IndividualProductInfo getIndividualProductInfo( @PathVariable Integer productNumber, @AuthenticationPrincipal UserDetails userDetails ) { - if (userDetails != null) { - return productService.getIndividualProductInfo(shopType, productNumber, userDetails); - } else { - return productService.getIndividualProductInfo(shopType, productNumber, null); - } + return productService.getIndividualProductInfo(shopType, productNumber, userDetails); } @Operation(summary = "개별 상품 정보 조회 옛 버전", description = "이건 사용 안하는 API 입니다. 포폴 업데이트 후 삭제 예정") @@ -126,9 +123,10 @@ public List searchProductsAndBrandsDetail( public List getPopularProducts( @PathVariable ShopType shopType, @RequestParam(required = false) Integer lastId, - @RequestParam(defaultValue = "10") int size + @RequestParam(defaultValue = "10") int size, + @AuthenticationPrincipal UserDetails userDetails ) { - return productService.getPopularProducts(shopType, lastId, size); + return productService.getPopularProducts(shopType, lastId, size, userDetails); } @Operation(summary = "알람 기준 인기 상품 조회", description = "알람 많이 등록한 기준 인기 상품 조회") @@ -136,9 +134,11 @@ public List getPopularProducts( public List getAlarmProducts( @PathVariable ShopType shopType, @RequestParam(required = false) Integer lastId, - @RequestParam(defaultValue = "10") int size + @RequestParam(defaultValue = "10") int size, + @AuthenticationPrincipal UserDetails userDetails ) { - return productService.getPopularProductsByAlarm(shopType, lastId, size); + return productService.getPopularProductsByAlarm(shopType, lastId, size, userDetails); + } } diff --git a/src/main/java/com/pricewagon/pricewagon/domain/product/service/ProductService.java b/src/main/java/com/pricewagon/pricewagon/domain/product/service/ProductService.java index 7cbe99f..9d63c84 100644 --- a/src/main/java/com/pricewagon/pricewagon/domain/product/service/ProductService.java +++ b/src/main/java/com/pricewagon/pricewagon/domain/product/service/ProductService.java @@ -1,6 +1,7 @@ package com.pricewagon.pricewagon.domain.product.service; import java.util.List; +import java.util.Optional; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; @@ -43,9 +44,18 @@ public class ProductService { // 쇼핑몰에 따른 상품 리스트 조회 @Transactional(readOnly = true) - public List getProductsByShopType(ShopType shopType, Integer lastId, int size) { - List products = productRepository.findProductsByShopTypeAndLastId(shopType, lastId, size); - return convertToBasciProductInfo(products); + public List getProductsByShopType(ShopType shopType, Integer lastId, int size, + UserDetails userDetails) { + if (userDetails == null) { + List products = productRepository.findProductsByShopTypeAndLastId(shopType, lastId, size); + return convertToBasciProductInfo(products); + } else { + User user = userRepository.findByAccount(userDetails.getUsername()) + .orElseThrow(() -> new CustomException(ErrorCode.MEMBER_NOT_FOUND)); + List products = productRepository.findProductsByShopTypeAndLastId(shopType, lastId, size); + return convertToBasciProductInfo(products, user); + } + } // 검색 후 상품 및 브랜드 리스트 조회 @@ -67,18 +77,32 @@ public List getSearchingProductsAndBrandsDetail(ShopType shopT // 좋아요 기반으로 인기 상품 조회 @Transactional(readOnly = true) - public List getPopularProducts(ShopType shopType, Integer lastId, int size) { + public List getPopularProducts(ShopType shopType, Integer lastId, int size, + UserDetails userDetails) { + User user = Optional.ofNullable(userDetails) + .map(UserDetails::getUsername) + .map(username -> userRepository.findByAccount(username) + .orElseThrow(() -> new CustomException(ErrorCode.MEMBER_NOT_FOUND))) + .orElse(null); List popularProducts = productRepository.findPopularProductsByShopTypeAndLastId(shopType, lastId, size); - return convertToBasciProductInfo(popularProducts); + return user != null ? convertToBasciProductInfo(popularProducts, user) + : convertToBasciProductInfo(popularProducts); } - + // 알람 기반으로 인기 상품 조회 @Transactional(readOnly = true) - public List getPopularProductsByAlarm(ShopType shopType, Integer lastId, int size) { - List popularProducts = productRepository.findAlarmProductsByShopTypeAndLastId(shopType, lastId, - size); - return convertToBasciProductInfo(popularProducts); + public List getPopularProductsByAlarm(ShopType shopType, Integer lastId, int size, + UserDetails userDetails) { + User user = Optional.ofNullable(userDetails) + .map(UserDetails::getUsername) + .map(username -> userRepository.findByAccount(username) + .orElseThrow(() -> new CustomException(ErrorCode.MEMBER_NOT_FOUND))) + .orElse(null); + + List popularProducts = productRepository.findAlarmProductsByShopTypeAndLastId(shopType, lastId, size); + return user != null ? convertToBasciProductInfo(popularProducts, user) + : convertToBasciProductInfo(popularProducts); } // 개별 상품 정보 조회 @@ -216,4 +240,14 @@ private List convertToBasciProductInfo(List products) .toList(); } + private List convertToBasciProductInfo(List products, User user) { + return products.stream() + .map(product -> { + boolean isLiked = likeRepository.existsByUserAndProduct(user, product); + Integer previousPrice = productHistoryService.getDifferentLatestPriceByProductId(product); + return BasicProductInfo.createWithLikeStatus(product, previousPrice, isLiked); + }) + .toList(); + } + }