Skip to content

Commit

Permalink
feat: 아이템 목록 조회 Response에 현재 적용된 아이템 속성 추가 (#100)
Browse files Browse the repository at this point in the history
* feat: 아이템 목록 조회 시 defaultItemId 속성 추가

* test: default 아이템 속성 추가 반영

* style: TodayBugResponse 패키지 위치 변경
  • Loading branch information
kmebin authored Nov 17, 2023
1 parent 58d5280 commit 63030bb
Show file tree
Hide file tree
Showing 11 changed files with 22 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.moabam.api.dto;
package com.moabam.api.dto.bug;

import lombok.Builder;

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 @@ -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;
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 @@ -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;
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

0 comments on commit 63030bb

Please sign in to comment.