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

feat: 아이템 목록 조회 Response에 현재 적용된 아이템 속성 추가 #100

Merged
merged 4 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ public static ItemResponse toItemResponse(Item item) {
.build();
}

public static ItemsResponse toItemsResponse(List<Item> purchasedItems, List<Item> notPurchasedItems) {
public static ItemsResponse toItemsResponse(Long itemId, List<Item> purchasedItems, List<Item> notPurchasedItems) {
return ItemsResponse.builder()
.defaultItemId(itemId)
.purchasedItems(StreamUtils.map(purchasedItems, ItemMapper::toItemResponse))
.notPurchasedItems(StreamUtils.map(notPurchasedItems, ItemMapper::toItemResponse))
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Item> purchasedItems = inventorySearchRepository.findItems(memberId, type);
List<Item> notPurchasedItems = itemSearchRepository.findNotPurchasedItems(memberId, type);

return ItemMapper.toItemsResponse(purchasedItems, notPurchasedItems);
return ItemMapper.toItemsResponse(defaultItem.getId(), purchasedItems, notPurchasedItems);
}

@Transactional
Expand Down Expand Up @@ -80,6 +81,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 -> {
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/moabam/api/dto/item/ItemsResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

@Builder
public record ItemsResponse(
Long defaultItemId,
List<ItemResponse> purchasedItems,
List<ItemResponse> notPurchasedItems
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 이상이어야 합니다."),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,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());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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")
Expand Down