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 cf40d215..af91b206 100644 --- a/src/main/java/com/moabam/api/application/bug/BugMapper.java +++ b/src/main/java/com/moabam/api/application/bug/BugMapper.java @@ -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()) @@ -54,6 +45,15 @@ public static BugHistoryResponse toBugHistoryResponse(List 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)); + } } diff --git a/src/main/java/com/moabam/api/application/coupon/CouponService.java b/src/main/java/com/moabam/api/application/coupon/CouponService.java index 1d36be55..3653802c 100644 --- a/src/main/java/com/moabam/api/application/coupon/CouponService.java +++ b/src/main/java/com/moabam/api/application/coupon/CouponService.java @@ -6,7 +6,7 @@ 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; @@ -14,7 +14,6 @@ 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; @@ -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; @@ -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); } diff --git a/src/main/java/com/moabam/api/application/item/ItemService.java b/src/main/java/com/moabam/api/application/item/ItemService.java index a0d5015f..8e0f9f72 100644 --- a/src/main/java/com/moabam/api/application/item/ItemService.java +++ b/src/main/java/com/moabam/api/application/item/ItemService.java @@ -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; @@ -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(); @@ -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 diff --git a/src/main/java/com/moabam/api/application/room/CertificationService.java b/src/main/java/com/moabam/api/application/room/CertificationService.java index f8e72e9d..3a679550 100644 --- a/src/main/java/com/moabam/api/application/room/CertificationService.java +++ b/src/main/java/com/moabam/api/application/room/CertificationService.java @@ -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; @@ -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 @@ -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) { @@ -187,6 +189,6 @@ private void provideBugToCompletedMembers(BugType bugType, List completedMember.getBug().increase(bugType, expAppliedRoomLevel)); + .forEach(completedMember -> bugService.reward(completedMember, bugType, expAppliedRoomLevel)); } } diff --git a/src/main/java/com/moabam/api/domain/bug/Bug.java b/src/main/java/com/moabam/api/domain/bug/Bug.java index ae07f4d6..f581c870 100644 --- a/src/main/java/com/moabam/api/domain/bug/Bug.java +++ b/src/main/java/com/moabam/api/domain/bug/Bug.java @@ -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) { @@ -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; } } diff --git a/src/main/java/com/moabam/api/presentation/ItemController.java b/src/main/java/com/moabam/api/presentation/ItemController.java index fbb55663..7b55c155 100644 --- a/src/main/java/com/moabam/api/presentation/ItemController.java +++ b/src/main/java/com/moabam/api/presentation/ItemController.java @@ -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); } diff --git a/src/test/java/com/moabam/api/application/bug/BugServiceTest.java b/src/test/java/com/moabam/api/application/bug/BugServiceTest.java index e2bb6d8e..7c147a98 100644 --- a/src/test/java/com/moabam/api/application/bug/BugServiceTest.java +++ b/src/test/java/com/moabam/api/application/bug/BugServiceTest.java @@ -1,6 +1,7 @@ package com.moabam.api.application.bug; import static com.moabam.api.domain.product.ProductType.*; +import static com.moabam.support.fixture.BugFixture.*; import static com.moabam.support.fixture.CouponFixture.*; import static com.moabam.support.fixture.MemberFixture.*; import static com.moabam.support.fixture.ProductFixture.*; @@ -18,11 +19,13 @@ import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; -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.domain.bug.Bug; +import com.moabam.api.domain.bug.BugType; +import com.moabam.api.domain.bug.repository.BugHistoryRepository; 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; @@ -46,7 +49,7 @@ class BugServiceTest { MemberService memberService; @Mock - CouponService couponService; + BugHistoryRepository bugHistoryRepository; @Mock ProductRepository productRepository; @@ -54,6 +57,9 @@ class BugServiceTest { @Mock PaymentRepository paymentRepository; + @Mock + CouponWalletSearchRepository couponWalletSearchRepository; + @DisplayName("벌레를 조회한다.") @Test void get_bug_success() { @@ -100,12 +106,13 @@ void apply_coupon_success() { Long memberId = 1L; Long productId = 1L; Long couponWalletId = 1L; + CouponWallet couponWallet = CouponWallet.create(memberId, discount1000Coupon()); Payment payment = PaymentMapper.toPayment(memberId, bugProduct()); PurchaseProductRequest request = new PurchaseProductRequest(couponWalletId); given(productRepository.findById(productId)).willReturn(Optional.of(bugProduct())); given(paymentRepository.save(any(Payment.class))).willReturn(payment); - given(couponService.getWalletByIdAndMemberId(couponWalletId, memberId)).willReturn( - CouponWallet.create(memberId, discount1000Coupon())); + given(couponWalletSearchRepository.findByIdAndMemberId(couponWalletId, memberId)).willReturn( + Optional.of(couponWallet)); // when PurchaseProductResponse response = bugService.purchaseBugProduct(memberId, productId, request); @@ -130,4 +137,47 @@ void product_not_found_exception() { .hasMessage("존재하지 않는 상품입니다."); } } + + @DisplayName("벌레를 사용한다.") + @Test + void use_success() { + // given + Member member = spy(member()); + given(member.getId()).willReturn(1L); + + // when + bugService.use(member, BugType.MORNING, 5); + + // then + assertThat(member.getBug().getMorningBug()).isEqualTo(MORNING_BUG - 5); + } + + @DisplayName("벌레 보상을 준다.") + @Test + void reward_success() { + // given + Member member = spy(member()); + given(member.getId()).willReturn(1L); + + // when + bugService.reward(member, BugType.NIGHT, 5); + + // then + assertThat(member.getBug().getNightBug()).isEqualTo(NIGHT_BUG + 5); + } + + @DisplayName("벌레를 충전한다.") + @Test + void charge_success() { + // given + Long memberId = 1L; + Member member = member(); + given(memberService.findMember(memberId)).willReturn(member); + + // when + bugService.charge(memberId, bugProduct()); + + // then + assertThat(member.getBug().getGoldenBug()).isEqualTo(GOLDEN_BUG + BUG_PRODUCT_QUANTITY); + } } diff --git a/src/test/java/com/moabam/api/application/coupon/CouponServiceTest.java b/src/test/java/com/moabam/api/application/coupon/CouponServiceTest.java index de7e817e..86db23f0 100644 --- a/src/test/java/com/moabam/api/application/coupon/CouponServiceTest.java +++ b/src/test/java/com/moabam/api/application/coupon/CouponServiceTest.java @@ -17,7 +17,7 @@ import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; -import com.moabam.api.application.member.MemberService; +import com.moabam.api.application.bug.BugService; import com.moabam.api.domain.coupon.Coupon; import com.moabam.api.domain.coupon.CouponType; import com.moabam.api.domain.coupon.CouponWallet; @@ -25,7 +25,6 @@ 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; @@ -40,9 +39,7 @@ import com.moabam.global.error.model.ErrorMessage; import com.moabam.support.annotation.WithMember; import com.moabam.support.common.FilterProcessExtension; -import com.moabam.support.fixture.BugFixture; import com.moabam.support.fixture.CouponFixture; -import com.moabam.support.fixture.MemberFixture; @ExtendWith({MockitoExtension.class, FilterProcessExtension.class}) class CouponServiceTest { @@ -54,7 +51,7 @@ class CouponServiceTest { CouponManageService couponManageService; @Mock - MemberService memberService; + BugService bugService; @Mock CouponRepository couponRepository; @@ -321,11 +318,9 @@ void getByWalletIdAndMemberId_success() { @Test void use_success() { // Given - Member member = MemberFixture.member(BugFixture.zeroBug()); Coupon coupon = CouponFixture.coupon(CouponType.GOLDEN, 1000); CouponWallet couponWallet = CouponWallet.create(1L, coupon); - given(memberService.findMember(any(Long.class))).willReturn(member); given(couponWalletSearchRepository.findByIdAndMemberId(any(Long.class), any(Long.class))) .willReturn(Optional.of(couponWallet)); diff --git a/src/test/java/com/moabam/api/application/item/ItemServiceTest.java b/src/test/java/com/moabam/api/application/item/ItemServiceTest.java index 6abe441d..dca4b164 100644 --- a/src/test/java/com/moabam/api/application/item/ItemServiceTest.java +++ b/src/test/java/com/moabam/api/application/item/ItemServiceTest.java @@ -19,8 +19,8 @@ import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; +import com.moabam.api.application.bug.BugService; import com.moabam.api.application.member.MemberService; -import com.moabam.api.domain.bug.BugHistory; import com.moabam.api.domain.bug.BugType; import com.moabam.api.domain.bug.repository.BugHistoryRepository; import com.moabam.api.domain.item.Inventory; @@ -47,6 +47,9 @@ class ItemServiceTest { @Mock MemberService memberService; + @Mock + BugService bugService; + @Mock ItemRepository itemRepository; @@ -106,11 +109,8 @@ void success() { itemService.purchaseItem(memberId, itemId, request); // Then - verify(memberService).findMember(memberId); - verify(itemRepository).findById(itemId); - verify(inventorySearchRepository).findOne(memberId, itemId); + verify(bugService).use(any(Member.class), any(BugType.class), anyInt()); verify(inventoryRepository).save(any(Inventory.class)); - verify(bugHistoryRepository).save(any(BugHistory.class)); } @DisplayName("해당 아이템이 존재하지 않으면 예외가 발생한다.") @@ -167,8 +167,6 @@ void success() { itemService.selectItem(memberId, itemId); // then - verify(inventorySearchRepository).findOne(memberId, itemId); - verify(inventorySearchRepository).findDefault(memberId, itemType); assertFalse(defaultInventory.isDefault()); assertTrue(inventory.isDefault()); } diff --git a/src/test/java/com/moabam/api/application/room/CertificationServiceTest.java b/src/test/java/com/moabam/api/application/room/CertificationServiceTest.java index 83c98f41..d15ecdf1 100644 --- a/src/test/java/com/moabam/api/application/room/CertificationServiceTest.java +++ b/src/test/java/com/moabam/api/application/room/CertificationServiceTest.java @@ -19,6 +19,7 @@ import org.mockito.Spy; import org.mockito.junit.jupiter.MockitoExtension; +import com.moabam.api.application.bug.BugService; import com.moabam.api.application.image.ImageService; import com.moabam.api.application.member.MemberService; import com.moabam.api.application.room.mapper.CertificationsMapper; @@ -51,6 +52,9 @@ class CertificationServiceTest { @Mock private MemberService memberService; + @Mock + private BugService bugService; + @Mock private RoomRepository roomRepository; @@ -161,7 +165,7 @@ void already_certified_room_routine_success() { certificationService.certifyRoom(certifyInfo); // then - assertThat(member1.getBug().getMorningBug()).isEqualTo(12); + verify(bugService).reward(any(Member.class), any(BugType.class), anyInt()); } @DisplayName("인증되지 않은 방에서 루틴 인증 후 방의 인증 성공") @@ -184,11 +188,7 @@ void not_certified_room_routine_success() { certificationService.certifyRoom(certifyInfo); // then - assertThat(member1.getBug().getMorningBug()).isEqualTo(12); - assertThat(member2.getBug().getMorningBug()).isEqualTo(12); - assertThat(member3.getBug().getMorningBug()).isEqualTo(12); - assertThat(member3.getBug().getNightBug()).isEqualTo(20); - assertThat(member3.getBug().getGoldenBug()).isEqualTo(30); + verify(bugService, times(3)).reward(any(Member.class), any(BugType.class), anyInt()); assertThat(room.getExp()).isEqualTo(1); assertThat(room.getLevel()).isEqualTo(2); } diff --git a/src/test/java/com/moabam/api/presentation/ItemControllerTest.java b/src/test/java/com/moabam/api/presentation/ItemControllerTest.java index b637e8f7..3cdd53d7 100644 --- a/src/test/java/com/moabam/api/presentation/ItemControllerTest.java +++ b/src/test/java/com/moabam/api/presentation/ItemControllerTest.java @@ -27,6 +27,7 @@ import org.springframework.transaction.annotation.Transactional; import com.fasterxml.jackson.databind.ObjectMapper; +import com.moabam.api.application.bug.BugService; import com.moabam.api.application.item.ItemMapper; import com.moabam.api.application.member.MemberService; import com.moabam.api.domain.bug.BugType; @@ -54,6 +55,9 @@ class ItemControllerTest extends WithoutFilterSupporter { @MockBean MemberService memberService; + @MockBean + BugService bugService; + @Autowired ItemRepository itemRepository;