Skip to content

Commit

Permalink
feat: 벌레 내역 조회 API 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
kmebin committed Nov 26, 2023
1 parent 0756c4d commit 2925f0f
Show file tree
Hide file tree
Showing 9 changed files with 159 additions and 24 deletions.
43 changes: 37 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,33 @@
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;

@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 +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<BugHistoryDto> dtoList) {
return BugHistoryResponse.builder()
.history(StreamUtils.map(dtoList, BugMapper::toBugHistoryItemResponse))
.build();
}
}
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 @@ -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;
Expand All @@ -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;

Expand All @@ -43,6 +47,12 @@ public BugResponse getBug(Long memberId) {
return BugMapper.toBugResponse(member.getBug());
}

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

return BugMapper.toBugHistoryResponse(history);
}

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

Expand Down
8 changes: 8 additions & 0 deletions 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 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.domain.item.repository.BugHistoryDto;
import com.querydsl.core.types.Projections;
import com.querydsl.jpa.impl.JPAQueryFactory;

import lombok.RequiredArgsConstructor;
Expand All @@ -21,20 +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<BugHistoryDto> 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()));
}
}
Original file line number Diff line number Diff line change
@@ -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

) {

}
29 changes: 29 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,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
) {

}
}
13 changes: 13 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,13 @@
package com.moabam.api.dto.bug;

import java.util.List;

import lombok.Builder;

@Builder
public record BugHistoryResponse(

List<BugHistoryItemResponse> history
) {

}
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);
}
}

0 comments on commit 2925f0f

Please sign in to comment.