-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #196 from THEGOODs-repo/feature/184
[Feat/184] 리뷰 등록 API 구현
- Loading branch information
Showing
11 changed files
with
252 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
src/main/java/com/umc/TheGoods/apiPayload/exception/handler/ReviewHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.umc.TheGoods.apiPayload.exception.handler; | ||
|
||
import com.umc.TheGoods.apiPayload.code.BaseErrorCode; | ||
import com.umc.TheGoods.apiPayload.exception.GeneralException; | ||
|
||
public class ReviewHandler extends GeneralException { | ||
public ReviewHandler(BaseErrorCode code) { | ||
super(code); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
src/main/java/com/umc/TheGoods/converter/review/ReviewConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.umc.TheGoods.converter.review; | ||
|
||
import com.umc.TheGoods.domain.enums.ReviewStatus; | ||
import com.umc.TheGoods.domain.item.Review; | ||
import com.umc.TheGoods.domain.order.OrderItem; | ||
import com.umc.TheGoods.web.dto.review.ReviewResponseDTO; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
public class ReviewConverter { | ||
|
||
public static Review toReview(OrderItem orderItem, String text, Integer score) { | ||
return Review.builder() | ||
.text(text) | ||
.score(score) | ||
.orderItem(orderItem) | ||
.status(ReviewStatus.SHOW) | ||
.build(); | ||
} | ||
|
||
public static ReviewResponseDTO.reviewPostDTO toReviewPostDTO(Review review) { | ||
return ReviewResponseDTO.reviewPostDTO.builder() | ||
.reviewId(review.getId()) | ||
.createdAt(review.getCreatedAt()) | ||
.itemName(review.getItem().getName()) | ||
.score(review.getScore()) | ||
.text(review.getText()) | ||
.optionStringList(ReviewConverter.toOptionStringList(review.getOrderItem())) | ||
.build(); | ||
} | ||
|
||
private static List<String> toOptionStringList(OrderItem orderItem) { | ||
return orderItem.getOrderDetailList().stream().map(orderDetail -> { | ||
return orderDetail.getItemOption().getName(); | ||
}).collect(Collectors.toList()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
src/main/java/com/umc/TheGoods/repository/review/ReviewRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.umc.TheGoods.repository.review; | ||
|
||
import com.umc.TheGoods.domain.item.Review; | ||
import com.umc.TheGoods.domain.order.OrderItem; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface ReviewRepository extends JpaRepository<Review, Long> { | ||
|
||
boolean existsByOrderItem(OrderItem orderItem); | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/com/umc/TheGoods/service/ReviewService/ReviewCommandService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.umc.TheGoods.service.ReviewService; | ||
|
||
import com.umc.TheGoods.domain.item.Review; | ||
import com.umc.TheGoods.domain.member.Member; | ||
import com.umc.TheGoods.web.dto.review.ReviewRequestDTO; | ||
|
||
public interface ReviewCommandService { | ||
|
||
Review create(ReviewRequestDTO.addReviewDTO request, Member member); | ||
} |
55 changes: 55 additions & 0 deletions
55
src/main/java/com/umc/TheGoods/service/ReviewService/ReviewCommandServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package com.umc.TheGoods.service.ReviewService; | ||
|
||
import com.umc.TheGoods.apiPayload.code.status.ErrorStatus; | ||
import com.umc.TheGoods.apiPayload.exception.handler.OrderHandler; | ||
import com.umc.TheGoods.apiPayload.exception.handler.ReviewHandler; | ||
import com.umc.TheGoods.converter.review.ReviewConverter; | ||
import com.umc.TheGoods.domain.item.Review; | ||
import com.umc.TheGoods.domain.member.Member; | ||
import com.umc.TheGoods.domain.order.OrderItem; | ||
import com.umc.TheGoods.repository.order.OrderItemRepository; | ||
import com.umc.TheGoods.repository.review.ReviewRepository; | ||
import com.umc.TheGoods.web.dto.review.ReviewRequestDTO; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Slf4j | ||
@Service | ||
@RequiredArgsConstructor | ||
@Transactional | ||
public class ReviewCommandServiceImpl implements ReviewCommandService { | ||
|
||
private final OrderItemRepository orderItemRepository; | ||
private final ReviewRepository reviewRepository; | ||
|
||
/** | ||
* 리뷰 등록 | ||
* | ||
* @param request | ||
* @param member | ||
* @return | ||
*/ | ||
@Override | ||
public Review create(ReviewRequestDTO.addReviewDTO request, Member member) { | ||
// 해당 orderItem이 해당 회원의 것이 맞는지 검증 | ||
OrderItem orderItem = orderItemRepository.findById(request.getOrderItemId()).orElseThrow(() -> new OrderHandler(ErrorStatus.ORDER_ITEM_NOT_FOUND)); | ||
if (!orderItem.getOrders().getMember().equals(member)) { | ||
throw new ReviewHandler(ErrorStatus.NOT_ORDER_OWNER); | ||
} | ||
|
||
// 이미 리뷰를 등록했던 orderItem인지 검증 | ||
boolean isExists = reviewRepository.existsByOrderItem(orderItem); | ||
if (isExists) { | ||
throw new ReviewHandler(ErrorStatus.REVIEW_ALREADY_EXISTS); | ||
} | ||
|
||
// review 엔티티 생성 및 연관관계 매핑 | ||
Review review = ReviewConverter.toReview(orderItem, request.getText(), request.getScore()); | ||
review.setMember(member); | ||
review.setItem(orderItem.getItem()); | ||
|
||
return reviewRepository.save(review); | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
src/main/java/com/umc/TheGoods/web/controller/ReviewController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package com.umc.TheGoods.web.controller; | ||
|
||
import com.umc.TheGoods.apiPayload.ApiResponse; | ||
import com.umc.TheGoods.apiPayload.code.status.ErrorStatus; | ||
import com.umc.TheGoods.apiPayload.exception.handler.MemberHandler; | ||
import com.umc.TheGoods.converter.review.ReviewConverter; | ||
import com.umc.TheGoods.domain.item.Review; | ||
import com.umc.TheGoods.domain.member.Member; | ||
import com.umc.TheGoods.service.MemberService.MemberQueryService; | ||
import com.umc.TheGoods.service.ReviewService.ReviewCommandService; | ||
import com.umc.TheGoods.web.dto.review.ReviewRequestDTO; | ||
import com.umc.TheGoods.web.dto.review.ReviewResponseDTO; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.validation.annotation.Validated; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import javax.validation.Valid; | ||
|
||
@Slf4j | ||
@Tag(name = "Review", description = "리뷰 관련 API") | ||
@Validated | ||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/api/review") | ||
public class ReviewController { | ||
private final MemberQueryService memberQueryService; | ||
private final ReviewCommandService reviewCommandService; | ||
|
||
@PostMapping | ||
public ApiResponse<ReviewResponseDTO.reviewPostDTO> addReview(@RequestBody @Valid ReviewRequestDTO.addReviewDTO request, | ||
Authentication authentication) { | ||
// 비회원인 경우 처리 불가 | ||
if (authentication == null) { | ||
throw new MemberHandler(ErrorStatus._UNAUTHORIZED); | ||
} | ||
|
||
// request에서 member id 추출해 Member 엔티티 찾기 | ||
Member member = memberQueryService.findMemberById(Long.valueOf(authentication.getName().toString())).orElseThrow(() -> new MemberHandler(ErrorStatus.MEMBER_NOT_FOUND)); | ||
|
||
// 리뷰 등록 | ||
Review review = reviewCommandService.create(request, member); | ||
|
||
return ApiResponse.onSuccess(ReviewConverter.toReviewPostDTO(review)); | ||
} | ||
|
||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/com/umc/TheGoods/web/dto/review/ReviewRequestDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.umc.TheGoods.web.dto.review; | ||
|
||
import lombok.Getter; | ||
|
||
import javax.validation.constraints.Max; | ||
import javax.validation.constraints.Min; | ||
import javax.validation.constraints.NotNull; | ||
import javax.validation.constraints.Size; | ||
|
||
public class ReviewRequestDTO { | ||
|
||
@Getter | ||
public static class addReviewDTO { | ||
@NotNull | ||
@Min(1) | ||
@Max(5) | ||
Integer score; | ||
|
||
@NotNull | ||
Long orderItemId; | ||
|
||
@Size(max = 1500) | ||
String text; | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/main/java/com/umc/TheGoods/web/dto/review/ReviewResponseDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.umc.TheGoods.web.dto.review; | ||
|
||
import com.fasterxml.jackson.annotation.JsonInclude; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
|
||
public class ReviewResponseDTO { | ||
@Builder | ||
@Getter | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public static class reviewPostDTO { | ||
Long reviewId; | ||
LocalDateTime createdAt; | ||
String itemName; | ||
@JsonInclude(JsonInclude.Include.NON_NULL) | ||
List<String> optionStringList; | ||
Integer score; | ||
String text; | ||
} | ||
} |