-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 서버에서 충전소 정보들을 필터링 하는 기능을 만든다 (#218)
* feat: 필터링 기능 추가 * test: 필터링 기능 테스트 및 기존 코드 리팩토링 진행 * feat: List<Station>을 관리하는 객체 생성 * test: List<Station>을 관리하는 객체 테스트 * refactor: getValue()에서 다른 부분과 겹치지 않기 위해서 따로 getter 재정의 * feat: 인덱스 적용 및 효율적인 연산을 위해서 fetch join 쿼리에서 BETWEEN 연산자 제거 및 범위 쿼리 사용 * refactor: 불필요한 인덱스 제거 * refactor: 코드 컨벤션 정리 및 일부 리팩토링 진행 * refactor: 에러 방지를 위해 fixture의 날짜를 수동으로 설정 * refactor: 네이밍 및 메서드 구조 변경 * refactor: 네이밍 및 메서드 구조 변경 * refactor: 테스트 통합화 * refactor: 데이터를 가져올 때 INNER JOIN으로 가져와서 null인 값들은 제외하도록 변경 * refactor: 메서드 분리 * refactor: 값을 복사하고 반환하도록 변경
- Loading branch information
Showing
13 changed files
with
362 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
backend/src/main/java/com/carffeine/carffeine/station/domain/station/Coordinate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.carffeine.carffeine.station.domain.station; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
import java.math.BigDecimal; | ||
|
||
@AllArgsConstructor(access = AccessLevel.PRIVATE) | ||
@Getter | ||
public class Coordinate { | ||
|
||
private final Latitude minLatitude; | ||
private final Latitude maxLatitude; | ||
private final Longitude minLongitude; | ||
private final Longitude maxLongitude; | ||
|
||
public static Coordinate of(BigDecimal latitude, | ||
BigDecimal latitudeDelta, | ||
BigDecimal longitude, | ||
BigDecimal longitudeDelta) { | ||
Latitude originLatitude = Latitude.from(latitude); | ||
Latitude minLatitude = originLatitude.calculateMinLatitudeByDelta(latitudeDelta); | ||
Latitude maxLatitude = originLatitude.calculateMaxLatitudeByDelta(latitudeDelta); | ||
|
||
Longitude originLongitude = Longitude.from(longitude); | ||
Longitude minLongitude = originLongitude.calculateMinLongitudeByDelta(longitudeDelta); | ||
Longitude maxLongitude = originLongitude.calculateMaxLongitudeByDelta(longitudeDelta); | ||
|
||
return new Coordinate(minLatitude, maxLatitude, minLongitude, maxLongitude); | ||
} | ||
|
||
public BigDecimal minLatitudeValue() { | ||
return minLatitude.getValue(); | ||
} | ||
|
||
public BigDecimal maxLatitudeValue() { | ||
return maxLatitude.getValue(); | ||
} | ||
|
||
public BigDecimal minLongitudeValue() { | ||
return minLongitude.getValue(); | ||
} | ||
|
||
public BigDecimal maxLongitudeValue() { | ||
return maxLongitude.getValue(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
backend/src/main/java/com/carffeine/carffeine/station/domain/station/Stations.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package com.carffeine.carffeine.station.domain.station; | ||
|
||
import com.carffeine.carffeine.station.domain.charger.ChargerType; | ||
|
||
import java.math.BigDecimal; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
public class Stations { | ||
|
||
private final List<Station> stations; | ||
|
||
private Stations(List<Station> stations) { | ||
this.stations = new ArrayList<>(stations); | ||
} | ||
|
||
public static Stations from(List<Station> stations) { | ||
return new Stations(stations); | ||
} | ||
|
||
private static void filterByCompanyNames(List<Station> stations, List<String> companyNames) { | ||
if (!companyNames.isEmpty()) { | ||
stations.removeIf(station -> !companyNames.contains(station.getCompanyName())); | ||
} | ||
} | ||
|
||
public List<Station> findFilteredStations(List<String> companyNames, | ||
List<ChargerType> chargerTypes, | ||
List<BigDecimal> capacities) { | ||
List<Station> stations = new ArrayList<>(this.stations); | ||
filterByCompanyNames(stations, companyNames); | ||
filterByChargerTypes(stations, chargerTypes); | ||
filterByCapacities(stations, capacities); | ||
return Collections.unmodifiableList(stations); | ||
} | ||
|
||
private void filterByChargerTypes(List<Station> stations, List<ChargerType> chargerTypes) { | ||
if (!chargerTypes.isEmpty()) { | ||
stations.removeIf(station -> station | ||
.getChargers() | ||
.stream() | ||
.noneMatch(charger -> chargerTypes.contains(charger.getType())) | ||
); | ||
} | ||
} | ||
|
||
private void filterByCapacities(List<Station> stations, List<BigDecimal> capacities) { | ||
if (!capacities.isEmpty()) { | ||
stations.removeIf(station -> station | ||
.getChargers() | ||
.stream() | ||
.noneMatch(charger -> capacities.stream() | ||
.anyMatch(capacity -> isSameCapacity(charger.getCapacity(), capacity)) | ||
)); | ||
} | ||
} | ||
|
||
private boolean isSameCapacity(BigDecimal capacity, BigDecimal filterCapacity) { | ||
return capacity.compareTo(filterCapacity) == 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
backend/src/test/java/com/carffeine/carffeine/station/domain/station/StationsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package com.carffeine.carffeine.station.domain.station; | ||
|
||
import com.carffeine.carffeine.station.domain.charger.ChargerType; | ||
import org.junit.jupiter.api.DisplayNameGeneration; | ||
import org.junit.jupiter.api.DisplayNameGenerator; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.math.BigDecimal; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static com.carffeine.carffeine.station.fixture.station.StationFixture.선릉역_충전소_충전기_2개_사용가능_1개_이름_변경됨; | ||
import static com.carffeine.carffeine.station.fixture.station.StationFixture.천호역_충전소_충전기_2개_사용가능_1개; | ||
import static org.assertj.core.api.SoftAssertions.assertSoftly; | ||
|
||
@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) | ||
@SuppressWarnings("NonAsciiCharacters") | ||
class StationsTest { | ||
|
||
@Test | ||
void 회사_이름을_기준으로_충전소를_필터링한다() { | ||
// given | ||
Stations stations = Stations.from(List.of( | ||
천호역_충전소_충전기_2개_사용가능_1개, | ||
선릉역_충전소_충전기_2개_사용가능_1개_이름_변경됨 | ||
)); | ||
|
||
// when | ||
List<Station> result = stations.findFilteredStations(List.of("볼튼"), new ArrayList<>(), new ArrayList<>()); | ||
|
||
// then | ||
assertSoftly(softly -> { | ||
softly.assertThat(result.size()).isEqualTo(1); | ||
softly.assertThat(result.get(0).getStationId()).isEqualTo(선릉역_충전소_충전기_2개_사용가능_1개_이름_변경됨.getStationId()); | ||
}); | ||
} | ||
|
||
@Test | ||
void 충전기_타입을_기준으로_충전소를_필터링한다() { | ||
// given | ||
Stations stations = Stations.from(List.of( | ||
천호역_충전소_충전기_2개_사용가능_1개, | ||
선릉역_충전소_충전기_2개_사용가능_1개_이름_변경됨 | ||
)); | ||
|
||
// when | ||
List<Station> result = stations.findFilteredStations(new ArrayList<>(), List.of(ChargerType.AC_SLOW), new ArrayList<>()); | ||
|
||
// then | ||
assertSoftly(softly -> { | ||
softly.assertThat(result.size()).isEqualTo(1); | ||
softly.assertThat(result.get(0).getStationId()).isEqualTo(천호역_충전소_충전기_2개_사용가능_1개.getStationId()); | ||
}); | ||
} | ||
|
||
@Test | ||
void 충전기_속도를_기준으로_충전소를_필터링한다() { | ||
// given | ||
Stations stations = Stations.from(List.of( | ||
천호역_충전소_충전기_2개_사용가능_1개, | ||
선릉역_충전소_충전기_2개_사용가능_1개_이름_변경됨 | ||
)); | ||
|
||
// when | ||
List<Station> result = stations.findFilteredStations(new ArrayList<>(), new ArrayList<>(), List.of(BigDecimal.valueOf(100.00))); | ||
|
||
// then | ||
assertSoftly(softly -> { | ||
softly.assertThat(result.size()).isEqualTo(1); | ||
softly.assertThat(result.get(0).getStationId()).isEqualTo(천호역_충전소_충전기_2개_사용가능_1개.getStationId()); | ||
}); | ||
} | ||
} |
Oops, something went wrong.