Skip to content

Commit

Permalink
Fix: popular Item inquiry
Browse files Browse the repository at this point in the history
  • Loading branch information
oo-ni committed Feb 6, 2024
1 parent 47547bc commit d74e1d4
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/main/java/kea/dpang/item/controller/ItemController.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ public ResponseEntity<SuccessResponse<List<ReviewDto>>> getReviewList(

@GetMapping("/popular/list")
@Operation(summary = "인기 상품 리스트 조회", description = "인기 상품 정보를 페이지 정보에 따라 조회합니다.")
public ResponseEntity<SuccessResponse<List<PopularItemDto>>> getPopularItems(Pageable pageable) {
List<PopularItemDto> popularItems = itemService.getPopularItems();
public ResponseEntity<SuccessResponse<List<ItemDetailDto>>> getPopularItems(Pageable pageable) {
List<ItemDetailDto> popularItems = itemService.getPopularItems(pageable);
return new ResponseEntity<>(
new SuccessResponse<>(HttpStatus.OK.value(),"인기 상품 리스트가 조회되었습니다.", popularItems),
HttpStatus.OK
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/kea/dpang/item/service/ItemService.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Page<ItemDetailDto> getItemList(
*
* @return 조회된 인기 상품 목록이 담긴 DTO 리스트
*/
List<PopularItemDto> getPopularItems();
List<ItemDetailDto> getPopularItems(Pageable pageable);

void incrementViewCount(Long itemId);

Expand Down
33 changes: 23 additions & 10 deletions src/main/java/kea/dpang/item/service/ItemServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Set;
Expand Down Expand Up @@ -85,23 +86,35 @@ public ItemDto getItemInfo(Long itemId) {
// 인기 상품 조회
@Override
@Transactional(readOnly = true)
public List<PopularItemDto> getPopularItems() {
public List<ItemDetailDto> getPopularItems(Pageable pageable) {
// Redis에서 조회수를 기반으로 인기 상품 ID와 점수를 가져옴
Set<ZSetOperations.TypedTuple<String>> items = redisTemplate.opsForZSet()
.reverseRangeWithScores(ITEM_VIEW_COUNT_KEY, 0, -1);
.reverseRangeWithScores(ITEM_VIEW_COUNT_KEY, pageable.getOffset(), pageable.getOffset() + pageable.getPageSize() - 1);

// 가져온 데이터를 PopularItemDto 리스트로 변환
return items.stream().map(item -> {
List<ItemDetailDto> popularItems = new ArrayList<>();
for (ZSetOperations.TypedTuple<String> item : items) {
Long itemId = Long.valueOf(item.getValue());
Double score = item.getScore();
String itemName = itemRepository.findById(itemId)
.map(Item::getName)
.orElseThrow(() -> new ItemNotFoundException(itemId));

return new PopularItemDto(itemId, itemName, score);
}).toList();
try {
Item foundItem = itemRepository.findById(itemId)
.orElseThrow(() -> new ItemNotFoundException(itemId));

// 판매자 이름을 가져옴
ResponseEntity<SuccessResponse<SellerDto>> sellerResponse = sellerServiceFeignClient.getSeller(foundItem.getSellerId());
SellerDto seller = sellerResponse.getBody().getData();
String sellerName = seller.getName();

popularItems.add(new ItemDetailDto(foundItem, sellerName));
} catch (ItemNotFoundException e) {
log.error("상품을 찾을 수 없습니다. 상품 ID: {}", itemId, e);
// 상품을 찾을 수 없는 경우, 다음 상품으로 넘어감.
continue;
}
}
return popularItems;
}


@Override
public void incrementViewCount(Long itemId) {
redisTemplate.opsForZSet().incrementScore(ITEM_VIEW_COUNT_KEY, String.valueOf(itemId), 1);
Expand Down

0 comments on commit d74e1d4

Please sign in to comment.