๐Ÿ“Œ [MarketBridge] 07. ๊ฐœ๋ฐœ์ผ์ง€ - DTO ๊ตฌ์กฐ๋ฅผ ๊ฐœ์„ ํ•ด๋ณด์ž!

๋ชจ๊น…ยท2024๋…„ 2์›” 2์ผ
0

๊ฐœ๋ฐœ์ผ์ง€ - MarketBridge

๋ชฉ๋ก ๋ณด๊ธฐ
7/8
post-thumbnail
post-custom-banner

๐Ÿ“– DTO ๊ตฌ์กฐ์— ๋Œ€ํ•œ ๋ฌธ์ œ์  ์ธ์‹

ํ•„์š”ํ•œ ์ด๋ฆ„์œผ๋กœ ์ž‘์„ฑํ•˜๋‹ค๋ณด๋‹ˆ ํŒ€์›์ด ์›ํ•˜๋Š” 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. ํ…Œ์ŠคํŠธ๊นŒ์ง€ ์ˆ˜์ •ํ•˜๋Š”๋ฐ ๋„ˆ๋ฌด ํž˜๋“ค์—ˆ๋‹ค ํ‘ํ‘

profile
๋ฉˆ์ถ”์ง€ ์•Š๊ธฐ
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€