Skip to content
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
3 changes: 2 additions & 1 deletion src/jmh/java/com/benchmark/PagingBenchmark.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ public void testOffsetLastPaging(Blackhole blackhole) {
@Benchmark
public void testNoOffsetPaging(Blackhole blackhole) {
Integer lastId = 149990;
List<BasicProductInfo> noOffsetProducts = productService.getProductsByShopType(shopType, lastId, pageSize);
List<BasicProductInfo> noOffsetProducts = productService.getProductsByShopType(shopType, lastId, pageSize,
null);
blackhole.consume(noOffsetProducts);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,11 @@ public class ProductController {
public List<BasicProductInfo> 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 = "특정 상품에 대한 정보")
Expand All @@ -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 입니다. 포폴 업데이트 후 삭제 예정")
Expand Down Expand Up @@ -126,19 +123,22 @@ public List<BasicProductInfo> searchProductsAndBrandsDetail(
public List<BasicProductInfo> 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 = "알람 많이 등록한 기준 인기 상품 조회")
@GetMapping("/alarm/{shopType}")
public List<BasicProductInfo> 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);

}

}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -43,9 +44,18 @@ public class ProductService {

// 쇼핑몰에 따른 상품 리스트 조회
@Transactional(readOnly = true)
public List<BasicProductInfo> getProductsByShopType(ShopType shopType, Integer lastId, int size) {
List<Product> products = productRepository.findProductsByShopTypeAndLastId(shopType, lastId, size);
return convertToBasciProductInfo(products);
public List<BasicProductInfo> getProductsByShopType(ShopType shopType, Integer lastId, int size,
UserDetails userDetails) {
if (userDetails == null) {
List<Product> products = productRepository.findProductsByShopTypeAndLastId(shopType, lastId, size);
return convertToBasciProductInfo(products);
} else {
User user = userRepository.findByAccount(userDetails.getUsername())
.orElseThrow(() -> new CustomException(ErrorCode.MEMBER_NOT_FOUND));
List<Product> products = productRepository.findProductsByShopTypeAndLastId(shopType, lastId, size);
return convertToBasciProductInfo(products, user);
}

}

// 검색 후 상품 및 브랜드 리스트 조회
Expand All @@ -67,18 +77,32 @@ public List<BasicProductInfo> getSearchingProductsAndBrandsDetail(ShopType shopT

// 좋아요 기반으로 인기 상품 조회
@Transactional(readOnly = true)
public List<BasicProductInfo> getPopularProducts(ShopType shopType, Integer lastId, int size) {
public List<BasicProductInfo> 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<Product> popularProducts = productRepository.findPopularProductsByShopTypeAndLastId(shopType, lastId,
size);
return convertToBasciProductInfo(popularProducts);
return user != null ? convertToBasciProductInfo(popularProducts, user)
: convertToBasciProductInfo(popularProducts);
}

// 알람 기반으로 인기 상품 조회
@Transactional(readOnly = true)
public List<BasicProductInfo> getPopularProductsByAlarm(ShopType shopType, Integer lastId, int size) {
List<Product> popularProducts = productRepository.findAlarmProductsByShopTypeAndLastId(shopType, lastId,
size);
return convertToBasciProductInfo(popularProducts);
public List<BasicProductInfo> 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<Product> popularProducts = productRepository.findAlarmProductsByShopTypeAndLastId(shopType, lastId, size);
return user != null ? convertToBasciProductInfo(popularProducts, user)
: convertToBasciProductInfo(popularProducts);
}

// 개별 상품 정보 조회
Expand Down Expand Up @@ -216,4 +240,14 @@ private List<BasicProductInfo> convertToBasciProductInfo(List<Product> products)
.toList();
}

private List<BasicProductInfo> convertToBasciProductInfo(List<Product> 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();
}

}