-
Notifications
You must be signed in to change notification settings - Fork 279
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
Step3 경로조회 기능 #629
Open
owen-q
wants to merge
3
commits into
next-step:owen-q
Choose a base branch
from
owen-q:step3
base: owen-q
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Step3 경로조회 기능 #629
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
47 changes: 47 additions & 0 deletions
47
src/main/java/nextstep/subway/path/DijkstraShortestPathFinder.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,47 @@ | ||
package nextstep.subway.path; | ||
|
||
import org.jgrapht.GraphPath; | ||
import org.jgrapht.alg.shortestpath.DijkstraShortestPath; | ||
import org.jgrapht.graph.DefaultWeightedEdge; | ||
import org.jgrapht.graph.WeightedMultigraph; | ||
|
||
import nextstep.subway.line.domain.Sections; | ||
import nextstep.subway.section.domain.Section; | ||
import nextstep.subway.station.domain.Station; | ||
import nextstep.subway.support.ErrorCode; | ||
import nextstep.subway.support.SubwayException; | ||
|
||
public class DijkstraShortestPathFinder implements ShortestPathFinder { | ||
|
||
@Override | ||
public ShortestPath find(Sections sections, Station source, Station target) { | ||
WeightedMultigraph<Station, DefaultWeightedEdge> graph = createGraph(sections); | ||
|
||
DijkstraShortestPath dijkstraShortestPath = new DijkstraShortestPath(graph); | ||
GraphPath<Station, ShortestPath> result = dijkstraShortestPath.getPath(source, target); | ||
|
||
if (result == null) { | ||
throw new SubwayException(ErrorCode.PATH_SOURCE_TARGET_NOT_CONNECTED); | ||
} | ||
|
||
return new ShortestPath(result.getVertexList(), (int) result.getWeight()); | ||
} | ||
|
||
private WeightedMultigraph<Station, DefaultWeightedEdge> createGraph(Sections sections) { | ||
WeightedMultigraph<Station, DefaultWeightedEdge> graph = new WeightedMultigraph(DefaultWeightedEdge.class); | ||
|
||
for (Section section : sections.getSections()) { | ||
if (!graph.containsVertex(section.getUpStation())) { | ||
graph.addVertex(section.getUpStation()); | ||
} | ||
|
||
if (!graph.containsVertex(section.getDownStation())) { | ||
graph.addVertex(section.getDownStation()); | ||
} | ||
|
||
graph.setEdgeWeight(graph.addEdge(section.getUpStation(), section.getDownStation()), section.getDistance()); | ||
} | ||
|
||
return graph; | ||
} | ||
} |
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,28 @@ | ||
package nextstep.subway.path; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import nextstep.subway.station.service.StationService; | ||
|
||
@RestController | ||
@Slf4j | ||
@RequiredArgsConstructor | ||
public class PathController { | ||
private final PathFindService pathFindService; | ||
private final StationService stationService; | ||
|
||
@GetMapping("/paths") | ||
public ResponseEntity<PathResponse> getPaths(@RequestParam("source") Long sourceStationId, @RequestParam("target") Long targetStationId) { | ||
ShortestPath shortestPath = pathFindService.getPath(stationService.get(sourceStationId), stationService.get(targetStationId)); | ||
|
||
return ResponseEntity.ok() | ||
.body(new PathResponse(shortestPath)); | ||
} | ||
} | ||
|
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,32 @@ | ||
package nextstep.subway.path; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import nextstep.subway.line.domain.Sections; | ||
import nextstep.subway.section.service.SectionReadService; | ||
import nextstep.subway.station.domain.Station; | ||
import nextstep.subway.support.ErrorCode; | ||
import nextstep.subway.support.SubwayException; | ||
|
||
@Service | ||
@Slf4j | ||
@RequiredArgsConstructor | ||
public class PathFindService { | ||
private final SectionReadService sectionReadService; | ||
private final ShortestPathFinder shortestPathFinder; | ||
|
||
@Transactional(readOnly = true) | ||
public ShortestPath getPath(Station sourceStation, Station targetStation) { | ||
if (sourceStation.equals(targetStation)) { | ||
throw new SubwayException(ErrorCode.PATH_SOURCE_TARGET_SHOULD_DIFFERENT); | ||
} | ||
|
||
Sections allSections = new Sections(sectionReadService.getAll()); | ||
|
||
return shortestPathFinder.find(allSections, sourceStation, targetStation); | ||
} | ||
} |
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,24 @@ | ||
package nextstep.subway.path; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import lombok.Getter; | ||
|
||
import nextstep.subway.station.view.StationResponse; | ||
|
||
@Getter | ||
public class PathResponse { | ||
|
||
private List<StationResponse> stations; | ||
private int distance; | ||
|
||
public PathResponse(ShortestPath shortestPath) { | ||
this.stations = shortestPath.getStations() | ||
.stream() | ||
.map(StationResponse::new) | ||
.collect(Collectors.toList()); | ||
|
||
this.distance = shortestPath.getDistance(); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/nextstep/subway/path/ShortedPathFinderConfiguration.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,13 @@ | ||
package nextstep.subway.path; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
public class ShortedPathFinderConfiguration { | ||
|
||
@Bean | ||
public ShortestPathFinder shortestPathFinder() { | ||
return new DijkstraShortestPathFinder(); | ||
} | ||
} | ||
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,18 @@ | ||
package nextstep.subway.path; | ||
|
||
import java.util.List; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
import nextstep.subway.station.domain.Station; | ||
|
||
@Getter | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class ShortestPath { | ||
private List<Station> stations; | ||
private int distance; | ||
|
||
} |
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,8 @@ | ||
package nextstep.subway.path; | ||
|
||
import nextstep.subway.line.domain.Sections; | ||
import nextstep.subway.station.domain.Station; | ||
|
||
public interface ShortestPathFinder { | ||
ShortestPath find(Sections sections, Station sourceStation, Station targetStation); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,5 +7,4 @@ | |
|
||
@Repository | ||
public interface SectionRepository extends JpaRepository<Section, Long> { | ||
|
||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/nextstep/subway/section/service/SectionReadService.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,24 @@ | ||
package nextstep.subway.section.service; | ||
|
||
import java.util.List; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import nextstep.subway.section.domain.Section; | ||
import nextstep.subway.section.repository.SectionRepository; | ||
|
||
@Service | ||
@Slf4j | ||
@RequiredArgsConstructor | ||
@Transactional(readOnly = true) | ||
public class SectionReadService { | ||
private final SectionRepository sectionRepository; | ||
|
||
public List<Section> getAll() { | ||
return sectionRepository.findAll(); | ||
} | ||
} |
7 changes: 6 additions & 1 deletion
7
src/main/java/nextstep/subway/station/exception/StationNotFoundException.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 |
---|---|---|
@@ -1,5 +1,10 @@ | ||
package nextstep.subway.station.exception; | ||
|
||
public class StationNotFoundException extends RuntimeException { | ||
import nextstep.subway.support.ErrorCode; | ||
import nextstep.subway.support.SubwayException; | ||
|
||
public class StationNotFoundException extends SubwayException { | ||
public StationNotFoundException() { | ||
super(ErrorCode.STATION_NOT_FOUND); | ||
} | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DI로 조립해주셨군요! 👍