Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: 사용하지 않는 API 제거 및 코드 스타일 수정 #129

Merged
merged 2 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions src/main/java/com/moabam/api/application/bug/BugMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.moabam.api.domain.bug.BugHistory;
import com.moabam.api.domain.bug.BugType;
import com.moabam.api.dto.bug.BugResponse;
import com.moabam.api.dto.bug.TodayBugResponse;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
Expand All @@ -21,13 +20,6 @@ public static BugResponse toBugResponse(Bug bug) {
.build();
}

public static TodayBugResponse toTodayBugResponse(int morningBug, int nightBug) {
return TodayBugResponse.builder()
.morningBug(morningBug)
.nightBug(nightBug)
.build();
}

public static BugHistory toUseBugHistory(Long memberId, BugType bugType, int quantity) {
return BugHistory.builder()
.memberId(memberId)
Expand Down
26 changes: 1 addition & 25 deletions src/main/java/com/moabam/api/application/bug/BugService.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.moabam.api.application.bug;

import static com.moabam.api.domain.bug.BugActionType.*;
import static com.moabam.api.domain.bug.BugType.*;
import static com.moabam.api.domain.product.ProductType.*;
import static com.moabam.global.error.model.ErrorMessage.*;
import static java.util.Objects.*;
Expand All @@ -14,9 +12,6 @@
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.BugHistory;
import com.moabam.api.domain.bug.BugType;
import com.moabam.api.domain.bug.repository.BugHistorySearchRepository;
import com.moabam.api.domain.coupon.Coupon;
import com.moabam.api.domain.coupon.repository.CouponRepository;
import com.moabam.api.domain.member.Member;
Expand All @@ -25,11 +20,9 @@
import com.moabam.api.domain.product.Product;
import com.moabam.api.domain.product.repository.ProductRepository;
import com.moabam.api.dto.bug.BugResponse;
import com.moabam.api.dto.bug.TodayBugResponse;
import com.moabam.api.dto.product.ProductsResponse;
import com.moabam.api.dto.product.PurchaseProductRequest;
import com.moabam.api.dto.product.PurchaseProductResponse;
import com.moabam.global.common.util.ClockHolder;
import com.moabam.global.error.exception.NotFoundException;

import lombok.RequiredArgsConstructor;
Expand All @@ -40,26 +33,16 @@
public class BugService {

private final MemberService memberService;
private final BugHistorySearchRepository bugHistorySearchRepository;
private final ProductRepository productRepository;
private final PaymentRepository paymentRepository;
private final CouponRepository couponRepository;
private final ClockHolder clockHolder;

public BugResponse getBug(Long memberId) {
Member member = memberService.getById(memberId);

return BugMapper.toBugResponse(member.getBug());
}

public TodayBugResponse getTodayBug(Long memberId) {
List<BugHistory> todayRewardBug = bugHistorySearchRepository.find(memberId, REWARD, clockHolder.times());
int morningBug = calculateBugQuantity(todayRewardBug, MORNING);
int nightBug = calculateBugQuantity(todayRewardBug, NIGHT);

return BugMapper.toTodayBugResponse(morningBug, nightBug);
}

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

Expand All @@ -69,7 +52,7 @@ public ProductsResponse getBugProducts() {
@Transactional
public PurchaseProductResponse purchaseBugProduct(Long memberId, Long productId, PurchaseProductRequest request) {
Product product = getById(productId);
Payment payment = PaymentMapper.toEntity(memberId, product);
Payment payment = PaymentMapper.toPayment(memberId, product);

if (!isNull(request.couponId())) {
// TODO: (임시) CouponWallet 에 존재하는 할인 쿠폰인지 확인 @홍혁준
Expand All @@ -82,13 +65,6 @@ public PurchaseProductResponse purchaseBugProduct(Long memberId, Long productId,
return ProductMapper.toPurchaseProductResponse(payment);
}

private int calculateBugQuantity(List<BugHistory> bugHistory, BugType bugType) {
return bugHistory.stream()
.filter(history -> bugType.equals(history.getBugType()))
.mapToInt(BugHistory::getQuantity)
.sum();
}

private Product getById(Long productId) {
return productRepository.findById(productId)
.orElseThrow(() -> new NotFoundException(PRODUCT_NOT_FOUND));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class PaymentMapper {

public static Payment toEntity(Long memberId, Product product) {
public static Payment toPayment(Long memberId, Product product) {
Order order = Order.builder()
.name(product.getName())
.amount(product.getPrice())
.build();

return Payment.builder()
.memberId(memberId)
.product(product)
.order(order)
.amount(product.getPrice())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ public class PaymentService {
private final PaymentRepository paymentRepository;

@Transactional
public void requestPayment(Long memberId, Long paymentId, PaymentRequest request) {
Payment payment = getPayment(paymentId);
public void request(Long memberId, Long paymentId, PaymentRequest request) {
Payment payment = getById(paymentId);
payment.validateByMember(memberId);
payment.request(request.orderId());
}

private Payment getPayment(Long paymentId) {
private Payment getById(Long paymentId) {
return paymentRepository.findById(paymentId)
.orElseThrow(() -> new NotFoundException(PAYMENT_NOT_FOUND));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static PurchaseProductResponse toPurchaseProductResponse(Payment payment)
return PurchaseProductResponse.builder()
.paymentId(payment.getId())
.orderName(payment.getOrder().getName())
.price(payment.getOrder().getAmount())
.price(payment.getAmount())
.build();
}
}
25 changes: 1 addition & 24 deletions src/main/java/com/moabam/api/domain/payment/Order.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
package com.moabam.api.domain.payment;

import static com.moabam.global.error.model.ErrorMessage.*;
import static java.lang.Math.*;
import static java.util.Objects.*;

import com.moabam.global.error.exception.BadRequestException;

import jakarta.persistence.Column;
import jakarta.persistence.Embeddable;
import lombok.AccessLevel;
Expand All @@ -18,34 +14,15 @@
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Order {

private static final int MIN_AMOUNT = 0;

@Column(name = "order_id")
private String id;

@Column(name = "order_name", nullable = false)
private String name;

@Column(name = "amount", nullable = false)
private int amount;

@Builder
private Order(String id, String name, int amount) {
this.id = id;
private Order(String name) {
this.name = requireNonNull(name);
this.amount = validateAmount(amount);
}

private int validateAmount(int amount) {
if (amount < MIN_AMOUNT) {
throw new BadRequestException(INVALID_ORDER_AMOUNT);
}

return amount;
}

public void discountAmount(int price) {
this.amount = max(MIN_AMOUNT, amount - price);
}

public void updateId(String id) {
Expand Down
46 changes: 37 additions & 9 deletions src/main/java/com/moabam/api/domain/payment/Payment.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
package com.moabam.api.domain.payment;

import static com.moabam.global.error.model.ErrorMessage.*;
import static java.lang.Math.*;
import static java.util.Objects.*;

import java.time.LocalDateTime;

import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import com.moabam.api.domain.coupon.Coupon;
import com.moabam.api.domain.product.Product;
import com.moabam.global.common.entity.BaseTimeEntity;
import com.moabam.global.error.exception.BadRequestException;

import jakarta.persistence.Column;
import jakarta.persistence.Embedded;
import jakarta.persistence.Entity;
import jakarta.persistence.EntityListeners;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.FetchType;
Expand All @@ -29,7 +35,10 @@
@Getter
@Table(name = "payment")
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Payment extends BaseTimeEntity {
@EntityListeners(AuditingEntityListener.class)
public class Payment {

private static final int MIN_AMOUNT = 0;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand All @@ -50,27 +59,41 @@ public class Payment extends BaseTimeEntity {
@Embedded
private Order order;

@Column(name = "amount", nullable = false)
private int amount;

@Column(name = "payment_key")
private String paymentKey;

@Enumerated(value = EnumType.STRING)
@Column(name = "status", nullable = false)
private PaymentStatus status;

@CreatedDate
@Column(name = "created_at", updatable = false, nullable = false)
private LocalDateTime createdAt;

@Column(name = "requested_at")
private LocalDateTime requestedAt;

@Column(name = "approved_at")
private LocalDateTime approvedAt;

@Builder
public Payment(Long memberId, Product product, Coupon coupon, Order order, String paymentKey,
PaymentStatus status) {
public Payment(Long memberId, Product product, Order order, int amount, PaymentStatus status) {
this.memberId = requireNonNull(memberId);
this.product = requireNonNull(product);
this.coupon = coupon;
this.order = requireNonNull(order);
this.paymentKey = paymentKey;
this.amount = validateAmount(amount);
this.status = requireNonNullElse(status, PaymentStatus.PENDING);
}

public void applyCoupon(Coupon coupon) {
this.order.discountAmount(coupon.getPoint());
this.coupon = coupon;
private int validateAmount(int amount) {
if (amount < MIN_AMOUNT) {
throw new BadRequestException(INVALID_PAYMENT_AMOUNT);
}

return amount;
}

public void validateByMember(Long memberId) {
Expand All @@ -79,6 +102,11 @@ public void validateByMember(Long memberId) {
}
}

public void applyCoupon(Coupon coupon) {
this.coupon = coupon;
this.amount = max(MIN_AMOUNT, amount - coupon.getPoint());
}

public void request(String orderId) {
this.order.updateId(orderId);
this.status = PaymentStatus.REQUEST;
Expand Down
11 changes: 0 additions & 11 deletions src/main/java/com/moabam/api/dto/bug/TodayBugResponse.java

This file was deleted.

7 changes: 0 additions & 7 deletions src/main/java/com/moabam/api/presentation/BugController.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

import com.moabam.api.application.bug.BugService;
import com.moabam.api.dto.bug.BugResponse;
import com.moabam.api.dto.bug.TodayBugResponse;
import com.moabam.api.dto.product.ProductsResponse;
import com.moabam.api.dto.product.PurchaseProductRequest;
import com.moabam.api.dto.product.PurchaseProductResponse;
Expand All @@ -34,12 +33,6 @@ public BugResponse getBug(@CurrentMember AuthorizationMember member) {
return bugService.getBug(member.id());
}

@GetMapping("/today")
@ResponseStatus(HttpStatus.OK)
public TodayBugResponse getTodayBug(@CurrentMember AuthorizationMember member) {
return bugService.getTodayBug(member.id());
}

@GetMapping("/products")
@ResponseStatus(HttpStatus.OK)
public ProductsResponse getBugProducts() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ public class PaymentController {

private final PaymentService paymentService;

@PostMapping("/{paymentId}/request")
@PostMapping("/{paymentId}")
@ResponseStatus(HttpStatus.OK)
public void requestPayment(@CurrentMember AuthorizationMember member,
public void request(@CurrentMember AuthorizationMember member,
@PathVariable Long paymentId,
@Valid @RequestBody PaymentRequest request) {
paymentService.requestPayment(member.id(), paymentId, request);
paymentService.request(member.id(), paymentId, request);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public enum ErrorMessage {
INVALID_PRICE("가격은 0 이상이어야 합니다."),
INVALID_QUANTITY("수량은 1 이상이어야 합니다."),
INVALID_LEVEL("레벨은 1 이상이어야 합니다."),
INVALID_ORDER_AMOUNT("주문 금액은 0 이상이어야 합니다."),
INVALID_PAYMENT_AMOUNT("결제 금액은 0 이상이어야 합니다."),

PRODUCT_NOT_FOUND("존재하지 않는 상품입니다."),

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ void payment_not_found_exception() {
given(paymentRepository.findById(paymentId)).willReturn(Optional.empty());

// when, then
assertThatThrownBy(() -> paymentService.requestPayment(memberId, paymentId, request))
assertThatThrownBy(() -> paymentService.request(memberId, paymentId, request))
.isInstanceOf(NotFoundException.class)
.hasMessage("존재하지 않는 결제 정보입니다.");
}
Expand Down
Loading