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 0d439458..9a3bb497 100644 --- a/src/main/java/com/moabam/api/application/bug/BugMapper.java +++ b/src/main/java/com/moabam/api/application/bug/BugMapper.java @@ -1,10 +1,17 @@ package com.moabam.api.application.bug; +import java.util.List; + 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.BugResponse; +import com.moabam.global.common.util.DateUtils; +import com.moabam.global.common.util.StreamUtils; import lombok.AccessLevel; import lombok.NoArgsConstructor; @@ -12,6 +19,15 @@ @NoArgsConstructor(access = AccessLevel.PRIVATE) public final class BugMapper { + public static BugHistory toUseBugHistory(Long memberId, BugType bugType, int quantity) { + return BugHistory.builder() + .memberId(memberId) + .bugType(bugType) + .actionType(BugActionType.USE) + .quantity(quantity) + .build(); + } + public static BugResponse toBugResponse(Bug bug) { return BugResponse.builder() .morningBug(bug.getMorningBug()) @@ -20,12 +36,27 @@ public static BugResponse toBugResponse(Bug bug) { .build(); } - public static BugHistory toUseBugHistory(Long memberId, BugType bugType, int quantity) { - return BugHistory.builder() - .memberId(memberId) - .bugType(bugType) - .actionType(BugActionType.USE) - .quantity(quantity) + public static BugHistoryItemResponse toBugHistoryItemResponse(BugHistoryDto dto) { + BugHistoryItemResponse.PaymentResponse payment = BugHistoryItemResponse.PaymentResponse.builder() + .id(dto.payment().getId()) + .orderName(dto.payment().getOrder().getName()) + .discountAmount(dto.payment().getCoupon().getPoint()) + .totalAmount(dto.payment().getAmount()) + .build(); + + return BugHistoryItemResponse.builder() + .id(dto.id()) + .bugType(dto.bugType().name()) + .actionType(dto.actionType().name()) + .quantity(dto.quantity()) + .date(DateUtils.format(dto.createdAt())) + .payment(payment) + .build(); + } + + public static BugHistoryResponse toBugHistoryResponse(List dtoList) { + return BugHistoryResponse.builder() + .history(StreamUtils.map(dtoList, BugMapper::toBugHistoryItemResponse)) .build(); } } diff --git a/src/main/java/com/moabam/api/application/bug/BugService.java b/src/main/java/com/moabam/api/application/bug/BugService.java index 46908ae4..3fb80281 100644 --- a/src/main/java/com/moabam/api/application/bug/BugService.java +++ b/src/main/java/com/moabam/api/application/bug/BugService.java @@ -13,12 +13,15 @@ import com.moabam.api.application.member.MemberService; import com.moabam.api.application.payment.PaymentMapper; import com.moabam.api.application.product.ProductMapper; +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.member.Member; 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; @@ -34,6 +37,7 @@ public class BugService { private final MemberService memberService; private final CouponService couponService; + private final BugHistorySearchRepository bugHistorySearchRepository; private final ProductRepository productRepository; private final PaymentRepository paymentRepository; @@ -43,6 +47,12 @@ public BugResponse getBug(Long memberId) { return BugMapper.toBugResponse(member.getBug()); } + public BugHistoryResponse getBugHistory(Long memberId) { + List history = bugHistorySearchRepository.findByMemberIdWithPayment(memberId); + + return BugMapper.toBugHistoryResponse(history); + } + public ProductsResponse getBugProducts() { List products = productRepository.findAllByType(BUG); 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 072a16ec..a5bfea26 100644 --- a/src/main/java/com/moabam/api/domain/bug/BugHistory.java +++ b/src/main/java/com/moabam/api/domain/bug/BugHistory.java @@ -3,6 +3,7 @@ import static com.moabam.global.error.model.ErrorMessage.*; import static java.util.Objects.*; +import com.moabam.api.domain.payment.Payment; import com.moabam.global.common.entity.BaseTimeEntity; import com.moabam.global.error.exception.BadRequestException; @@ -10,9 +11,12 @@ import jakarta.persistence.Entity; import jakarta.persistence.EnumType; import jakarta.persistence.Enumerated; +import jakarta.persistence.FetchType; import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; import jakarta.persistence.Id; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.ManyToOne; import jakarta.persistence.Table; import lombok.AccessLevel; import lombok.Builder; @@ -33,6 +37,10 @@ public class BugHistory extends BaseTimeEntity { @Column(name = "member_id", updatable = false, nullable = false) private Long memberId; + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "payment_id") + private Payment payment; + @Enumerated(value = EnumType.STRING) @Column(name = "bug_type", nullable = false) private BugType bugType; 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 26a8f9ff..609ec878 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 @@ -1,16 +1,14 @@ package com.moabam.api.domain.bug.repository; import static com.moabam.api.domain.bug.QBugHistory.*; +import static com.moabam.api.domain.payment.QPayment.*; -import java.time.LocalDateTime; import java.util.List; import org.springframework.stereotype.Repository; -import com.moabam.api.domain.bug.BugActionType; -import com.moabam.api.domain.bug.BugHistory; -import com.moabam.global.common.util.DynamicQuery; -import com.querydsl.core.types.dsl.BooleanExpression; +import com.moabam.api.domain.item.repository.BugHistoryDto; +import com.querydsl.core.types.Projections; import com.querydsl.jpa.impl.JPAQueryFactory; import lombok.RequiredArgsConstructor; @@ -21,20 +19,20 @@ public class BugHistorySearchRepository { private final JPAQueryFactory jpaQueryFactory; - public List find(Long memberId, BugActionType actionType, LocalDateTime dateTime) { - return jpaQueryFactory - .selectFrom(bugHistory) - .where( - DynamicQuery.generateEq(memberId, bugHistory.memberId::eq), - DynamicQuery.generateEq(actionType, bugHistory.actionType::eq), - DynamicQuery.generateEq(dateTime, this::equalDate) + public List findByMemberIdWithPayment(Long memberId) { + return jpaQueryFactory.select(Projections.fields( + BugHistoryDto.class, + bugHistory.id, + bugHistory.bugType, + bugHistory.actionType, + bugHistory.quantity, + bugHistory.createdAt, + payment) ) + .from(bugHistory) + .leftJoin(bugHistory.payment, payment) + .where(bugHistory.memberId.eq(memberId)) + .orderBy(bugHistory.createdAt.desc()) .fetch(); } - - private BooleanExpression equalDate(LocalDateTime dateTime) { - return bugHistory.createdAt.year().eq(dateTime.getYear()) - .and(bugHistory.createdAt.month().eq(dateTime.getMonthValue())) - .and(bugHistory.createdAt.dayOfMonth().eq(dateTime.getDayOfMonth())); - } } diff --git a/src/main/java/com/moabam/api/domain/item/repository/BugHistoryDto.java b/src/main/java/com/moabam/api/domain/item/repository/BugHistoryDto.java new file mode 100644 index 00000000..a6ae6101 --- /dev/null +++ b/src/main/java/com/moabam/api/domain/item/repository/BugHistoryDto.java @@ -0,0 +1,22 @@ +package com.moabam.api.domain.item.repository; + +import java.time.LocalDateTime; + +import com.moabam.api.domain.bug.BugActionType; +import com.moabam.api.domain.bug.BugType; +import com.moabam.api.domain.payment.Payment; + +import lombok.Builder; + +@Builder +public record BugHistoryDto( + Long id, + BugType bugType, + BugActionType actionType, + int quantity, + LocalDateTime createdAt, + Payment payment + +) { + +} diff --git a/src/main/java/com/moabam/api/dto/bug/BugHistoryItemResponse.java b/src/main/java/com/moabam/api/dto/bug/BugHistoryItemResponse.java new file mode 100644 index 00000000..8be12171 --- /dev/null +++ b/src/main/java/com/moabam/api/dto/bug/BugHistoryItemResponse.java @@ -0,0 +1,29 @@ +package com.moabam.api.dto.bug; + +import static com.fasterxml.jackson.annotation.JsonInclude.Include.*; + +import com.fasterxml.jackson.annotation.JsonInclude; + +import lombok.Builder; + +@Builder +public record BugHistoryItemResponse( + Long id, + String bugType, + String actionType, + int quantity, + String date, + @JsonInclude(NON_NULL) PaymentResponse payment + +) { + + @Builder + public record PaymentResponse( + Long id, + String orderName, + int discountAmount, + int totalAmount + ) { + + } +} diff --git a/src/main/java/com/moabam/api/dto/bug/BugHistoryResponse.java b/src/main/java/com/moabam/api/dto/bug/BugHistoryResponse.java new file mode 100644 index 00000000..ed2796d4 --- /dev/null +++ b/src/main/java/com/moabam/api/dto/bug/BugHistoryResponse.java @@ -0,0 +1,13 @@ +package com.moabam.api.dto.bug; + +import java.util.List; + +import lombok.Builder; + +@Builder +public record BugHistoryResponse( + + List history +) { + +} diff --git a/src/main/java/com/moabam/api/presentation/BugController.java b/src/main/java/com/moabam/api/presentation/BugController.java index 41a56009..51246958 100644 --- a/src/main/java/com/moabam/api/presentation/BugController.java +++ b/src/main/java/com/moabam/api/presentation/BugController.java @@ -10,6 +10,7 @@ import org.springframework.web.bind.annotation.RestController; import com.moabam.api.application.bug.BugService; +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; @@ -33,6 +34,12 @@ public BugResponse getBug(@Auth AuthMember member) { return bugService.getBug(member.id()); } + @GetMapping("/history") + @ResponseStatus(HttpStatus.OK) + public BugHistoryResponse getBugHistory(@Auth AuthMember member) { + return bugService.getBugHistory(member.id()); + } + @GetMapping("/products") @ResponseStatus(HttpStatus.OK) public ProductsResponse getBugProducts() { diff --git a/src/main/java/com/moabam/global/common/util/DateUtils.java b/src/main/java/com/moabam/global/common/util/DateUtils.java new file mode 100644 index 00000000..38fa97b4 --- /dev/null +++ b/src/main/java/com/moabam/global/common/util/DateUtils.java @@ -0,0 +1,17 @@ +package com.moabam.global.common.util; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; + +import lombok.AccessLevel; +import lombok.NoArgsConstructor; + +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public final class DateUtils { + + private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"); + + public static String format(LocalDateTime dateTime) { + return dateTime.format(formatter); + } +}