diff --git a/src/main/java/com/moabam/api/application/bug/BugMapper.java b/src/main/java/com/moabam/api/application/bug/BugMapper.java index 5099351f..ba01ac3e 100644 --- a/src/main/java/com/moabam/api/application/bug/BugMapper.java +++ b/src/main/java/com/moabam/api/application/bug/BugMapper.java @@ -1,6 +1,7 @@ package com.moabam.api.application.bug; import java.util.List; +import java.util.Optional; import com.moabam.api.domain.bug.Bug; import com.moabam.api.domain.bug.BugActionType; @@ -37,17 +38,19 @@ public static BugResponse toBugResponse(Bug bug) { } public static BugHistoryItemResponse toBugHistoryItemResponse(BugHistoryDto dto) { - BugHistoryItemResponse.PaymentResponse payment = BugHistoryItemResponse.PaymentResponse.builder() - .id(dto.payment().getId()) - .orderName(dto.payment().getOrder().getName()) - .discountAmount(dto.payment().getDiscountAmount()) - .totalAmount(dto.payment().getTotalAmount()) - .build(); + 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); return BugHistoryItemResponse.builder() .id(dto.id()) - .bugType(dto.bugType().name()) - .actionType(dto.actionType().name()) + .bugType(dto.bugType()) + .actionType(dto.actionType()) .quantity(dto.quantity()) .date(DateUtils.format(dto.createdAt())) .payment(payment) diff --git a/src/main/java/com/moabam/api/domain/bug/BugHistory.java b/src/main/java/com/moabam/api/domain/bug/BugHistory.java index a5bfea26..9c08f4b4 100644 --- a/src/main/java/com/moabam/api/domain/bug/BugHistory.java +++ b/src/main/java/com/moabam/api/domain/bug/BugHistory.java @@ -53,8 +53,9 @@ public class BugHistory extends BaseTimeEntity { private int quantity; @Builder - private BugHistory(Long memberId, BugType bugType, BugActionType actionType, int quantity) { + private BugHistory(Long memberId, Payment payment, BugType bugType, BugActionType actionType, int quantity) { this.memberId = requireNonNull(memberId); + this.payment = payment; this.bugType = requireNonNull(bugType); this.actionType = requireNonNull(actionType); this.quantity = validateQuantity(quantity); diff --git a/src/main/java/com/moabam/api/domain/bug/repository/BugHistorySearchRepository.java b/src/main/java/com/moabam/api/domain/bug/repository/BugHistorySearchRepository.java index 609ec878..5eb17a50 100644 --- a/src/main/java/com/moabam/api/domain/bug/repository/BugHistorySearchRepository.java +++ b/src/main/java/com/moabam/api/domain/bug/repository/BugHistorySearchRepository.java @@ -20,7 +20,7 @@ public class BugHistorySearchRepository { private final JPAQueryFactory jpaQueryFactory; public List findByMemberIdWithPayment(Long memberId) { - return jpaQueryFactory.select(Projections.fields( + return jpaQueryFactory.select(Projections.constructor( BugHistoryDto.class, bugHistory.id, bugHistory.bugType, diff --git a/src/main/java/com/moabam/api/dto/bug/BugHistoryItemResponse.java b/src/main/java/com/moabam/api/dto/bug/BugHistoryItemResponse.java index 8be12171..5bcefc63 100644 --- a/src/main/java/com/moabam/api/dto/bug/BugHistoryItemResponse.java +++ b/src/main/java/com/moabam/api/dto/bug/BugHistoryItemResponse.java @@ -3,14 +3,16 @@ import static com.fasterxml.jackson.annotation.JsonInclude.Include.*; import com.fasterxml.jackson.annotation.JsonInclude; +import com.moabam.api.domain.bug.BugActionType; +import com.moabam.api.domain.bug.BugType; import lombok.Builder; @Builder public record BugHistoryItemResponse( Long id, - String bugType, - String actionType, + BugType bugType, + BugActionType actionType, int quantity, String date, @JsonInclude(NON_NULL) PaymentResponse payment diff --git a/src/test/java/com/moabam/api/presentation/BugControllerTest.java b/src/test/java/com/moabam/api/presentation/BugControllerTest.java index 34025882..0a8dcb3e 100644 --- a/src/test/java/com/moabam/api/presentation/BugControllerTest.java +++ b/src/test/java/com/moabam/api/presentation/BugControllerTest.java @@ -3,6 +3,7 @@ import static com.moabam.global.auth.model.AuthorizationThreadLocal.*; import static com.moabam.support.fixture.BugFixture.*; import static com.moabam.support.fixture.MemberFixture.*; +import static com.moabam.support.fixture.PaymentFixture.*; import static com.moabam.support.fixture.ProductFixture.*; import static java.nio.charset.StandardCharsets.*; import static org.assertj.core.api.Assertions.*; @@ -15,6 +16,7 @@ import java.util.List; import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; @@ -27,10 +29,15 @@ import com.moabam.api.application.bug.BugMapper; import com.moabam.api.application.member.MemberService; import com.moabam.api.application.product.ProductMapper; +import com.moabam.api.domain.bug.BugActionType; +import com.moabam.api.domain.bug.BugType; import com.moabam.api.domain.bug.repository.BugHistoryRepository; import com.moabam.api.domain.bug.repository.BugHistorySearchRepository; +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.BugResponse; import com.moabam.api.dto.product.ProductsResponse; import com.moabam.api.dto.product.PurchaseProductRequest; @@ -61,6 +68,9 @@ class BugControllerTest extends WithoutFilterSupporter { @Autowired ProductRepository productRepository; + @Autowired + PaymentRepository paymentRepository; + @DisplayName("벌레를 조회한다.") @WithMember @Test @@ -82,6 +92,61 @@ void get_bug_success() throws Exception { assertThat(actual).isEqualTo(expected); } + @DisplayName("벌레 내역을 조회한다.") + @Nested + class GetBugHistory { + + @DisplayName("성공한다.") + @WithMember + @Test + void success() throws Exception { + // given + Long memberId = getAuthMember().id(); + bugHistoryRepository.save(rewardMorningBugHistory(memberId)); + + // expected + String content = mockMvc.perform(get("/bugs/history") + .contentType(APPLICATION_JSON)) + .andExpect(status().isOk()) + .andDo(print()) + .andReturn() + .getResponse() + .getContentAsString(UTF_8); + BugHistoryResponse actual = objectMapper.readValue(content, BugHistoryResponse.class); + assertThat(actual.history().get(0).bugType()).isEqualTo(BugType.MORNING); + assertThat(actual.history().get(0).actionType()).isEqualTo(BugActionType.REWARD); + assertThat(actual.history().get(0).quantity()).isEqualTo(REWARD_MORNING_BUG); + assertThat(actual.history().get(0).payment()).isNull(); + } + + @DisplayName("벌레 충전 내역인 경우 결제 정보를 포함한다.") + @WithMember + @Test + void charge_success() throws Exception { + // given + Long memberId = getAuthMember().id(); + Product product = productRepository.save(bugProduct()); + Payment payment = paymentRepository.save(payment(product)); + bugHistoryRepository.save(chargeGoldenBugHistory(memberId, payment)); + + // expected + String content = mockMvc.perform(get("/bugs/history") + .contentType(APPLICATION_JSON)) + .andExpect(status().isOk()) + .andDo(print()) + .andReturn() + .getResponse() + .getContentAsString(UTF_8); + BugHistoryResponse actual = objectMapper.readValue(content, BugHistoryResponse.class); + assertThat(actual.history().get(0).bugType()).isEqualTo(BugType.GOLDEN); + assertThat(actual.history().get(0).actionType()).isEqualTo(BugActionType.CHARGE); + assertThat(actual.history().get(0).quantity()).isEqualTo(BUG_PRODUCT_QUANTITY); + assertThat(actual.history().get(0).payment().orderName()).isEqualTo(BUG_PRODUCT_NAME); + assertThat(actual.history().get(0).payment().totalAmount()).isEqualTo(BUG_PRODUCT_PRICE); + assertThat(actual.history().get(0).payment().discountAmount()).isZero(); + } + } + @DisplayName("벌레 상품 목록을 조회한다.") @Test void get_bug_products_success() throws Exception { diff --git a/src/test/java/com/moabam/support/fixture/BugFixture.java b/src/test/java/com/moabam/support/fixture/BugFixture.java index a2584bcd..2215101b 100644 --- a/src/test/java/com/moabam/support/fixture/BugFixture.java +++ b/src/test/java/com/moabam/support/fixture/BugFixture.java @@ -1,12 +1,20 @@ package com.moabam.support.fixture; +import static com.moabam.support.fixture.ProductFixture.*; + 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.payment.Payment; public final class BugFixture { public static final int MORNING_BUG = 10; public static final int NIGHT_BUG = 20; public static final int GOLDEN_BUG = 30; + public static final int REWARD_MORNING_BUG = 3; + public static final int USE_NIGHT_BUG = 5; public static Bug bug() { return Bug.builder() @@ -15,4 +23,23 @@ public static Bug bug() { .goldenBug(GOLDEN_BUG) .build(); } + + public static BugHistory rewardMorningBugHistory(Long memberId) { + return BugHistory.builder() + .memberId(memberId) + .bugType(BugType.MORNING) + .actionType(BugActionType.REWARD) + .quantity(REWARD_MORNING_BUG) + .build(); + } + + public static BugHistory chargeGoldenBugHistory(Long memberId, Payment payment) { + return BugHistory.builder() + .memberId(memberId) + .payment(payment) + .bugType(BugType.GOLDEN) + .actionType(BugActionType.CHARGE) + .quantity(BUG_PRODUCT_QUANTITY) + .build(); + } }