Skip to content

Commit

Permalink
feat: 벌레 내역 조회 기능 구현 (#155)
Browse files Browse the repository at this point in the history
* feat: 벌레 내역 조회 API 구현

* refactor: 결제 테이블 coupon_id 컬럼을 discount_amount로 변경

* test: 벌레 내역 조회 컨트롤러 통합 테스트

* fix: 테스트 오류 수정

* chore: 사용하지 않는 메서드 제거

* refactor: Response 분리

* style: 줄바꿈 제거
  • Loading branch information
kmebin authored Nov 27, 2023
1 parent 99e9afe commit cff2586
Show file tree
Hide file tree
Showing 16 changed files with 267 additions and 27 deletions.
37 changes: 31 additions & 6 deletions src/main/java/com/moabam/api/application/bug/BugMapper.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,34 @@
package com.moabam.api.application.bug;

import java.util.List;

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.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;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;

@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())
Expand All @@ -20,12 +37,20 @@ 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(BugHistoryWithPayment dto) {
return BugHistoryItemResponse.builder()
.id(dto.id())
.bugType(dto.bugType())
.actionType(dto.actionType())
.quantity(dto.quantity())
.date(DateUtils.format(dto.createdAt()))
.payment(PaymentMapper.toPaymentResponse(dto.payment()))
.build();
}

public static BugHistoryResponse toBugHistoryResponse(List<BugHistoryWithPayment> dtoList) {
return BugHistoryResponse.builder()
.history(StreamUtils.map(dtoList, BugMapper::toBugHistoryItemResponse))
.build();
}

Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/moabam/api/application/bug/BugService.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@
import com.moabam.api.application.product.ProductMapper;
import com.moabam.api.domain.bug.Bug;
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.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 All @@ -36,6 +39,7 @@ public class BugService {
private final MemberService memberService;
private final CouponService couponService;
private final BugHistoryRepository bugHistoryRepository;
private final BugHistorySearchRepository bugHistorySearchRepository;
private final ProductRepository productRepository;
private final PaymentRepository paymentRepository;

Expand All @@ -45,6 +49,12 @@ public BugResponse getBug(Long memberId) {
return BugMapper.toBugResponse(bug);
}

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

return BugMapper.toBugHistoryResponse(history);
}

public ProductsResponse getBugProducts() {
List<Product> products = productRepository.findAllByType(BUG);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import com.moabam.api.domain.coupon.CouponWallet;
import com.moabam.api.domain.coupon.repository.CouponRepository;
import com.moabam.api.domain.coupon.repository.CouponSearchRepository;
import com.moabam.api.domain.coupon.repository.CouponWalletRepository;
import com.moabam.api.domain.coupon.repository.CouponWalletSearchRepository;
import com.moabam.api.domain.member.Role;
import com.moabam.api.dto.coupon.CouponResponse;
Expand All @@ -35,7 +34,6 @@ public class CouponService {
private final CouponManageService couponManageService;
private final CouponRepository couponRepository;
private final CouponSearchRepository couponSearchRepository;
private final CouponWalletRepository couponWalletRepository;
private final CouponWalletSearchRepository couponWalletSearchRepository;

@Transactional
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);
}
}
11 changes: 10 additions & 1 deletion src/main/java/com/moabam/api/domain/bug/BugHistory.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,20 @@
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;

import jakarta.persistence.Column;
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;
Expand All @@ -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;
Expand All @@ -45,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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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.dto.bug.BugHistoryWithPayment;
import com.querydsl.core.types.Projections;
import com.querydsl.jpa.impl.JPAQueryFactory;

import lombok.RequiredArgsConstructor;
Expand All @@ -21,19 +19,20 @@ public class BugHistorySearchRepository {

private final JPAQueryFactory jpaQueryFactory;

public List<BugHistory> 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<BugHistoryWithPayment> findByMemberIdWithPayment(Long memberId) {
return jpaQueryFactory.select(Projections.constructor(
BugHistoryWithPayment.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()));
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.moabam.api.domain.coupon.repository;

import java.util.Optional;

import org.springframework.data.jpa.repository.JpaRepository;

import com.moabam.api.domain.coupon.CouponWallet;

public interface CouponWalletRepository extends JpaRepository<CouponWallet, Long> {

Optional<CouponWallet> findByIdAndMemberId(Long id, Long memberId);
}
22 changes: 22 additions & 0 deletions src/main/java/com/moabam/api/dto/bug/BugHistoryItemResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.moabam.api.dto.bug;

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 com.moabam.api.dto.payment.PaymentResponse;

import lombok.Builder;

@Builder
public record BugHistoryItemResponse(
Long id,
BugType bugType,
BugActionType actionType,
int quantity,
String date,
@JsonInclude(NON_NULL) PaymentResponse payment
) {

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

import java.util.List;

import lombok.Builder;

@Builder
public record BugHistoryResponse(
List<BugHistoryItemResponse> history
) {

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

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 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
) {

}
7 changes: 7 additions & 0 deletions src/main/java/com/moabam/api/presentation/BugController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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() {
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/com/moabam/global/common/util/DateUtils.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
1 change: 0 additions & 1 deletion src/test/java/com/moabam/api/domain/bug/BugTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,5 @@ void increase_bug_success() {
// then
assertThat(bug.getMorningBug()).isEqualTo(MORNING_BUG + 5);
assertThat(bug.getNightBug()).isEqualTo(NIGHT_BUG + 5);
assertThat(bug.getGoldenBug()).isEqualTo(GOLDEN_BUG + 5);
}
}
Loading

0 comments on commit cff2586

Please sign in to comment.