Skip to content

Commit

Permalink
feat: 벌레 보상/충전/사용 시 내역 저장 로직 추가 (#165)
Browse files Browse the repository at this point in the history
* refactor: 벌레 사용 + 벌레 내역 저장 로직 하나의 메서드로 분리

* refactor: 벌레 보상 + 벌레 내역 저장 로직 하나의 메서드로 분리

* test: 아이템 서비스 테스트 수정

* test: BugService Mock 추가

* test: 벌레 사용/충전/보상 서비스 테스트

* refactor: 쿠폰 사용 + 벌레 내역 저장 로직 하나의 메서드로 분리

* fix: 불필요한 Mock 제거
  • Loading branch information
kmebin authored Nov 28, 2023
1 parent a8a32bc commit d03feb3
Show file tree
Hide file tree
Showing 12 changed files with 169 additions and 72 deletions.
36 changes: 27 additions & 9 deletions src/main/java/com/moabam/api/application/bug/BugMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,6 @@
@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 @@ -54,6 +45,15 @@ public static BugHistoryResponse toBugHistoryResponse(List<BugHistoryWithPayment
.build();
}

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 BugHistory toChargeBugHistory(Long memberId, int quantity) {
return BugHistory.builder()
.memberId(memberId)
Expand All @@ -62,4 +62,22 @@ public static BugHistory toChargeBugHistory(Long memberId, int quantity) {
.quantity(quantity)
.build();
}

public static BugHistory toRewardBugHistory(Long memberId, BugType bugType, int quantity) {
return BugHistory.builder()
.memberId(memberId)
.bugType(bugType)
.actionType(BugActionType.REWARD)
.quantity(quantity)
.build();
}

public static BugHistory toCouponBugHistory(Long memberId, BugType bugType, int quantity) {
return BugHistory.builder()
.memberId(memberId)
.bugType(bugType)
.actionType(BugActionType.COUPON)
.quantity(quantity)
.build();
}
}
51 changes: 44 additions & 7 deletions src/main/java/com/moabam/api/application/bug/BugService.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.moabam.api.application.coupon.CouponService;
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.Bug;
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.coupon.CouponWallet;
import com.moabam.api.domain.coupon.repository.CouponWalletSearchRepository;
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;
Expand All @@ -28,6 +30,7 @@
import com.moabam.api.dto.product.PurchaseProductRequest;
import com.moabam.api.dto.product.PurchaseProductResponse;
import com.moabam.global.error.exception.NotFoundException;
import com.moabam.global.error.model.ErrorMessage;

import lombok.RequiredArgsConstructor;

Expand All @@ -37,14 +40,14 @@
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;
private final CouponWalletSearchRepository couponWalletSearchRepository;

public BugResponse getBug(Long memberId) {
Bug bug = memberService.findMember(memberId).getBug();
Bug bug = getByMemberId(memberId);

return BugMapper.toBugResponse(bug);
}
Expand All @@ -63,28 +66,62 @@ public ProductsResponse getBugProducts() {

@Transactional
public PurchaseProductResponse purchaseBugProduct(Long memberId, Long productId, PurchaseProductRequest request) {
Product product = getById(productId);
Product product = getProductById(productId);
Payment payment = PaymentMapper.toPayment(memberId, product);

if (!isNull(request.couponWalletId())) {
CouponWallet couponWallet = couponService.getWalletByIdAndMemberId(request.couponWalletId(), memberId);
CouponWallet couponWallet = getCouponWallet(request.couponWalletId(), memberId);
payment.applyCoupon(couponWallet);
}
paymentRepository.save(payment);

return ProductMapper.toPurchaseProductResponse(payment);
}

@Transactional
public void use(Member member, BugType bugType, int count) {
Bug bug = member.getBug();

bug.use(bugType, count);
bugHistoryRepository.save(BugMapper.toUseBugHistory(member.getId(), bugType, count));
}

@Transactional
public void reward(Member member, BugType bugType, int count) {
Bug bug = member.getBug();

bug.increase(bugType, count);
bugHistoryRepository.save(BugMapper.toRewardBugHistory(member.getId(), bugType, count));
}

@Transactional
public void charge(Long memberId, Product bugProduct) {
Bug bug = memberService.findMember(memberId).getBug();
Bug bug = getByMemberId(memberId);

bug.charge(bugProduct.getQuantity());
bugHistoryRepository.save(BugMapper.toChargeBugHistory(memberId, bugProduct.getQuantity()));
}

private Product getById(Long productId) {
@Transactional
public void applyCoupon(Long memberId, BugType bugType, int count) {
Bug bug = getByMemberId(memberId);

bug.increase(bugType, count);
bugHistoryRepository.save(BugMapper.toCouponBugHistory(memberId, bugType, count));
}

private Bug getByMemberId(Long memberId) {
return memberService.findMember(memberId)
.getBug();
}

private Product getProductById(Long productId) {
return productRepository.findById(productId)
.orElseThrow(() -> new NotFoundException(PRODUCT_NOT_FOUND));
}

private CouponWallet getCouponWallet(Long couponWalletId, Long memberId) {
return couponWalletSearchRepository.findByIdAndMemberId(couponWalletId, memberId)
.orElseThrow(() -> new NotFoundException(ErrorMessage.NOT_FOUND_COUPON_WALLET));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.moabam.api.application.member.MemberService;
import com.moabam.api.application.bug.BugService;
import com.moabam.api.domain.bug.BugType;
import com.moabam.api.domain.coupon.Coupon;
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.Member;
import com.moabam.api.domain.member.Role;
import com.moabam.api.dto.coupon.CouponResponse;
import com.moabam.api.dto.coupon.CouponStatusRequest;
Expand All @@ -35,7 +34,7 @@
public class CouponService {

private final ClockHolder clockHolder;
private final MemberService memberService;
private final BugService bugService;
private final CouponManageService couponManageService;
private final CouponRepository couponRepository;
private final CouponSearchRepository couponSearchRepository;
Expand All @@ -59,9 +58,7 @@ public void use(Long memberId, Long couponWalletId) {
Coupon coupon = couponWallet.getCoupon();
BugType bugType = coupon.getType().getBugType();

Member member = memberService.findMember(memberId);
member.getBug().increase(bugType, coupon.getPoint());

bugService.applyCoupon(memberId, bugType, coupon.getPoint());
couponWalletRepository.delete(couponWallet);
}

Expand Down
10 changes: 3 additions & 7 deletions src/main/java/com/moabam/api/application/item/ItemService.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.moabam.api.application.bug.BugMapper;
import com.moabam.api.application.bug.BugService;
import com.moabam.api.application.member.MemberService;
import com.moabam.api.domain.bug.Bug;
import com.moabam.api.domain.bug.repository.BugHistoryRepository;
import com.moabam.api.domain.item.Inventory;
import com.moabam.api.domain.item.Item;
import com.moabam.api.domain.item.ItemType;
Expand All @@ -32,11 +30,11 @@
public class ItemService {

private final MemberService memberService;
private final BugService bugService;
private final ItemRepository itemRepository;
private final ItemSearchRepository itemSearchRepository;
private final InventoryRepository inventoryRepository;
private final InventorySearchRepository inventorySearchRepository;
private final BugHistoryRepository bugHistoryRepository;

public ItemsResponse getItems(Long memberId, ItemType type) {
Item defaultItem = getDefaultInventory(memberId, type).getItem();
Expand All @@ -54,12 +52,10 @@ public void purchaseItem(Long memberId, Long itemId, PurchaseItemRequest request
validateAlreadyPurchased(memberId, itemId);
item.validatePurchasable(request.bugType(), member.getLevel());

Bug bug = member.getBug();
int price = item.getPrice(request.bugType());

bug.use(request.bugType(), price);
bugService.use(member, request.bugType(), price);
inventoryRepository.save(ItemMapper.toInventory(memberId, item));
bugHistoryRepository.save(BugMapper.toUseBugHistory(memberId, request.bugType(), price));
}

@Transactional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.moabam.api.application.bug.BugService;
import com.moabam.api.application.member.MemberService;
import com.moabam.api.application.room.mapper.CertificationsMapper;
import com.moabam.api.domain.bug.BugType;
Expand Down Expand Up @@ -51,6 +52,7 @@ public class CertificationService {
private final DailyRoomCertificationRepository dailyRoomCertificationRepository;
private final DailyMemberCertificationRepository dailyMemberCertificationRepository;
private final MemberService memberService;
private final BugService bugService;
private final ClockHolder clockHolder;

@Transactional
Expand Down Expand Up @@ -88,7 +90,7 @@ public void certifyRoom(CertifiedMemberInfo certifyInfo) {
return;
}

member.getBug().increase(bugType, room.getLevel());
bugService.reward(member, bugType, room.getLevel());
}

public boolean existsMemberCertification(Long memberId, Long roomId, LocalDate date) {
Expand Down Expand Up @@ -187,6 +189,6 @@ private void provideBugToCompletedMembers(BugType bugType, List<DailyMemberCerti
.toList();

memberService.getRoomMembers(memberIds)
.forEach(completedMember -> completedMember.getBug().increase(bugType, expAppliedRoomLevel));
.forEach(completedMember -> bugService.reward(completedMember, bugType, expAppliedRoomLevel));
}
}
31 changes: 16 additions & 15 deletions src/main/java/com/moabam/api/domain/bug/Bug.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,11 @@ private int validateBugCount(int bug) {
return bug;
}

public void use(BugType bugType, int price) {
public void use(BugType bugType, int count) {
int currentBug = getBug(bugType);
validateEnoughBug(currentBug, price);
decrease(bugType, price);

validateEnoughBug(currentBug, count);
decrease(bugType, count);
}

private int getBug(BugType bugType) {
Expand All @@ -59,29 +60,29 @@ private int getBug(BugType bugType) {
};
}

private void validateEnoughBug(int currentBug, int price) {
if (price > currentBug) {
private void validateEnoughBug(int currentBug, int count) {
if (currentBug < count) {
throw new BadRequestException(BUG_NOT_ENOUGH);
}
}

private void decrease(BugType bugType, int bug) {
private void decrease(BugType bugType, int count) {
switch (bugType) {
case MORNING -> this.morningBug -= bug;
case NIGHT -> this.nightBug -= bug;
case GOLDEN -> this.goldenBug -= bug;
case MORNING -> this.morningBug -= count;
case NIGHT -> this.nightBug -= count;
case GOLDEN -> this.goldenBug -= count;
}
}

public void increase(BugType bugType, int bug) {
public void increase(BugType bugType, int count) {
switch (bugType) {
case MORNING -> this.morningBug += bug;
case NIGHT -> this.nightBug += bug;
case GOLDEN -> this.goldenBug += bug;
case MORNING -> this.morningBug += count;
case NIGHT -> this.nightBug += count;
case GOLDEN -> this.goldenBug += count;
}
}

public void charge(int quantity) {
this.goldenBug += quantity;
public void charge(int count) {
this.goldenBug += count;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ public ItemsResponse getItems(@Auth AuthMember member, @RequestParam ItemType ty

@PostMapping("/{itemId}/purchase")
@ResponseStatus(HttpStatus.OK)
public void purchaseItem(@Auth AuthMember member,
@PathVariable Long itemId,
public void purchaseItem(@Auth AuthMember member, @PathVariable Long itemId,
@Valid @RequestBody PurchaseItemRequest request) {
itemService.purchaseItem(member.id(), itemId, request);
}
Expand Down
Loading

0 comments on commit d03feb3

Please sign in to comment.