Entity -> dto 변환을 어떻게 처리하고 계신가요?? #51
Answered
by
Aleexender
Kwonminwoo
asked this question in
Q&A
-
발생과정@Service
@RequiredArgsConstructor
public class ProductService {
private final ProductRepository productRepository;
public ProductFindResponse findProduct(Long productId) {
Product foundProduct = productRepository.findById(productId)
.orElseThrow(() -> new ProductNotFoundException(ErrorCode.PRODUCT_NOT_FOUND));
Reservation foundReservation = foundProduct.getReservation();
Hotel foundHotel = foundReservation.getHotel();
Room foundRoom = foundHotel.getRoom();
LocalDateTime checkInDateTime = LocalDateTime.of(foundReservation.getStartDate(),
foundRoom.getCheckIn());
LocalDateTime checkOutDateTime = LocalDateTime.of(foundReservation.getEndDate(),
foundRoom.getCheckOut());
int price = foundHotel.getHotelRoomPrice().getOffPeakPrice();
if (SeasonValidator.isPeakTime(LocalDate.now())) {
price = foundHotel.getHotelRoomPrice().getPeakPrice();
}
RoomTheme foundRoomTheme = foundRoom.getRoomTheme();
RoomThemeFindResponse roomThemeResponse = RoomThemeFindResponse.builder()
.parkingZone(foundRoomTheme.hasParkingZone())
.breakfast(foundRoomTheme.hasBreakfast())
.pool(foundRoomTheme.hasPool())
.oceanView(foundRoomTheme.hasOceanView())
.build();
return ProductFindResponse.builder()
.hotelName(foundHotel.getHotelName())
.roomName(foundRoom.getRoomName())
.checkIn(checkInDateTime)
.checkOut(checkOutDateTime)
.originalPrice(price)
.sellingPrice(foundReservation.getPurchasePrice())
.standardPeople(foundRoom.getStandardPeople())
.maxPeople(foundRoom.getMaxPeople())
.bedType(foundRoom.getBedType())
.roomTheme(roomThemeResponse)
.hotelAddress(foundHotel.getHotelDetailAddress())
.saleStatus(getSaleStatus(foundProduct))
.hotelInfoUrl(foundHotel.getHotelInfoUrl())
.build();
}
private boolean getSaleStatus(Product product) {
return product.getPaymentHistory() != null;
}
}위 코드는 제가 작성한 상품 상세 조회 서비스 로직입니다. 원인전체 코드 중 entity를 response dto로 변환하는 코드가 상당부분 차지하고 있어서 이 부분을 따로 빼고 싶습니다.
어떤 방식이 더 관심사를 잘 분리한 것 같을까요?? |
Beta Was this translation helpful? Give feedback.
Answered by
Aleexender
Jan 4, 2024
Replies: 2 comments 1 reply
-
|
저라면 2번인 컨버터 만드는게 좀더 좋은 방법 같아여! 둘 다 사실 좋은방법인거 같긴한데 저는 개인 취향상 컨버터를 더 선호하긴합니당! |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
Kwonminwoo
-
|
음 저는 지금가지 1번으로 사용해봤어요! 2번은 안사용해봤지만 다른분들이 추천하신데는 이유가 있을 것같아서 2번도 좋습니다 ~ |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
저라면 2번인 컨버터 만드는게 좀더 좋은 방법 같아여! 둘 다 사실 좋은방법인거 같긴한데 저는 개인 취향상 컨버터를 더 선호하긴합니당!