From d0f2da311f28efc3f65adfd3b7544b223b68e336 Mon Sep 17 00:00:00 2001 From: kmebin Date: Thu, 16 Nov 2023 17:02:38 +0900 Subject: [PATCH 1/3] =?UTF-8?q?feat:=20=EC=95=84=EC=9D=B4=ED=85=9C=20?= =?UTF-8?q?=EB=AA=A9=EB=A1=9D=20=EC=A1=B0=ED=9A=8C=20=EC=8B=9C=20defaultIt?= =?UTF-8?q?emId=20=EC=86=8D=EC=84=B1=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/moabam/api/application/item/ItemMapper.java | 3 ++- .../com/moabam/api/application/item/ItemService.java | 11 ++++++++--- .../java/com/moabam/api/dto/item/ItemsResponse.java | 1 + .../com/moabam/global/error/model/ErrorMessage.java | 1 + 4 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/moabam/api/application/item/ItemMapper.java b/src/main/java/com/moabam/api/application/item/ItemMapper.java index 7499d561..f91591df 100644 --- a/src/main/java/com/moabam/api/application/item/ItemMapper.java +++ b/src/main/java/com/moabam/api/application/item/ItemMapper.java @@ -27,8 +27,9 @@ public static ItemResponse toItemResponse(Item item) { .build(); } - public static ItemsResponse toItemsResponse(List purchasedItems, List notPurchasedItems) { + public static ItemsResponse toItemsResponse(Long itemId, List purchasedItems, List notPurchasedItems) { return ItemsResponse.builder() + .defaultItemId(itemId) .purchasedItems(StreamUtils.map(purchasedItems, ItemMapper::toItemResponse)) .notPurchasedItems(StreamUtils.map(notPurchasedItems, ItemMapper::toItemResponse)) .build(); 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 d1d278aa..822c59a8 100644 --- a/src/main/java/com/moabam/api/application/item/ItemService.java +++ b/src/main/java/com/moabam/api/application/item/ItemService.java @@ -39,10 +39,11 @@ public class ItemService { private final BugHistoryRepository bugHistoryRepository; public ItemsResponse getItems(Long memberId, ItemType type) { + Item defaultItem = getDefaultInventory(memberId, type).getItem(); List purchasedItems = inventorySearchRepository.findItems(memberId, type); List notPurchasedItems = itemSearchRepository.findNotPurchasedItems(memberId, type); - return ItemMapper.toItemsResponse(purchasedItems, notPurchasedItems); + return ItemMapper.toItemsResponse(defaultItem.getId(), purchasedItems, notPurchasedItems); } @Transactional @@ -65,8 +66,7 @@ public void purchaseItem(Long memberId, Long itemId, PurchaseItemRequest request public void selectItem(Long memberId, Long itemId) { Inventory inventory = getInventory(memberId, itemId); - inventorySearchRepository.findDefault(memberId, inventory.getItemType()) - .ifPresent(Inventory::deselect); + getDefaultInventory(memberId, inventory.getItemType()).deselect(); inventory.select(); } @@ -80,6 +80,11 @@ private Inventory getInventory(Long memberId, Long itemId) { .orElseThrow(() -> new NotFoundException(INVENTORY_NOT_FOUND)); } + private Inventory getDefaultInventory(Long memberId, ItemType type) { + return inventorySearchRepository.findDefault(memberId, type) + .orElseThrow(() -> new NotFoundException(DEFAULT_INVENTORY_NOT_FOUND)); + } + private void validateAlreadyPurchased(Long memberId, Long itemId) { inventorySearchRepository.findOne(memberId, itemId) .ifPresent(inventory -> { diff --git a/src/main/java/com/moabam/api/dto/item/ItemsResponse.java b/src/main/java/com/moabam/api/dto/item/ItemsResponse.java index 085d339d..70de47ec 100644 --- a/src/main/java/com/moabam/api/dto/item/ItemsResponse.java +++ b/src/main/java/com/moabam/api/dto/item/ItemsResponse.java @@ -6,6 +6,7 @@ @Builder public record ItemsResponse( + Long defaultItemId, List purchasedItems, List notPurchasedItems ) { diff --git a/src/main/java/com/moabam/global/error/model/ErrorMessage.java b/src/main/java/com/moabam/global/error/model/ErrorMessage.java index a1bc6d53..90c493c0 100644 --- a/src/main/java/com/moabam/global/error/model/ErrorMessage.java +++ b/src/main/java/com/moabam/global/error/model/ErrorMessage.java @@ -40,6 +40,7 @@ public enum ErrorMessage { ITEM_UNLOCK_LEVEL_HIGH("아이템 해금 레벨이 높습니다."), ITEM_NOT_PURCHASABLE_BY_BUG_TYPE("해당 벌레 타입으로는 구매할 수 없는 아이템입니다."), INVENTORY_NOT_FOUND("구매하지 않은 아이템은 적용할 수 없습니다."), + DEFAULT_INVENTORY_NOT_FOUND("현재 적용된 아이템이 없습니다."), INVENTORY_CONFLICT("이미 구매한 아이템입니다."), INVALID_BUG_COUNT("벌레 개수는 0 이상이어야 합니다."), From 0180f5235310df51d135459f74eeb57d52a6c695 Mon Sep 17 00:00:00 2001 From: kmebin Date: Thu, 16 Nov 2023 17:09:53 +0900 Subject: [PATCH 2/3] =?UTF-8?q?test:=20default=20=EC=95=84=EC=9D=B4?= =?UTF-8?q?=ED=85=9C=20=EC=86=8D=EC=84=B1=20=EC=B6=94=EA=B0=80=20=EB=B0=98?= =?UTF-8?q?=EC=98=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/moabam/api/application/item/ItemService.java | 3 ++- .../java/com/moabam/api/application/ItemServiceTest.java | 2 ++ .../com/moabam/api/presentation/ItemControllerTest.java | 6 ++++-- 3 files changed, 8 insertions(+), 3 deletions(-) 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 822c59a8..3a024c2e 100644 --- a/src/main/java/com/moabam/api/application/item/ItemService.java +++ b/src/main/java/com/moabam/api/application/item/ItemService.java @@ -66,7 +66,8 @@ public void purchaseItem(Long memberId, Long itemId, PurchaseItemRequest request public void selectItem(Long memberId, Long itemId) { Inventory inventory = getInventory(memberId, itemId); - getDefaultInventory(memberId, inventory.getItemType()).deselect(); + inventorySearchRepository.findDefault(memberId, inventory.getItemType()) + .ifPresent(Inventory::deselect); inventory.select(); } diff --git a/src/test/java/com/moabam/api/application/ItemServiceTest.java b/src/test/java/com/moabam/api/application/ItemServiceTest.java index 57f69232..a4d3b667 100644 --- a/src/test/java/com/moabam/api/application/ItemServiceTest.java +++ b/src/test/java/com/moabam/api/application/ItemServiceTest.java @@ -71,6 +71,8 @@ void get_products_success() { ItemType type = ItemType.MORNING; Item item1 = morningSantaSkin().build(); Item item2 = morningKillerSkin().build(); + Inventory inventory = inventory(memberId, item1); + given(inventorySearchRepository.findDefault(memberId, type)).willReturn(Optional.of(inventory)); given(inventorySearchRepository.findItems(memberId, type)).willReturn(List.of(item1, item2)); given(itemSearchRepository.findNotPurchasedItems(memberId, type)).willReturn(emptyList()); diff --git a/src/test/java/com/moabam/api/presentation/ItemControllerTest.java b/src/test/java/com/moabam/api/presentation/ItemControllerTest.java index 78e367c4..120f210a 100644 --- a/src/test/java/com/moabam/api/presentation/ItemControllerTest.java +++ b/src/test/java/com/moabam/api/presentation/ItemControllerTest.java @@ -30,6 +30,7 @@ import com.moabam.api.application.item.ItemMapper; import com.moabam.api.application.member.MemberService; import com.moabam.api.domain.bug.BugType; +import com.moabam.api.domain.item.Inventory; import com.moabam.api.domain.item.Item; import com.moabam.api.domain.item.ItemType; import com.moabam.api.domain.item.repository.InventoryRepository; @@ -70,9 +71,10 @@ void success() throws Exception { // given Long memberId = getAuthorizationMember().id(); Item item1 = itemRepository.save(morningSantaSkin().build()); - inventoryRepository.save(inventory(memberId, item1)); + Inventory inventory = inventoryRepository.save(inventory(memberId, item1)); + inventory.select(); Item item2 = itemRepository.save(morningKillerSkin().build()); - ItemsResponse expected = ItemMapper.toItemsResponse(List.of(item1), List.of(item2)); + ItemsResponse expected = ItemMapper.toItemsResponse(item1.getId(), List.of(item1), List.of(item2)); // expected String content = mockMvc.perform(get("/items") From 7fdbe9e3e4693bf20d77fee6cd9c443a205990cc Mon Sep 17 00:00:00 2001 From: kmebin Date: Fri, 17 Nov 2023 14:45:43 +0900 Subject: [PATCH 3/3] =?UTF-8?q?style:=20TodayBugResponse=20=ED=8C=A8?= =?UTF-8?q?=ED=82=A4=EC=A7=80=20=EC=9C=84=EC=B9=98=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/moabam/api/application/bug/BugMapper.java | 2 +- src/main/java/com/moabam/api/application/bug/BugService.java | 2 +- .../java/com/moabam/api/dto/{ => bug}/TodayBugResponse.java | 2 +- src/main/java/com/moabam/api/presentation/BugController.java | 2 +- .../java/com/moabam/api/presentation/BugControllerTest.java | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) rename src/main/java/com/moabam/api/dto/{ => bug}/TodayBugResponse.java (76%) 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 89a4fe83..0095b05a 100644 --- a/src/main/java/com/moabam/api/application/bug/BugMapper.java +++ b/src/main/java/com/moabam/api/application/bug/BugMapper.java @@ -4,8 +4,8 @@ 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.TodayBugResponse; import com.moabam.api.dto.bug.BugResponse; +import com.moabam.api.dto.bug.TodayBugResponse; import lombok.AccessLevel; import lombok.NoArgsConstructor; diff --git a/src/main/java/com/moabam/api/application/bug/BugService.java b/src/main/java/com/moabam/api/application/bug/BugService.java index 60222eb5..6093f51d 100644 --- a/src/main/java/com/moabam/api/application/bug/BugService.java +++ b/src/main/java/com/moabam/api/application/bug/BugService.java @@ -17,8 +17,8 @@ import com.moabam.api.domain.member.Member; import com.moabam.api.domain.product.Product; import com.moabam.api.domain.product.repository.ProductRepository; -import com.moabam.api.dto.TodayBugResponse; import com.moabam.api.dto.bug.BugResponse; +import com.moabam.api.dto.bug.TodayBugResponse; import com.moabam.api.dto.product.ProductsResponse; import com.moabam.global.common.util.ClockHolder; diff --git a/src/main/java/com/moabam/api/dto/TodayBugResponse.java b/src/main/java/com/moabam/api/dto/bug/TodayBugResponse.java similarity index 76% rename from src/main/java/com/moabam/api/dto/TodayBugResponse.java rename to src/main/java/com/moabam/api/dto/bug/TodayBugResponse.java index fb5282d5..11ee0dcc 100644 --- a/src/main/java/com/moabam/api/dto/TodayBugResponse.java +++ b/src/main/java/com/moabam/api/dto/bug/TodayBugResponse.java @@ -1,4 +1,4 @@ -package com.moabam.api.dto; +package com.moabam.api.dto.bug; import lombok.Builder; diff --git a/src/main/java/com/moabam/api/presentation/BugController.java b/src/main/java/com/moabam/api/presentation/BugController.java index 41dfa8d1..95de7f5b 100644 --- a/src/main/java/com/moabam/api/presentation/BugController.java +++ b/src/main/java/com/moabam/api/presentation/BugController.java @@ -7,8 +7,8 @@ import org.springframework.web.bind.annotation.RestController; import com.moabam.api.application.bug.BugService; -import com.moabam.api.dto.TodayBugResponse; import com.moabam.api.dto.bug.BugResponse; +import com.moabam.api.dto.bug.TodayBugResponse; import com.moabam.api.dto.product.ProductsResponse; import com.moabam.global.auth.annotation.CurrentMember; import com.moabam.global.auth.model.AuthorizationMember; diff --git a/src/test/java/com/moabam/api/presentation/BugControllerTest.java b/src/test/java/com/moabam/api/presentation/BugControllerTest.java index c0bf7dbd..a2c66b1e 100644 --- a/src/test/java/com/moabam/api/presentation/BugControllerTest.java +++ b/src/test/java/com/moabam/api/presentation/BugControllerTest.java @@ -32,8 +32,8 @@ import com.moabam.api.domain.bug.repository.BugHistorySearchRepository; import com.moabam.api.domain.product.Product; import com.moabam.api.domain.product.repository.ProductRepository; -import com.moabam.api.dto.TodayBugResponse; import com.moabam.api.dto.bug.BugResponse; +import com.moabam.api.dto.bug.TodayBugResponse; import com.moabam.api.dto.product.ProductsResponse; import com.moabam.support.annotation.WithMember; import com.moabam.support.common.WithoutFilterSupporter;