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

refactor: Store 전체 로직 수정 #13

Merged
merged 5 commits into from
Aug 17, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
Binary file modified .DS_Store
Binary file not shown.
33 changes: 9 additions & 24 deletions src/main/java/matal/store/controller/StoreController.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,9 @@ public class StoreController {
@ApiResponse(responseCode = "404", description = "실패"),
})
public List<StoreResponseDto> getStoreListByNmae(@RequestParam(name = "name", required = false) String name) {
StoreRequestDto storeRequestDto = new StoreRequestDto(name, null, null);
List<StoreResponseDto> storeResponseDtoList = new ArrayList<>();

if (storeRequestDto.name() != null) {
Optional<List<StoreResponseDto>> nameSearchResults = storeService.StoreNameSearch(storeRequestDto);
nameSearchResults.ifPresent(storeResponseDtoList::addAll);
}
return storeResponseDtoList;
if(name == null)
throw new IllegalArgumentException("Error");
return storeService.findStoresByName(name);
}

//카테고리로 가게 리스트 조회
Expand All @@ -55,14 +50,9 @@ public List<StoreResponseDto> getStoreListByNmae(@RequestParam(name = "name", re
@ApiResponse(responseCode = "404", description = "실패"),
})
public List<StoreResponseDto> getStoreListByCategory(@RequestParam(name = "category", required = false) String category) {
StoreRequestDto storeRequestDto = new StoreRequestDto(null, category, null);
List<StoreResponseDto> storeResponseDtoList = new ArrayList<>();

if (storeRequestDto.category() != null) {
Optional<List<StoreResponseDto>> categorySearchResults = storeService.StoreCategorySearch(storeRequestDto);
categorySearchResults.ifPresent(storeResponseDtoList::addAll);
}
return storeResponseDtoList;
if(category == null)
throw new IllegalArgumentException("Error");
return storeService.findStoresByCategory(category);
}

//지하철역으로 가게 리스트 조회
Expand All @@ -75,14 +65,9 @@ public List<StoreResponseDto> getStoreListByCategory(@RequestParam(name = "categ
@ApiResponse(responseCode = "404", description = "실패"),
})
public List<StoreResponseDto> getStoreListByStation( @RequestParam(name = "nearby_station", required = false) String nearby_station) {
StoreRequestDto storeRequestDto = new StoreRequestDto(null, null, nearby_station);
List<StoreResponseDto> storeResponseDtoList = new ArrayList<>();

if (storeRequestDto.nearby_station() != null) {
Optional<List<StoreResponseDto>> stationSearchResults = storeService.StoreStationSearch(storeRequestDto);
stationSearchResults.ifPresent(storeResponseDtoList::addAll);
}
return storeResponseDtoList;
if(nearby_station == null)
throw new IllegalArgumentException("Error");
return storeService.findStoresByStation(nearby_station);
}

// //가게 상세 정보 조회
Expand Down
31 changes: 15 additions & 16 deletions src/main/java/matal/store/service/StoreService.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,24 @@ public class StoreService {

private final StoreRepository storeRepository;

//가게 이름 검색 조회 리스트
public Optional<List<StoreResponseDto>> StoreNameSearch(StoreRequestDto storeRequestDto) {
return storeRepository.findByNameContaining(storeRequestDto.name())
.map(stores -> stores.stream()
.map(StoreResponseDto::from)
.toList());
public List<StoreResponseDto> findStoresByName(String name) {
return storeRepository.findByNameContaining(name)
.orElseThrow(() -> new IllegalArgumentException("Error"))
.stream().map(StoreResponseDto::from)
.toList();
}

public Optional<List<StoreResponseDto>> StoreCategorySearch(StoreRequestDto storeRequestDto) {
return storeRepository.findByCategoryContaining(storeRequestDto.category())
.map(stores -> stores.stream()
.map(StoreResponseDto::from)
.toList());
public List<StoreResponseDto> findStoresByCategory(String category) {
return storeRepository.findByCategoryContaining(category)
.orElseThrow(() -> new IllegalArgumentException("Error"))
.stream().map(StoreResponseDto::from)
.toList();
}

public Optional<List<StoreResponseDto>> StoreStationSearch(StoreRequestDto storeRequestDto) {
return storeRepository.findByNearbyStationContaining(storeRequestDto.nearby_station())
.map(stores -> stores.stream()
.map(StoreResponseDto::from)
.toList());
public List<StoreResponseDto> findStoresByStation(String stationName) {
return storeRepository.findByNearbyStationContaining(stationName)
.orElseThrow(() -> new IllegalArgumentException("Error"))
.stream().map(StoreResponseDto::from)
.toList();
}
}
4 changes: 1 addition & 3 deletions src/test/java/matal/MatalApplicationTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,5 @@
class MatalApplicationTests {

@Test
void contextLoads() {
}

void contextLoads() {}
}
114 changes: 0 additions & 114 deletions src/test/java/matal/StoreServiceTest.java

This file was deleted.

111 changes: 111 additions & 0 deletions src/test/java/matal/store/service/StoreServiceTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package matal.store.service;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.Mockito.when;

import java.util.List;
import java.util.Optional;
import matal.store.dto.StoreResponseDto;
import matal.store.entity.Store;
import matal.store.repository.StoreRepository;
import org.junit.jupiter.api.BeforeEach;
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 org.springframework.test.context.ActiveProfiles;

@ExtendWith(MockitoExtension.class)
@ActiveProfiles("local")
public class StoreServiceTest {

@Mock
private StoreRepository storeRepository;

@InjectMocks
private StoreService storeService;

private Store store1;
private Store store2;

@BeforeEach
void setUp() {
store1 = createStore(1L, "Test Store1", "Address 1", "Station 1");
store2 = createStore(2L, "Test Store2", "Address 2", "Station 2");
}

private Store createStore(Long id, String name, String address, String nearbyStation) {
return Store.builder()
.id(id)
.keyword("Test")
.name(name)
.store_link("ssss")
.reviews_count(10L)
.category("Food")
.address(address)
.nearby_station(nearbyStation)
.phone("123-456-789")
.business_hours("9-23")
.latitude(12.4)
.longitude(11.11)
.positive_keywords("good")
.review_summary("summary")
.rating(4.5)
.positive_ratio(93.0)
.negative_ratio(7.0)
.build();
}

@Test
@DisplayName("가게 이름을 이용해 목록 조회 테스트")
void testStoreNameSearch() {
// given
List<Store> stores = List.of(store1, store2);

// when
when(storeRepository.findByNameContaining("Test")).thenReturn(Optional.of(stores));
List<StoreResponseDto> responses = storeService.findStoresByName("Test");

// then
assertNotNull(responses);
assertEquals(responses.get(0).address(), store1.getAddress());
assertEquals(responses.get(1).address(), store2.getAddress());
}

@Test
@DisplayName("가게 카테고리를 이용해 목록 조회 테스트")
void testStoreCategorySearch() {
// given
List<Store> stores = List.of(store1, store2);

// when
when(storeRepository.findByCategoryContaining("Food")).thenReturn(Optional.of(stores));
List<StoreResponseDto> responses = storeService.findStoresByCategory("Food");

// then
assertNotNull(responses);
assertEquals(responses.get(0).address(), store1.getAddress());
assertEquals(responses.get(1).address(), store2.getAddress());
}

@Test
@DisplayName("가게 주변 역을 이용한 목록 조회 테스트")
void testStoreStationSearch() {
// given
store1 = createStore(1L, "Test Store1", "Address 1", "부산역 3번 출구로 부터 10m");
store2 = createStore(2L, "Test Store2", "Address 2", "부산역 2번 출구로부터 30m");
List<Store> stores = List.of(store1, store2);

// when
when(storeRepository.findByNearbyStationContaining("부산역")).thenReturn(Optional.of(stores));
List<StoreResponseDto> responses = storeService.findStoresByStation("부산역");

// then
assertNotNull(responses);
assertEquals(responses.get(0).address(), store1.getAddress());
assertEquals(responses.get(1).address(), store2.getAddress());
}
}
Loading