diff --git a/src/main/java/kea/dpang/item/dto/review/PersonalReviewDto.java b/src/main/java/kea/dpang/item/dto/review/PersonalReviewDto.java index a7b4321..e418233 100644 --- a/src/main/java/kea/dpang/item/dto/review/PersonalReviewDto.java +++ b/src/main/java/kea/dpang/item/dto/review/PersonalReviewDto.java @@ -21,7 +21,7 @@ public class PersonalReviewDto { public static PersonalReviewDto of(Review review, String reviewerName) { return new PersonalReviewDto( - review.getId(), + review.getReviewId(), review.getReviewerId(), reviewerName, review.getCreatedTime(), diff --git a/src/main/java/kea/dpang/item/dto/review/ReviewDto.java b/src/main/java/kea/dpang/item/dto/review/ReviewDto.java index 068216b..a795623 100644 --- a/src/main/java/kea/dpang/item/dto/review/ReviewDto.java +++ b/src/main/java/kea/dpang/item/dto/review/ReviewDto.java @@ -9,20 +9,25 @@ @Data @AllArgsConstructor public class ReviewDto { + private Long reviewId; private Long reviewerId; + private String reviewerName; private LocalDateTime createdTime; private Long itemId; private String content; private Double rating; - public ReviewDto(Review review) { - this.reviewId = review.getId(); - this.reviewerId = review.getReviewerId(); - this.createdTime = review.getCreatedTime(); - this.itemId = review.getItem().getId(); - this.content = review.getContent(); - this.rating = review.getRating(); + public static ReviewDto of(Review review, String reviewerName) { + return new ReviewDto( + review.getReviewId(), + review.getReviewerId(), + reviewerName, + review.getCreatedTime(), + review.getItem().getId(), + review.getContent(), + review.getRating() + ); } } diff --git a/src/main/java/kea/dpang/item/entity/Item.java b/src/main/java/kea/dpang/item/entity/Item.java index d3fbd3d..eb3f432 100644 --- a/src/main/java/kea/dpang/item/entity/Item.java +++ b/src/main/java/kea/dpang/item/entity/Item.java @@ -99,5 +99,13 @@ public void decreaseStock(int quantity) { } this.stockQuantity -= quantity; } + + public void updateAverageRating() { + this.averageRating = (float) this.reviews.stream() + .mapToDouble(Review::getRating) + .average() + .orElse(0.0); + } + } diff --git a/src/main/java/kea/dpang/item/entity/Review.java b/src/main/java/kea/dpang/item/entity/Review.java index 01dddde..9fc3190 100644 --- a/src/main/java/kea/dpang/item/entity/Review.java +++ b/src/main/java/kea/dpang/item/entity/Review.java @@ -20,7 +20,7 @@ public class Review extends BaseEntity { @Id @Column(name = "item_review_id", nullable = false) @GeneratedValue(strategy = GenerationType.IDENTITY) - private Long id; + private Long reviewId; // 리뷰 작성자 ID @Column(name = "reviewer_id", nullable = false) @@ -47,4 +47,10 @@ public static Review from(CreateReviewRequestDto dto, Item item) { .rating(dto.getRating()) .build(); } + + @PostPersist + @PostUpdate + public void updateItemAverageRating() { + this.item.updateAverageRating(); + } } \ No newline at end of file diff --git a/src/main/java/kea/dpang/item/service/ItemServiceImpl.java b/src/main/java/kea/dpang/item/service/ItemServiceImpl.java index e601711..8291d14 100644 --- a/src/main/java/kea/dpang/item/service/ItemServiceImpl.java +++ b/src/main/java/kea/dpang/item/service/ItemServiceImpl.java @@ -1,5 +1,6 @@ package kea.dpang.item.service; +import kea.dpang.item.base.SuccessResponse; import kea.dpang.item.dto.item.*; import kea.dpang.item.entity.Category; import kea.dpang.item.entity.Item; @@ -7,6 +8,7 @@ import kea.dpang.item.exception.ItemNotFoundException; import kea.dpang.item.exception.SellerNotFoundException; import kea.dpang.item.feign.SellerServiceFeignClient; +import kea.dpang.item.feign.dto.SellerDto; import kea.dpang.item.repository.ItemRepository; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -15,6 +17,8 @@ import org.springframework.data.jpa.domain.Specification; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.core.ZSetOperations; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -39,16 +43,37 @@ public void createItem(CreateItemRequestDto dto) { log.info("ItemCreateDto로부터 새로운 아이템 생성을 시작합니다 : {}", dto); try { + // 판매처 정보를 찾는 부분 + ResponseEntity> response = sellerServiceFeignClient.getSeller(dto.getSellerId()); + + // 판매처 정보가 없을 경우 SellerNotFoundException 발생 + if (response.getStatusCode() != HttpStatus.OK || response.getBody() == null || response.getBody().getData() == null) { + throw new SellerNotFoundException(dto.getSellerId()); + } + Item item = dto.toItem(); itemRepository.save(item); log.info("성공적으로 아이템이 생성되었습니다. 생성된 아이템의 ID는 : {}", item.getId()); - } catch (Exception e) { log.error("ItemCreateDto로부터 아이템 생성에 실패하였습니다. DTO 정보 : {}", dto, e); throw e; } } +// public void createItem(CreateItemRequestDto dto) { +// log.info("ItemCreateDto로부터 새로운 아이템 생성을 시작합니다 : {}", dto); +// +// try { +// Item item = dto.toItem(); +// itemRepository.save(item); +// log.info("성공적으로 아이템이 생성되었습니다. 생성된 아이템의 ID는 : {}", item.getId()); +// +// } catch (Exception e) { +// log.error("ItemCreateDto로부터 아이템 생성에 실패하였습니다. DTO 정보 : {}", dto, e); +// throw e; +// } +// } + @Override public List getItemList(List itemIds) { log.info("item ID 리스트로부터 아이템 리스트 조회를 시작합니다 : {}", itemIds); diff --git a/src/main/java/kea/dpang/item/service/ReviewServiceImpl.java b/src/main/java/kea/dpang/item/service/ReviewServiceImpl.java index d406506..dfc6c05 100644 --- a/src/main/java/kea/dpang/item/service/ReviewServiceImpl.java +++ b/src/main/java/kea/dpang/item/service/ReviewServiceImpl.java @@ -44,7 +44,7 @@ public void createReview(CreateReviewRequestDto dto) { // 리뷰 저장 reviewRepository.save(review); - log.info("새로운 리뷰 등록 완료. 리뷰 ID: {}", review.getId()); + log.info("새로운 리뷰 등록 완료. 리뷰 ID: {}", review.getReviewId()); } @@ -52,9 +52,21 @@ public void createReview(CreateReviewRequestDto dto) { @Override @Transactional public List getReviewList(Long itemId, Pageable pageable) { + + // 사용자 서버로부터 사용자 정보 조회 + ResponseEntity> responseEntity = userServiceFeignClient.getReviewer(itemId); + // 사용자 이름 가져오기 + + String name = Optional.ofNullable(responseEntity.getBody()) + .map(SuccessResponse::getData) + .map(UserDetailDto::getName) + .orElseThrow(() -> new RuntimeException("사용자 정보 조회에 실패하였습니다.")); + Page reviews = reviewRepository.findByItemId(itemId, pageable); - return reviews.stream() - .map(ReviewDto::new) + + // 리뷰 리스트를 PersonalReviewDto로 변환 + return reviews.getContent().stream() + .map(review -> ReviewDto.of(review, name)) .toList(); }