Skip to content

Commit

Permalink
refactor: 리뷰 반영
Browse files Browse the repository at this point in the history
- path 요청 POST방식에서 GET방식으로 수정
- 시간 관련 데이터 TimeEntity로 통합
- 가독성을 위해 스트림 사이 공백 추가
- 불필요한 생성자 파라미터 삭제
- PathType, GraphResponse˜ 클래스 패키지 이동
- GraphTest 추가
- Algorithm 확장성을 위한 수정
- Algorithm 테스트 추가
  • Loading branch information
fucct committed May 18, 2020
1 parent 34a7f8a commit 9eb43f2
Show file tree
Hide file tree
Showing 21 changed files with 294 additions and 157 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public LineController(LineService lineService) {
}

@PostMapping
public ResponseEntity<LineResponse> createLine(@Valid @RequestBody LineRequest request) {
public ResponseEntity<LineResponse> create(@Valid @RequestBody LineRequest request) {
LineResponse lineResponse = lineService.save(request.toLine());

return ResponseEntity
Expand All @@ -52,14 +52,14 @@ public ResponseEntity<LineResponse> retrieveLine(@PathVariable Long id) {
}

@PutMapping("/{id}")
public ResponseEntity<Void> updateLine(@PathVariable Long id,
public ResponseEntity<Void> update(@PathVariable Long id,
@RequestBody LineRequest request) {
lineService.updateLine(id, request);
return ResponseEntity.ok().build();
}

@DeleteMapping("/{id}")
public ResponseEntity<Void> deleteLine(@PathVariable Long id) {
public ResponseEntity<Void> delete(@PathVariable Long id) {
lineService.deleteLineById(id);
return ResponseEntity.noContent().build();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package wooteco.subway.admin.controller;

import javax.validation.Valid;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import wooteco.subway.admin.dto.PathRequest;
Expand All @@ -22,9 +20,10 @@ public PathController(PathService pathService) {
this.pathService = pathService;
}

@PostMapping
ResponseEntity<PathResponse> findPath(@Valid @RequestBody PathRequest pathRequest) {
PathResponse response = pathService.findPath(pathRequest);
@GetMapping
ResponseEntity<PathResponse> findPath(@RequestParam String sourceName,
@RequestParam String targetName, @RequestParam String type) {
PathResponse response = pathService.findPath(new PathRequest(sourceName, targetName, type));

return ResponseEntity.ok(response);
}
Expand Down
22 changes: 0 additions & 22 deletions src/main/java/wooteco/subway/admin/domain/CreateTime.java

This file was deleted.

51 changes: 10 additions & 41 deletions src/main/java/wooteco/subway/admin/domain/Graph.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,22 @@
import java.util.List;
import java.util.Objects;

import org.jgrapht.alg.shortestpath.DijkstraShortestPath;
import org.jgrapht.graph.WeightedMultigraph;

import wooteco.subway.admin.dto.GraphResponse;
import wooteco.subway.admin.dto.PathType;
import wooteco.subway.admin.exception.IllegalStationNameException;
import wooteco.subway.admin.exception.NotFoundLineException;
import wooteco.subway.admin.exception.NotFoundPathException;

public class Graph {

private final WeightedMultigraph<Long, LineStationEdge> graph;

private Graph(
WeightedMultigraph<Long, LineStationEdge> graph) {
private Graph(WeightedMultigraph<Long, LineStationEdge> graph) {
this.graph = graph;
}

public static Graph of(WeightedMultigraph<Long, LineStationEdge> graph) {
return new Graph(graph);
}

public static Graph of(List<Line> lines, PathType pathType) {
return new Graph(mapLinesToGraph(lines, pathType));
}
Expand All @@ -30,11 +28,12 @@ private static WeightedMultigraph<Long, LineStationEdge> mapLinesToGraph(List<Li
if (Objects.isNull(lines)) {
throw new NotFoundLineException();
}
WeightedMultigraph<Long, LineStationEdge> graph
= new WeightedMultigraph(LineStationEdge.class);
WeightedMultigraph<Long, LineStationEdge> graph = new WeightedMultigraph<>(
LineStationEdge.class);
lines.stream()
.flatMap(it -> it.getLineStationsId().stream())
.forEach(graph::addVertex);

lines.stream()
.flatMap(it -> it.getStations().stream())
.filter(it -> Objects.nonNull(it.getPreStationId()))
Expand All @@ -44,37 +43,7 @@ private static WeightedMultigraph<Long, LineStationEdge> mapLinesToGraph(List<Li
return graph;
}

public GraphResponse findPath(Long sourceId, Long targetId) {
validate(sourceId, targetId);
DijkstraShortestPath<Long, LineStationEdge> dijkstraShortestPath = new DijkstraShortestPath<>(
graph);

if (Objects.isNull(dijkstraShortestPath.getPath(sourceId, targetId))) {
throw new NotFoundPathException(sourceId, targetId);
}

return mapToGraphResponse(sourceId, targetId, dijkstraShortestPath);
}

private void validate(Long sourceId, Long targetId) {
if (Objects.equals(sourceId, targetId)) {
throw new IllegalStationNameException(sourceId, targetId);
}
}

private GraphResponse mapToGraphResponse(Long source, Long target,
DijkstraShortestPath<Long, LineStationEdge> dijkstraShortestPath) {
List<Long> path = dijkstraShortestPath.getPath(source, target).getVertexList();
int totalDistance = dijkstraShortestPath.getPath(source, target)
.getEdgeList()
.stream()
.mapToInt(LineStationEdge::getDistance)
.sum();
int totalDuration = dijkstraShortestPath.getPath(source, target)
.getEdgeList()
.stream()
.mapToInt(LineStationEdge::getDuration)
.sum();
return new GraphResponse(path, totalDistance, totalDuration);
public WeightedMultigraph<Long, LineStationEdge> getGraph() {
return graph;
}
}
6 changes: 2 additions & 4 deletions src/main/java/wooteco/subway/admin/domain/Line.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package wooteco.subway.admin.domain;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashSet;
Expand All @@ -11,7 +10,7 @@

import org.springframework.data.annotation.Id;

public class Line extends UpdateTime {
public class Line extends TimeEntity {

@Id
private Long id;
Expand All @@ -26,8 +25,7 @@ public Line() {
}

public Line(Long id, String name, String backgroundColor, LocalTime startTime,
LocalTime endTime, int intervalTime, LocalDateTime createdAt, LocalDateTime updatedAt,
Set<LineStation> stations) {
LocalTime endTime, int intervalTime, Set<LineStation> stations) {
this(name, backgroundColor, startTime, endTime, intervalTime);
this.id = id;
this.stations = stations;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/wooteco/subway/admin/domain/LineStation.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package wooteco.subway.admin.domain;

public class LineStation {
public class LineStation extends TimeEntity {
private Long preStationId;
private final Long stationId;
private final int distance;
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/wooteco/subway/admin/domain/LineStationEdge.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import org.jgrapht.graph.DefaultWeightedEdge;

import wooteco.subway.admin.dto.PathType;

public class LineStationEdge extends DefaultWeightedEdge {

private final LineStation lineStation;
Expand All @@ -26,4 +24,12 @@ public int getDistance() {
public int getDuration() {
return lineStation.getDuration();
}

public LineStation getLineStation() {
return lineStation;
}

public PathType getPathType() {
return pathType;
}
}
6 changes: 6 additions & 0 deletions src/main/java/wooteco/subway/admin/domain/PathAlgorithm.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package wooteco.subway.admin.domain;

public interface PathAlgorithm {

PathResult findPath(Long sourceId, Long targetId, Graph graph);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package wooteco.subway.admin.domain;

import java.util.List;
import java.util.Objects;

import org.jgrapht.alg.shortestpath.DijkstraShortestPath;
import org.springframework.stereotype.Component;

import wooteco.subway.admin.exception.IllegalStationNameException;
import wooteco.subway.admin.exception.NotFoundPathException;

@Component
public class PathAlgorithmByDijkstra implements PathAlgorithm {

public PathResult findPath(Long sourceId, Long targetId, Graph graph) {
DijkstraShortestPath<Long, LineStationEdge> dijkstraShortestPath =
new DijkstraShortestPath<>(graph.getGraph());
validate(sourceId, targetId);

if (Objects.isNull(dijkstraShortestPath.getPath(sourceId, targetId))) {
throw new NotFoundPathException(sourceId, targetId);
}

return mapToPahtResponse(sourceId, targetId, dijkstraShortestPath);
}

private void validate(Long sourceId, Long targetId) {
if (Objects.equals(sourceId, targetId)) {
throw new IllegalStationNameException(sourceId, targetId);
}
}

private PathResult mapToPahtResponse(Long source, Long target,
DijkstraShortestPath<Long, LineStationEdge> dijkstraShortestPath) {
List<Long> path = dijkstraShortestPath.getPath(source, target).getVertexList();

int totalDistance = dijkstraShortestPath.getPath(source, target)
.getEdgeList()
.stream()
.mapToInt(LineStationEdge::getDistance)
.sum();

int totalDuration = dijkstraShortestPath.getPath(source, target)
.getEdgeList()
.stream()
.mapToInt(LineStationEdge::getDuration)
.sum();
return new PathResult(path, totalDistance, totalDuration);
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package wooteco.subway.admin.dto;
package wooteco.subway.admin.domain;

import java.util.List;

public class GraphResponse {
public class PathResult {
private final List<Long> path;
private final int totalDistance;
private final int totalDuration;

public GraphResponse(List<Long> path, int totalDistance, int totalDuration) {
public PathResult(List<Long> path, int totalDistance, int totalDuration) {
this.path = path;
this.totalDistance = totalDistance;
this.totalDuration = totalDuration;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package wooteco.subway.admin.dto;
package wooteco.subway.admin.domain;

import java.util.Objects;
import java.util.function.Function;

import wooteco.subway.admin.domain.LineStation;
import wooteco.subway.admin.exception.IllegalTypeNameException;

public enum PathType {
Expand All @@ -16,10 +15,6 @@ public enum PathType {
this.function = function;
}

public int getWeight(LineStation lineStation) {
return function.apply(lineStation);
}

public static PathType of(String typeName) {
String upperCaseName = typeName.toUpperCase();
if (!Objects.equals(upperCaseName, DURATION.name()) && !Objects.equals(upperCaseName,
Expand All @@ -28,4 +23,8 @@ public static PathType of(String typeName) {
}
return valueOf(upperCaseName);
}

public int getWeight(LineStation lineStation) {
return function.apply(lineStation);
}
}
2 changes: 1 addition & 1 deletion src/main/java/wooteco/subway/admin/domain/Station.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import org.springframework.data.annotation.Id;

public class Station extends CreateTime {
public class Station extends TimeEntity {

@Id
private Long id;
Expand Down
39 changes: 39 additions & 0 deletions src/main/java/wooteco/subway/admin/domain/TimeEntity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package wooteco.subway.admin.domain;

import java.time.LocalDateTime;

import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;

public abstract class TimeEntity {

@CreatedDate
private LocalDateTime createdAt;

@LastModifiedDate
private LocalDateTime updatedAt;

public TimeEntity() {
}

public TimeEntity(LocalDateTime createdAt, LocalDateTime updatedAt) {
this.createdAt = createdAt;
this.updatedAt = updatedAt;
}

public void create(LocalDateTime createdAt) {
this.createdAt = createdAt;
}

public void update(LocalDateTime updatedAt) {
this.updatedAt = updatedAt;
}

public LocalDateTime getUpdatedAt() {
return updatedAt;
}

public LocalDateTime getCreatedAt() {
return createdAt;
}
}
Loading

0 comments on commit 9eb43f2

Please sign in to comment.