Skip to content

Commit

Permalink
refactor: Response 분리
Browse files Browse the repository at this point in the history
  • Loading branch information
kmebin committed Nov 27, 2023
1 parent 2cba6f2 commit fc07a7f
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 32 deletions.
19 changes: 5 additions & 14 deletions src/main/java/com/moabam/api/application/bug/BugMapper.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package com.moabam.api.application.bug;

import java.util.List;
import java.util.Optional;

import com.moabam.api.application.payment.PaymentMapper;
import com.moabam.api.domain.bug.Bug;
import com.moabam.api.domain.bug.BugActionType;
import com.moabam.api.domain.bug.BugHistory;
import com.moabam.api.domain.bug.BugType;
import com.moabam.api.domain.item.repository.BugHistoryDto;
import com.moabam.api.dto.bug.BugHistoryItemResponse;
import com.moabam.api.dto.bug.BugHistoryResponse;
import com.moabam.api.dto.bug.BugHistoryWithPayment;
import com.moabam.api.dto.bug.BugResponse;
import com.moabam.global.common.util.DateUtils;
import com.moabam.global.common.util.StreamUtils;
Expand Down Expand Up @@ -37,27 +37,18 @@ public static BugResponse toBugResponse(Bug bug) {
.build();
}

public static BugHistoryItemResponse toBugHistoryItemResponse(BugHistoryDto dto) {
BugHistoryItemResponse.PaymentResponse payment = Optional.ofNullable(dto.payment())
.map(p -> BugHistoryItemResponse.PaymentResponse.builder()
.id(p.getId())
.orderName(p.getOrder().getName())
.discountAmount(p.getDiscountAmount())
.totalAmount(p.getTotalAmount())
.build())
.orElse(null);

public static BugHistoryItemResponse toBugHistoryItemResponse(BugHistoryWithPayment dto) {
return BugHistoryItemResponse.builder()
.id(dto.id())
.bugType(dto.bugType())
.actionType(dto.actionType())
.quantity(dto.quantity())
.date(DateUtils.format(dto.createdAt()))
.payment(payment)
.payment(PaymentMapper.toPaymentResponse(dto.payment()))
.build();
}

public static BugHistoryResponse toBugHistoryResponse(List<BugHistoryDto> dtoList) {
public static BugHistoryResponse toBugHistoryResponse(List<BugHistoryWithPayment> dtoList) {
return BugHistoryResponse.builder()
.history(StreamUtils.map(dtoList, BugMapper::toBugHistoryItemResponse))
.build();
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/moabam/api/application/bug/BugService.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
import com.moabam.api.domain.bug.repository.BugHistoryRepository;
import com.moabam.api.domain.bug.repository.BugHistorySearchRepository;
import com.moabam.api.domain.coupon.Coupon;
import com.moabam.api.domain.item.repository.BugHistoryDto;
import com.moabam.api.domain.payment.Payment;
import com.moabam.api.domain.payment.repository.PaymentRepository;
import com.moabam.api.domain.product.Product;
import com.moabam.api.domain.product.repository.ProductRepository;
import com.moabam.api.dto.bug.BugHistoryResponse;
import com.moabam.api.dto.bug.BugHistoryWithPayment;
import com.moabam.api.dto.bug.BugResponse;
import com.moabam.api.dto.product.ProductsResponse;
import com.moabam.api.dto.product.PurchaseProductRequest;
Expand Down Expand Up @@ -50,7 +50,7 @@ public BugResponse getBug(Long memberId) {
}

public BugHistoryResponse getBugHistory(Long memberId) {
List<BugHistoryDto> history = bugHistorySearchRepository.findByMemberIdWithPayment(memberId);
List<BugHistoryWithPayment> history = bugHistorySearchRepository.findByMemberIdWithPayment(memberId);

return BugMapper.toBugHistoryResponse(history);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.moabam.api.application.payment;

import java.util.Optional;

import com.moabam.api.domain.payment.Order;
import com.moabam.api.domain.payment.Payment;
import com.moabam.api.domain.product.Product;
import com.moabam.api.dto.payment.PaymentResponse;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
Expand All @@ -22,4 +25,15 @@ public static Payment toPayment(Long memberId, Product product) {
.totalAmount(product.getPrice())
.build();
}

public static PaymentResponse toPaymentResponse(Payment payment) {
return Optional.ofNullable(payment)
.map(p -> PaymentResponse.builder()
.id(p.getId())
.orderName(p.getOrder().getName())
.discountAmount(p.getDiscountAmount())
.totalAmount(p.getTotalAmount())
.build())
.orElse(null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import org.springframework.stereotype.Repository;

import com.moabam.api.domain.item.repository.BugHistoryDto;
import com.moabam.api.dto.bug.BugHistoryWithPayment;
import com.querydsl.core.types.Projections;
import com.querydsl.jpa.impl.JPAQueryFactory;

Expand All @@ -19,9 +19,9 @@ public class BugHistorySearchRepository {

private final JPAQueryFactory jpaQueryFactory;

public List<BugHistoryDto> findByMemberIdWithPayment(Long memberId) {
public List<BugHistoryWithPayment> findByMemberIdWithPayment(Long memberId) {
return jpaQueryFactory.select(Projections.constructor(
BugHistoryDto.class,
BugHistoryWithPayment.class,
bugHistory.id,
bugHistory.bugType,
bugHistory.actionType,
Expand Down
11 changes: 1 addition & 10 deletions src/main/java/com/moabam/api/dto/bug/BugHistoryItemResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.fasterxml.jackson.annotation.JsonInclude;
import com.moabam.api.domain.bug.BugActionType;
import com.moabam.api.domain.bug.BugType;
import com.moabam.api.dto.payment.PaymentResponse;

import lombok.Builder;

Expand All @@ -16,16 +17,6 @@ public record BugHistoryItemResponse(
int quantity,
String date,
@JsonInclude(NON_NULL) PaymentResponse payment

) {

@Builder
public record PaymentResponse(
Long id,
String orderName,
int discountAmount,
int totalAmount
) {

}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.moabam.api.domain.item.repository;
package com.moabam.api.dto.bug;

import java.time.LocalDateTime;

Expand All @@ -9,14 +9,13 @@
import lombok.Builder;

@Builder
public record BugHistoryDto(
public record BugHistoryWithPayment(
Long id,
BugType bugType,
BugActionType actionType,
int quantity,
LocalDateTime createdAt,
Payment payment

) {

}
13 changes: 13 additions & 0 deletions src/main/java/com/moabam/api/dto/payment/PaymentResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.moabam.api.dto.payment;

import lombok.Builder;

@Builder
public record PaymentResponse(
Long id,
String orderName,
int discountAmount,
int totalAmount
) {

}

0 comments on commit fc07a7f

Please sign in to comment.