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 76b10a9f..60222eb5 100644 --- a/src/main/java/com/moabam/api/application/bug/BugService.java +++ b/src/main/java/com/moabam/api/application/bug/BugService.java @@ -2,6 +2,7 @@ import static com.moabam.api.domain.bug.BugActionType.*; import static com.moabam.api.domain.bug.BugType.*; +import static com.moabam.api.domain.product.ProductType.*; import java.util.List; @@ -9,12 +10,16 @@ import org.springframework.transaction.annotation.Transactional; import com.moabam.api.application.member.MemberService; +import com.moabam.api.application.product.ProductMapper; import com.moabam.api.domain.bug.BugHistory; import com.moabam.api.domain.bug.BugType; import com.moabam.api.domain.bug.repository.BugHistorySearchRepository; 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.product.ProductsResponse; import com.moabam.global.common.util.ClockHolder; import lombok.RequiredArgsConstructor; @@ -26,6 +31,7 @@ public class BugService { private final MemberService memberService; private final BugHistorySearchRepository bugHistorySearchRepository; + private final ProductRepository productRepository; private final ClockHolder clockHolder; public BugResponse getBug(Long memberId) { @@ -42,6 +48,12 @@ public TodayBugResponse getTodayBug(Long memberId) { return BugMapper.toTodayBugResponse(morningBug, nightBug); } + public ProductsResponse getBugProducts() { + List products = productRepository.findAllByType(BUG); + + return ProductMapper.toProductsResponse(products); + } + private int calculateBugQuantity(List bugHistory, BugType bugType) { return bugHistory.stream() .filter(history -> bugType.equals(history.getBugType())) diff --git a/src/main/java/com/moabam/api/application/product/ProductService.java b/src/main/java/com/moabam/api/application/product/ProductService.java deleted file mode 100644 index d5d389f3..00000000 --- a/src/main/java/com/moabam/api/application/product/ProductService.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.moabam.api.application.product; - -import java.util.List; - -import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; - -import com.moabam.api.domain.product.Product; -import com.moabam.api.domain.product.repository.ProductRepository; -import com.moabam.api.dto.product.ProductsResponse; - -import lombok.RequiredArgsConstructor; - -@Service -@Transactional(readOnly = true) -@RequiredArgsConstructor -public class ProductService { - - private final ProductRepository productRepository; - - public ProductsResponse getProducts() { - List products = productRepository.findAll(); - - return ProductMapper.toProductsResponse(products); - } -} diff --git a/src/main/java/com/moabam/api/domain/product/repository/ProductRepository.java b/src/main/java/com/moabam/api/domain/product/repository/ProductRepository.java index 4156d9a9..358aa0c0 100644 --- a/src/main/java/com/moabam/api/domain/product/repository/ProductRepository.java +++ b/src/main/java/com/moabam/api/domain/product/repository/ProductRepository.java @@ -1,9 +1,13 @@ package com.moabam.api.domain.product.repository; +import java.util.List; + import org.springframework.data.jpa.repository.JpaRepository; import com.moabam.api.domain.product.Product; +import com.moabam.api.domain.product.ProductType; public interface ProductRepository extends JpaRepository { + List findAllByType(ProductType type); } diff --git a/src/main/java/com/moabam/api/presentation/BugController.java b/src/main/java/com/moabam/api/presentation/BugController.java index edb99b76..41dfa8d1 100644 --- a/src/main/java/com/moabam/api/presentation/BugController.java +++ b/src/main/java/com/moabam/api/presentation/BugController.java @@ -9,6 +9,7 @@ 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.product.ProductsResponse; import com.moabam.global.auth.annotation.CurrentMember; import com.moabam.global.auth.model.AuthorizationMember; @@ -32,4 +33,10 @@ public BugResponse getBug(@CurrentMember AuthorizationMember member) { public TodayBugResponse getTodayBug(@CurrentMember AuthorizationMember member) { return bugService.getTodayBug(member.id()); } + + @GetMapping("/products") + @ResponseStatus(HttpStatus.OK) + public ProductsResponse getBugProducts() { + return bugService.getBugProducts(); + } } diff --git a/src/main/java/com/moabam/api/presentation/ProductController.java b/src/main/java/com/moabam/api/presentation/ProductController.java deleted file mode 100644 index cad9cd79..00000000 --- a/src/main/java/com/moabam/api/presentation/ProductController.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.moabam.api.presentation; - -import org.springframework.http.HttpStatus; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.ResponseStatus; -import org.springframework.web.bind.annotation.RestController; - -import com.moabam.api.application.product.ProductService; -import com.moabam.api.dto.product.ProductsResponse; - -import lombok.RequiredArgsConstructor; - -@RestController -@RequestMapping("/products") -@RequiredArgsConstructor -public class ProductController { - - private final ProductService productService; - - @GetMapping - @ResponseStatus(HttpStatus.OK) - public ProductsResponse getProducts() { - return productService.getProducts(); - } -} diff --git a/src/test/java/com/moabam/api/application/ProductServiceTest.java b/src/test/java/com/moabam/api/application/ProductServiceTest.java deleted file mode 100644 index 24b331a6..00000000 --- a/src/test/java/com/moabam/api/application/ProductServiceTest.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.moabam.api.application; - -import static com.moabam.support.fixture.ProductFixture.*; -import static org.assertj.core.api.Assertions.*; -import static org.mockito.BDDMockito.*; - -import java.util.List; - -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.mockito.junit.jupiter.MockitoExtension; - -import com.moabam.api.application.product.ProductService; -import com.moabam.api.domain.product.Product; -import com.moabam.api.domain.product.repository.ProductRepository; -import com.moabam.api.dto.product.ProductResponse; -import com.moabam.api.dto.product.ProductsResponse; -import com.moabam.global.common.util.StreamUtils; - -@ExtendWith(MockitoExtension.class) -class ProductServiceTest { - - @InjectMocks - ProductService productService; - - @Mock - ProductRepository productRepository; - - @DisplayName("상품 목록을 조회한다.") - @Test - void get_products_success() { - // given - Product product1 = bugProduct(); - Product product2 = bugProduct(); - given(productRepository.findAll()).willReturn(List.of(product1, product2)); - - // when - ProductsResponse response = productService.getProducts(); - - // then - List productNames = StreamUtils.map(response.products(), ProductResponse::name); - assertThat(response.products()).hasSize(2); - assertThat(productNames).containsExactly(BUG_PRODUCT_NAME, BUG_PRODUCT_NAME); - } -} diff --git a/src/test/java/com/moabam/api/application/BugServiceTest.java b/src/test/java/com/moabam/api/application/bug/BugServiceTest.java similarity index 53% rename from src/test/java/com/moabam/api/application/BugServiceTest.java rename to src/test/java/com/moabam/api/application/bug/BugServiceTest.java index 27b97d1a..6d65a2e3 100644 --- a/src/test/java/com/moabam/api/application/BugServiceTest.java +++ b/src/test/java/com/moabam/api/application/bug/BugServiceTest.java @@ -1,9 +1,13 @@ -package com.moabam.api.application; +package com.moabam.api.application.bug; +import static com.moabam.api.domain.product.ProductType.*; import static com.moabam.support.fixture.MemberFixture.*; +import static com.moabam.support.fixture.ProductFixture.*; import static org.assertj.core.api.Assertions.*; import static org.mockito.BDDMockito.*; +import java.util.List; + import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -11,11 +15,15 @@ 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.Bug; 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.bug.BugResponse; +import com.moabam.api.dto.product.ProductResponse; +import com.moabam.api.dto.product.ProductsResponse; +import com.moabam.global.common.util.StreamUtils; @ExtendWith(MockitoExtension.class) class BugServiceTest { @@ -26,6 +34,9 @@ class BugServiceTest { @Mock MemberService memberService; + @Mock + ProductRepository productRepository; + @DisplayName("벌레를 조회한다.") @Test void get_bug_success() { @@ -43,4 +54,21 @@ void get_bug_success() { assertThat(response.nightBug()).isEqualTo(bug.getNightBug()); assertThat(response.goldenBug()).isEqualTo(bug.getGoldenBug()); } + + @DisplayName("벌레 상품 목록을 조회한다.") + @Test + void get_bug_products_success() { + // given + Product product1 = bugProduct(); + Product product2 = bugProduct(); + given(productRepository.findAllByType(BUG)).willReturn(List.of(product1, product2)); + + // when + ProductsResponse response = bugService.getBugProducts(); + + // then + List productNames = StreamUtils.map(response.products(), ProductResponse::name); + assertThat(response.products()).hasSize(2); + assertThat(productNames).containsExactly(BUG_PRODUCT_NAME, BUG_PRODUCT_NAME); + } } diff --git a/src/test/java/com/moabam/api/application/ItemServiceTest.java b/src/test/java/com/moabam/api/application/item/ItemServiceTest.java similarity index 98% rename from src/test/java/com/moabam/api/application/ItemServiceTest.java rename to src/test/java/com/moabam/api/application/item/ItemServiceTest.java index 57f69232..c48fcb12 100644 --- a/src/test/java/com/moabam/api/application/ItemServiceTest.java +++ b/src/test/java/com/moabam/api/application/item/ItemServiceTest.java @@ -1,4 +1,4 @@ -package com.moabam.api.application; +package com.moabam.api.application.item; import static com.moabam.support.fixture.InventoryFixture.*; import static com.moabam.support.fixture.ItemFixture.*; @@ -19,7 +19,6 @@ import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; -import com.moabam.api.application.item.ItemService; import com.moabam.api.application.member.MemberService; import com.moabam.api.domain.bug.BugHistory; import com.moabam.api.domain.bug.BugType; diff --git a/src/test/java/com/moabam/api/domain/entity/BugTest.java b/src/test/java/com/moabam/api/domain/bug/BugTest.java similarity index 94% rename from src/test/java/com/moabam/api/domain/entity/BugTest.java rename to src/test/java/com/moabam/api/domain/bug/BugTest.java index 6a00a0f9..f2720165 100644 --- a/src/test/java/com/moabam/api/domain/entity/BugTest.java +++ b/src/test/java/com/moabam/api/domain/bug/BugTest.java @@ -1,4 +1,4 @@ -package com.moabam.api.domain.entity; +package com.moabam.api.domain.bug; import static com.moabam.support.fixture.BugFixture.*; import static org.assertj.core.api.Assertions.*; @@ -10,8 +10,6 @@ import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; -import com.moabam.api.domain.bug.Bug; -import com.moabam.api.domain.bug.BugType; import com.moabam.global.error.exception.BadRequestException; class BugTest { diff --git a/src/test/java/com/moabam/api/domain/entity/ItemTest.java b/src/test/java/com/moabam/api/domain/item/ItemTest.java similarity index 97% rename from src/test/java/com/moabam/api/domain/entity/ItemTest.java rename to src/test/java/com/moabam/api/domain/item/ItemTest.java index be0b518b..326dd553 100644 --- a/src/test/java/com/moabam/api/domain/entity/ItemTest.java +++ b/src/test/java/com/moabam/api/domain/item/ItemTest.java @@ -1,4 +1,4 @@ -package com.moabam.api.domain.entity; +package com.moabam.api.domain.item; import static com.moabam.support.fixture.ItemFixture.*; import static org.assertj.core.api.Assertions.*; @@ -11,7 +11,6 @@ import org.junit.jupiter.params.provider.CsvSource; import com.moabam.api.domain.bug.BugType; -import com.moabam.api.domain.item.Item; import com.moabam.global.error.exception.BadRequestException; class ItemTest { diff --git a/src/test/java/com/moabam/api/domain/repository/InventorySearchRepositoryTest.java b/src/test/java/com/moabam/api/domain/item/repository/InventorySearchRepositoryTest.java similarity index 93% rename from src/test/java/com/moabam/api/domain/repository/InventorySearchRepositoryTest.java rename to src/test/java/com/moabam/api/domain/item/repository/InventorySearchRepositoryTest.java index babd5c2e..58f2a8df 100644 --- a/src/test/java/com/moabam/api/domain/repository/InventorySearchRepositoryTest.java +++ b/src/test/java/com/moabam/api/domain/item/repository/InventorySearchRepositoryTest.java @@ -1,4 +1,4 @@ -package com.moabam.api.domain.repository; +package com.moabam.api.domain.item.repository; import static com.moabam.support.fixture.InventoryFixture.*; import static com.moabam.support.fixture.ItemFixture.*; @@ -16,9 +16,6 @@ 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; -import com.moabam.api.domain.item.repository.InventorySearchRepository; -import com.moabam.api.domain.item.repository.ItemRepository; import com.moabam.api.domain.member.Member; import com.moabam.api.domain.member.repository.MemberRepository; import com.moabam.support.annotation.QuerydslRepositoryTest; diff --git a/src/test/java/com/moabam/api/domain/repository/ItemSearchRepositoryTest.java b/src/test/java/com/moabam/api/domain/item/repository/ItemSearchRepositoryTest.java similarity index 92% rename from src/test/java/com/moabam/api/domain/repository/ItemSearchRepositoryTest.java rename to src/test/java/com/moabam/api/domain/item/repository/ItemSearchRepositoryTest.java index 6e7e16ff..1d1988a7 100644 --- a/src/test/java/com/moabam/api/domain/repository/ItemSearchRepositoryTest.java +++ b/src/test/java/com/moabam/api/domain/item/repository/ItemSearchRepositoryTest.java @@ -1,4 +1,4 @@ -package com.moabam.api.domain.repository; +package com.moabam.api.domain.item.repository; import static com.moabam.support.fixture.InventoryFixture.*; import static com.moabam.support.fixture.ItemFixture.*; @@ -13,9 +13,6 @@ import com.moabam.api.domain.item.Item; import com.moabam.api.domain.item.ItemType; -import com.moabam.api.domain.item.repository.InventoryRepository; -import com.moabam.api.domain.item.repository.ItemRepository; -import com.moabam.api.domain.item.repository.ItemSearchRepository; import com.moabam.support.annotation.QuerydslRepositoryTest; @QuerydslRepositoryTest diff --git a/src/test/java/com/moabam/api/domain/entity/ProductTest.java b/src/test/java/com/moabam/api/domain/product/ProductTest.java similarity index 92% rename from src/test/java/com/moabam/api/domain/entity/ProductTest.java rename to src/test/java/com/moabam/api/domain/product/ProductTest.java index f960a30a..4e905697 100644 --- a/src/test/java/com/moabam/api/domain/entity/ProductTest.java +++ b/src/test/java/com/moabam/api/domain/product/ProductTest.java @@ -1,11 +1,10 @@ -package com.moabam.api.domain.entity; +package com.moabam.api.domain.product; import static org.assertj.core.api.AssertionsForClassTypes.*; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; -import com.moabam.api.domain.product.Product; import com.moabam.global.error.exception.BadRequestException; class ProductTest { diff --git a/src/test/java/com/moabam/api/presentation/BugControllerTest.java b/src/test/java/com/moabam/api/presentation/BugControllerTest.java index 6431727b..c0bf7dbd 100644 --- a/src/test/java/com/moabam/api/presentation/BugControllerTest.java +++ b/src/test/java/com/moabam/api/presentation/BugControllerTest.java @@ -4,6 +4,7 @@ import static com.moabam.support.fixture.BugFixture.*; import static com.moabam.support.fixture.BugHistoryFixture.*; import static com.moabam.support.fixture.MemberFixture.*; +import static com.moabam.support.fixture.ProductFixture.*; import static java.nio.charset.StandardCharsets.*; import static org.assertj.core.api.Assertions.*; import static org.mockito.BDDMockito.*; @@ -26,10 +27,14 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.moabam.api.application.bug.BugMapper; import com.moabam.api.application.member.MemberService; +import com.moabam.api.application.product.ProductMapper; import com.moabam.api.domain.bug.repository.BugHistoryRepository; 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.product.ProductsResponse; import com.moabam.support.annotation.WithMember; import com.moabam.support.common.WithoutFilterSupporter; @@ -53,6 +58,9 @@ class BugControllerTest extends WithoutFilterSupporter { @Autowired BugHistorySearchRepository bugHistorySearchRepository; + @Autowired + ProductRepository productRepository; + @DisplayName("벌레를 조회한다.") @WithMember @Test @@ -97,4 +105,23 @@ void get_today_bug_success() throws Exception { TodayBugResponse actual = objectMapper.readValue(content, TodayBugResponse.class); assertThat(actual).isEqualTo(expected); } + + @DisplayName("벌레 상품 목록을 조회한다.") + @Test + void get_products_success() throws Exception { + // given + List products = productRepository.saveAll(List.of(bugProduct(), bugProduct())); + ProductsResponse expected = ProductMapper.toProductsResponse(products); + + // when, then + String content = mockMvc.perform(get("/bugs/products") + .contentType(APPLICATION_JSON)) + .andExpect(status().isOk()) + .andDo(print()) + .andReturn() + .getResponse() + .getContentAsString(UTF_8); + ProductsResponse actual = objectMapper.readValue(content, ProductsResponse.class); + assertThat(actual).isEqualTo(expected); + } } diff --git a/src/test/java/com/moabam/api/presentation/ProductControllerTest.java b/src/test/java/com/moabam/api/presentation/ProductControllerTest.java deleted file mode 100644 index df4f0c0f..00000000 --- a/src/test/java/com/moabam/api/presentation/ProductControllerTest.java +++ /dev/null @@ -1,60 +0,0 @@ -package com.moabam.api.presentation; - -import static com.moabam.support.fixture.ProductFixture.*; -import static java.nio.charset.StandardCharsets.*; -import static org.assertj.core.api.Assertions.*; -import static org.springframework.http.MediaType.*; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; -import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.*; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; - -import java.util.List; - -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.web.servlet.MockMvc; -import org.springframework.transaction.annotation.Transactional; - -import com.fasterxml.jackson.databind.ObjectMapper; -import com.moabam.api.application.product.ProductMapper; -import com.moabam.api.domain.product.Product; -import com.moabam.api.domain.product.repository.ProductRepository; -import com.moabam.api.dto.product.ProductsResponse; -import com.moabam.support.common.WithoutFilterSupporter; - -@Transactional -@SpringBootTest -@AutoConfigureMockMvc -class ProductControllerTest extends WithoutFilterSupporter { - - @Autowired - MockMvc mockMvc; - - @Autowired - ObjectMapper objectMapper; - - @Autowired - ProductRepository productRepository; - - @DisplayName("상품 목록을 조회한다.") - @Test - void get_products_success() throws Exception { - // given - List products = productRepository.saveAll(List.of(bugProduct(), bugProduct())); - ProductsResponse expected = ProductMapper.toProductsResponse(products); - - // when, then - String content = mockMvc.perform(get("/products") - .contentType(APPLICATION_JSON)) - .andExpect(status().isOk()) - .andDo(print()) - .andReturn() - .getResponse() - .getContentAsString(UTF_8); - ProductsResponse actual = objectMapper.readValue(content, ProductsResponse.class); - assertThat(actual).isEqualTo(expected); - } -}