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

Step3 경로조회 기능 #629

Open
wants to merge 3 commits into
base: owen-q
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions src/main/java/nextstep/subway/SubwayApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Import;

import nextstep.subway.path.ShortedPathFinderConfiguration;

@Import({
ShortedPathFinderConfiguration.class
})
@SpringBootApplication
public class SubwayApplication {

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/nextstep/subway/line/domain/Sections.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import javax.persistence.CascadeType;
import javax.persistence.Embeddable;
import javax.persistence.FetchType;
import javax.persistence.OneToMany;

import lombok.AccessLevel;
Expand All @@ -29,7 +30,7 @@
@AllArgsConstructor
public class Sections {

@OneToMany(mappedBy = "line", cascade = { CascadeType.PERSIST, CascadeType.MERGE}, orphanRemoval = true)
@OneToMany(mappedBy = "line", cascade = { CascadeType.PERSIST, CascadeType.MERGE}, orphanRemoval = true, fetch = FetchType.EAGER)
private List<Section> sections = new ArrayList<>();

public boolean hasOneSection() {
Expand Down Expand Up @@ -213,5 +214,4 @@ public Optional<Section> findSectionByDownStation(Station station) {
.filter(section -> section.equalsDownStation(station))
.findFirst();
}

}
47 changes: 47 additions & 0 deletions src/main/java/nextstep/subway/path/DijkstraShortestPathFinder.java
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;
}
}
28 changes: 28 additions & 0 deletions src/main/java/nextstep/subway/path/PathController.java
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));
}
}

32 changes: 32 additions & 0 deletions src/main/java/nextstep/subway/path/PathFindService.java
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);
}
}
24 changes: 24 additions & 0 deletions src/main/java/nextstep/subway/path/PathResponse.java
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();
}
}
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();
}
}
Comment on lines +6 to +13

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DI로 조립해주셨군요! 👍

18 changes: 18 additions & 0 deletions src/main/java/nextstep/subway/path/ShortestPath.java
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;

}
8 changes: 8 additions & 0 deletions src/main/java/nextstep/subway/path/ShortestPathFinder.java
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);
}
1 change: 1 addition & 0 deletions src/main/java/nextstep/subway/section/domain/Section.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor
@Builder
//@EqualsAndHashCode(of = "id")
public class Section {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,4 @@

@Repository
public interface SectionRepository extends JpaRepository<Section, Long> {

}
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();
}
}
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);
}
}
3 changes: 3 additions & 0 deletions src/main/java/nextstep/subway/support/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@ public enum ErrorCode {
SECTION_NOT_FOUND,
SECTION_ADD_FAIL,
STATION_NOT_ON_SECTIONS,
PATH_SOURCE_TARGET_SHOULD_DIFFERENT,
PATH_SOURCE_TARGET_NOT_CONNECTED,
STATION_NOT_FOUND,
;
}
Loading