Skip to content

Commit

Permalink
Merge pull request #78 from kea-dpang/48-api-리팩토링
Browse files Browse the repository at this point in the history
Feat: Add inquiry reviewerName in getReviewList.
  • Loading branch information
oo-ni authored Feb 6, 2024
2 parents 20d637a + 39a09aa commit 2427aef
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
19 changes: 12 additions & 7 deletions src/main/java/kea/dpang/item/dto/review/ReviewDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);
}
}

8 changes: 8 additions & 0 deletions src/main/java/kea/dpang/item/entity/Item.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}

8 changes: 7 additions & 1 deletion src/main/java/kea/dpang/item/entity/Review.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -47,4 +47,10 @@ public static Review from(CreateReviewRequestDto dto, Item item) {
.rating(dto.getRating())
.build();
}

@PostPersist
@PostUpdate
public void updateItemAverageRating() {
this.item.updateAverageRating();
}
}
27 changes: 26 additions & 1 deletion src/main/java/kea/dpang/item/service/ItemServiceImpl.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
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;
import kea.dpang.item.entity.SubCategory;
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;
Expand All @@ -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;

Expand All @@ -39,16 +43,37 @@ public void createItem(CreateItemRequestDto dto) {
log.info("ItemCreateDto로부터 새로운 아이템 생성을 시작합니다 : {}", dto);

try {
// 판매처 정보를 찾는 부분
ResponseEntity<SuccessResponse<SellerDto>> 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<ItemDto> getItemList(List<Long> itemIds) {
log.info("item ID 리스트로부터 아이템 리스트 조회를 시작합니다 : {}", itemIds);
Expand Down
18 changes: 15 additions & 3 deletions src/main/java/kea/dpang/item/service/ReviewServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,29 @@ public void createReview(CreateReviewRequestDto dto) {

// 리뷰 저장
reviewRepository.save(review);
log.info("새로운 리뷰 등록 완료. 리뷰 ID: {}", review.getId());
log.info("새로운 리뷰 등록 완료. 리뷰 ID: {}", review.getReviewId());
}


// 상품별 리뷰 리스트 조회
@Override
@Transactional
public List<ReviewDto> getReviewList(Long itemId, Pageable pageable) {

// 사용자 서버로부터 사용자 정보 조회
ResponseEntity<SuccessResponse<UserDetailDto>> responseEntity = userServiceFeignClient.getReviewer(itemId);
// 사용자 이름 가져오기

String name = Optional.ofNullable(responseEntity.getBody())
.map(SuccessResponse::getData)
.map(UserDetailDto::getName)
.orElseThrow(() -> new RuntimeException("사용자 정보 조회에 실패하였습니다."));

Page<Review> reviews = reviewRepository.findByItemId(itemId, pageable);
return reviews.stream()
.map(ReviewDto::new)

// 리뷰 리스트를 PersonalReviewDto로 변환
return reviews.getContent().stream()
.map(review -> ReviewDto.of(review, name))
.toList();
}

Expand Down

0 comments on commit 2427aef

Please sign in to comment.