ํ์ํ ์ด๋ฆ์ผ๋ก ์์ฑํ๋ค๋ณด๋ ํ์์ด ์ํ๋ Dto๋ช ๊ณผ ์ด๋ฏธ ๋ด๊ฐ ์์ฑํ Dto๋ช ์ด ๊ฒน์น๊ฒ ๋์๋ค. ์ผ๋จ ๋๋ ์ปจ๋ฒค์ ์์ด ๊ทธ๋ด๋ฏํ ์ด๋ฆ์ผ๋ก ์์ฑํ๋ค๋ณด๋ ๋ญ๊ฐ ๋ญ์ง ๋ชจ๋ฅด๋ ํ์๊น์ง ๋์ด๋ฒ๋ ธ๋ค...
์ด๋ฌํ ๋ฌธ์ ์ ์ ์ธ์ํ๊ณ ๊น๋ํ dto ์ปจ๋ฒค์ ์ ์์ฑํด์ผ๊ฒ ๋ค๊ณ ๋๊ผ๋ค.
Inner Class๋ฅผ ์ฌ์ฉํ์ฌ ๊น๋ํ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ ์ ์๋๋ก ํ์๋ค.
์ฐ๋ฆฌํ๋ผ๋ฆฌ์ ์ปจ๋ฒค์
์ ์ธ๋ถ ํด๋์ค๋ช
์ controller์ ๋ฉ์๋๋ช
์ ๋ฐ๋ผ๊ฐ๊ธฐ๋ก ํ๋ค.
๋ํ์ฌ Controller์์๋ Http๋ฅผ ์ฌ์ฉ, Service์์๋ dto, Repository์์๋ dtio๋ฅผ ์ฌ์ฉํ์๋ค. (dtio : DataTransformInfraObjcet -> ์ฐ๋ฆฌํ์์ ์ ํ ์์์ ์ ํ ์ด๋ฆ์ด๋๊น ์ด๊ฒ ๋ญ ๋ฃ๋ณด์ก? ๊ฐ์ ์คํด๋ ์ํ์ผ๋ฉด ์ข๊ฒ ๋ค.)
์ด๋ ๊ฒ ๊ณ์ธต๋ณ๋ก ์ด๋ฆ์ ๋๋ ์ด์ ๋ ์ด๋์์ ์ฌ์ฉํ๋ dto์ธ์ง ๋ฐ๋ก ์ ์ ์๋๋ก ํ๊ธฐ ์ํจ์ด๋ค.
@Getter
@NoArgsConstructor
public class OrderCancelRequest {
@NotNull
private String orderNo;
@NotNull
private String cancelReason;
@Builder
public OrderCancelRequest(String orderNo, String cancelReason) {
this.orderNo = orderNo;
this.cancelReason = cancelReason;
}
public CancelRequestDto toServiceRequest() {
return CancelRequestDto.builder()
.orderNo(orderNo)
.cancelReason(cancelReason)
.build();
}
}
@Getter
@NoArgsConstructor
public class OrderReturnResponse {
private List<ProductInfoResponse> productResponses;
private ReturnRefundInfoResponse returnRefundInfoResponse;
@Builder
private OrderReturnResponse(List<ProductInfoResponse> productResponses, ReturnRefundInfoResponse returnRefundInfoResponse) {
this.productResponses = productResponses;
this.returnRefundInfoResponse = returnRefundInfoResponse;
}
public static OrderReturnResponse of(ReturnResponseDto serviceDto) {
return OrderReturnResponse.builder()
.productResponses(
serviceDto.getProductInfoResponseDtos()
.stream()
.map(ProductInfoResponse::of)
.toList()
)
.returnRefundInfoResponse(
ReturnRefundInfoResponse.of(serviceDto.getReturnRefundInfoResponseDto())
)
.build();
}
}
์ด๋ ๋ ๊ฒ๋ค์ด Inner Class๋ฅผ ์ฌ์ฉํ์ฌ ๊น๋ํ ๋ง๋ค์๋ค.
public class ConfirmCancelReturnHttp {
public static class Request {
@NotNull
private String orderNo;
@NotNull
private String cancelReason;
@Builder
public Request(String orderNo, String cancelReason) {
this.orderNo = orderNo;
this.cancelReason = cancelReason;
}
public ConfirmCancelReturnDto.Request toServiceRequest() {
return ConfirmCancelReturnDto.Request.builder()
.orderNo(orderNo)
.cancelReason(cancelReason)
.build();
}
}
@Getter
@NoArgsConstructor
public static class Response {
private Long orderId;
private String orderNumber;
private Long totalPrice;
private LocalDateTime cancellationDate; // ์ฃผ๋ฌธ ์ทจ์ ์ผ์
private List<ProductResponse> cancelledItems;
private RefundInfo refundInfo;
@Builder
private Response(Long orderId, String orderNumber, Long totalPrice, LocalDateTime cancellationDate, RefundInfo refundInfo, List<ProductResponse> cancelledItems) {
this.orderId = orderId;
this.orderNumber = orderNumber;
this.totalPrice = totalPrice;
this.cancellationDate = cancellationDate;
this.refundInfo = refundInfo;
this.cancelledItems = cancelledItems;
}
public static Response of(ConfirmCancelReturnDto.Response serviceDto) {
return Response.builder()
.orderId(serviceDto.getOrderId())
.orderNumber(serviceDto.getOrderNumber())
.totalPrice(serviceDto.getTotalPrice())
.cancellationDate(serviceDto.getCancellationDate())
.refundInfo(serviceDto.getRefundInfo())
.cancelledItems(serviceDto.getCancelledItems())
.build();
}
}
}
์ด๋ ๊ฒ ํ๋๊น ๋ฌด์จ ๋ฉ์๋์์ ์ฌ์ฉํ๋์ง ์ฝ๊ฒ ์์์ฑ ์ ์๊ฒ ๋์๋ค.
Service ๊ณ์ธต์์ ์ฌ์ฉํ๋ dto๋ ๋ณด์.
@Getter
public class CancelRequestDto {
private String orderNo;
private String cancelReason;
@Builder
private CancelRequestDto(String orderNo, String cancelReason) {
this.orderNo = orderNo;
this.cancelReason = cancelReason;
}
}
@Getter
@NoArgsConstructor
public class CancelReturnResponseDto {
private Long orderId;
private String orderNumber;
private Long totalPrice;
private LocalDateTime cancellationDate; // ์ฃผ๋ฌธ ์ทจ์ ์ผ์
private List<ProductResponse> cancelledItems;
private RefundInfo refundInfo;
@Builder
private CancelReturnResponseDto(Long orderId, String orderNumber, Long totalPrice, LocalDateTime cancellationDate, RefundInfo refundInfo, List<ProductResponse> cancelledItems) {
this.orderId = orderId;
this.orderNumber = orderNumber;
this.totalPrice = totalPrice;
this.cancellationDate = cancellationDate;
this.refundInfo = refundInfo;
this.cancelledItems = cancelledItems;
}
public static CancelReturnResponseDto of(Order order, RefundDto refundDto, DateTimeHolder dateTimeHolder) {
return CancelReturnResponseDto.builder()
.orderId(order.getId())
.orderNumber(order.getOrderNo())
.totalPrice(order.getOrderDetails().stream()
.mapToLong(OrderDetail::totalAmount)
.sum()
)
.cancellationDate(dateTimeHolder.getUpdateTime(order))
.refundInfo(RefundInfo.of(refundDto))
.cancelledItems(
order.getOrderDetails().stream()
.map(orderDetail -> ProductResponse.of(orderDetail.getProduct(), orderDetail.getQuantity()))
.toList()
)
.build();
}
}
์ด๋ ๊ฒ ๋์ด์์๋ dto๋ค์ Inner Class๋ฅผ ์ฌ์ฉํ์ฌ ๊น๋ํ ๋ง๋ค์๋ค.
public class ConfirmCancelReturnDto {
@Getter
@NoArgsConstructor
public static class Request {
private String orderNo;
private String cancelReason;
@Builder
private Request(String orderNo, String cancelReason) {
this.orderNo = orderNo;
this.cancelReason = cancelReason;
}
}
@Getter
@NoArgsConstructor
public static class Response {
private Long orderId;
private String orderNo;
private Long totalPrice;
private LocalDateTime cancellationDate; // ์ฃผ๋ฌธ ์ทจ์ ์ผ์
private List<ProductInfo> cancelledItems;
private RefundInfo refundInfo;
@Builder
private Response(Long orderId, String orderNo, Long totalPrice, LocalDateTime cancellationDate, RefundInfo refundInfo, List<ProductInfo> cancelledItems) {
this.orderId = orderId;
this.orderNo = orderNo;
this.totalPrice = totalPrice;
this.cancellationDate = cancellationDate;
this.refundInfo = refundInfo;
this.cancelledItems = cancelledItems;
}
public static Response of(Order order, RefundDto refundDto, DateTimeHolder dateTimeHolder) {
return Response.builder()
.orderId(order.getId())
.orderNo(order.getOrderNo())
.totalPrice(
order.getOrderDetails()
.stream()
.mapToLong(OrderDetail::totalAmount)
.sum()
)
.cancellationDate(dateTimeHolder.getUpdateTime(order))
.refundInfo(RefundInfo.of(refundDto))
.cancelledItems(
order.getOrderDetails().stream()
.map(orderDetail -> ProductInfo.of(orderDetail.getProduct(), orderDetail.getQuantity()))
.toList()
)
.build();
}
}
@Getter
@NoArgsConstructor
public static class ProductInfo {
private Long productId;
private String productNo;
private String name;
private Long price;
private Long quantity;
@Builder
private ProductInfo(Long productId, String productNo, String name, Long price, Long quantity) {
this.productId = productId;
this.productNo = productNo;
this.name = name;
this.price = price;
this.quantity = quantity;
}
public static ProductInfo of(Product product, Long quantity) {
return ProductInfo.builder()
.productId(product.getId())
.name(product.getName())
.price(product.getPrice())
.quantity(quantity)
.productNo(product.getProductNo())
.build();
}
}
@Getter
@NoArgsConstructor
public static class RefundInfo {
private Long totalRefundAmount;
private String refundMethod;
private LocalDateTime refundProcessedAt; // ํ๋ถ ์ผ์
@Builder
public RefundInfo(Long totalRefundAmount, String refundMethod, LocalDateTime refundProcessedAt) {
this.totalRefundAmount = totalRefundAmount;
this.refundMethod = refundMethod;
this.refundProcessedAt = refundProcessedAt;
}
public static RefundInfo of(RefundDto refundDto) {
return RefundInfo.builder()
.totalRefundAmount(refundDto.getTotalRefundAmount())
.refundMethod(refundDto.getRefundMethod())
.refundProcessedAt(refundDto.getRefundProcessedAt())
.build();
}
}
}
์ด๋ ๊ฒ ๋ณ๊ฒฝํ๋ ์ด๋์์ ์ด๋ค ๋ฉ์๋์ ์ด๋ ๊ณ์ธต์์ ์ฌ์ฉํ๋ dto์ธ์ง ๋ฐ๋ก ์ ์ ์๊ฒ ๋์๋ค.
๋ชจ๋ ๋ฐ๊พผ ํ์ ๊ธฐ์กด๊ณผ ์ด๋ป๊ฒ ๋ฌ๋ผ์ก๋์ง ํ์ธํ์!
<Controller>
<Service>
์ด์ ๊ณผ ๋น๊ตํ์ ๋ ์ด๋ ๊ณ์ธต, ๋ฉ์๋์์ ์ฌ์ฉ๋๋์ง ์ฝ๊ฒ ์ ์ ์๊ฒ ๋์๋ค.
ps. ํ ์คํธ๊น์ง ์์ ํ๋๋ฐ ๋๋ฌด ํ๋ค์๋ค ํํ