Skip to content

Commit

Permalink
refactor: GraphService 를 도메인으로 이동
Browse files Browse the repository at this point in the history
- GraphService 를 도메인 패키지로 이동
- 프론트 에러 처리방식 변경
- clean up
  • Loading branch information
fucct committed May 15, 2020
1 parent d0a39f6 commit 34a7f8a
Show file tree
Hide file tree
Showing 25 changed files with 129 additions and 106 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package wooteco.subway.admin.controller;

import java.util.stream.Collectors;

import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.dao.DataAccessException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
Expand All @@ -13,8 +17,18 @@
@RestControllerAdvice
public class GlobalExceptionHandler {

@ExceptionHandler(value = {MethodArgumentNotValidException.class, BusinessException.class})
public ResponseEntity<ErrorResponse> bindingErrorHandler(RuntimeException e) {
@ExceptionHandler(value = MethodArgumentNotValidException.class)
public ResponseEntity<ErrorResponse> validExceptionHandler(MethodArgumentNotValidException e) {
BindingResult bindingResult = e.getBindingResult();
String errorMessage = bindingResult.getAllErrors()
.stream()
.map(DefaultMessageSourceResolvable::getDefaultMessage)
.collect(Collectors.joining("\n"));
return ResponseEntity.badRequest().body(new ErrorResponse(errorMessage));
}

@ExceptionHandler(value = BusinessException.class)
public ResponseEntity<ErrorResponse> bindingErrorHandler(BusinessException e) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.body(new ErrorResponse(e.getMessage()));
}
Expand All @@ -24,4 +38,10 @@ public ResponseEntity<ErrorResponse> bindingErrorHandler(DataAccessException e)
return ResponseEntity.status(HttpStatus.NOT_FOUND)
.body(new ErrorResponse(e.getMessage()));
}

@ExceptionHandler(value = Exception.class)
public ResponseEntity<ErrorResponse> defaultExceptionHandler(Exception e) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.body(new ErrorResponse(e.getMessage()));
}
}
1 change: 0 additions & 1 deletion src/main/java/wooteco/subway/admin/domain/CreateTime.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.time.LocalDateTime;

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

public abstract class CreateTime {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,46 +1,35 @@
package wooteco.subway.admin.service;
package wooteco.subway.admin.domain;

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

import org.jgrapht.alg.shortestpath.DijkstraShortestPath;
import org.jgrapht.graph.WeightedMultigraph;
import org.springframework.stereotype.Service;

import wooteco.subway.admin.domain.Line;
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;

@Service
public class GraphService {
public GraphResponse findPath(List<Line> lines, Long sourceId, Long targetId,
PathType pathType) {
validate(lines, sourceId, targetId);
WeightedMultigraph<Long, LineStationEdge> graph = mapLinesToGraph(lines, pathType);
DijkstraShortestPath<Long, LineStationEdge> dijkstraShortestPath = new DijkstraShortestPath<>(
graph);
public class Graph {

if (Objects.isNull(dijkstraShortestPath.getPath(sourceId, targetId))) {
throw new NotFoundPathException(sourceId, targetId);
}
private final WeightedMultigraph<Long, LineStationEdge> graph;

return mapToGraphResponse(sourceId, targetId, dijkstraShortestPath);
private Graph(
WeightedMultigraph<Long, LineStationEdge> graph) {
this.graph = graph;
}

private void validate(List<Line> lines, Long sourceId, Long targetId) {
if (Objects.isNull(lines)) {
throw new NotFoundLineException();
}
if (Objects.equals(sourceId, targetId)) {
throw new IllegalStationNameException(sourceId, targetId);
}
public static Graph of(List<Line> lines, PathType pathType) {
return new Graph(mapLinesToGraph(lines, pathType));
}

private WeightedMultigraph<Long, LineStationEdge> mapLinesToGraph(List<Line> lines,
private static WeightedMultigraph<Long, LineStationEdge> mapLinesToGraph(List<Line> lines,
PathType pathType) {
if (Objects.isNull(lines)) {
throw new NotFoundLineException();
}
WeightedMultigraph<Long, LineStationEdge> graph
= new WeightedMultigraph(LineStationEdge.class);
lines.stream()
Expand All @@ -55,6 +44,24 @@ private WeightedMultigraph<Long, LineStationEdge> mapLinesToGraph(List<Line> lin
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();
Expand Down
1 change: 1 addition & 0 deletions src/main/java/wooteco/subway/admin/domain/Line.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.springframework.data.annotation.Id;

public class Line extends UpdateTime {

@Id
private Long id;
private String name;
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/wooteco/subway/admin/domain/LineStation.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

public class LineStation {
private Long preStationId;
private Long stationId;
private int distance;
private int duration;
private final Long stationId;
private final int distance;
private final int duration;

public LineStation(Long preStationId, Long stationId, int distance, int duration) {
this.preStationId = preStationId;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package wooteco.subway.admin.service;
package wooteco.subway.admin.domain;

import org.jgrapht.graph.DefaultWeightedEdge;

import wooteco.subway.admin.domain.LineStation;
import wooteco.subway.admin.dto.PathType;

public class LineStationEdge extends DefaultWeightedEdge {

private LineStation lineStation;
private PathType pathType;
private final LineStation lineStation;
private final PathType pathType;

public LineStationEdge(LineStation lineStation, PathType pathType) {
this.lineStation = lineStation;
Expand Down
1 change: 1 addition & 0 deletions src/main/java/wooteco/subway/admin/domain/Station.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.springframework.data.annotation.Id;

public class Station extends CreateTime {

@Id
private Long id;
private String name;
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/wooteco/subway/admin/domain/UpdateTime.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@

import org.springframework.data.annotation.LastModifiedDate;

public abstract class UpdateTime extends CreateTime{
public abstract class UpdateTime extends CreateTime {

@LastModifiedDate
private LocalDateTime updatedAt;


public UpdateTime(){
public UpdateTime() {
}

public UpdateTime(LocalDateTime updatedAt) {
Expand Down
1 change: 0 additions & 1 deletion src/main/java/wooteco/subway/admin/dto/ErrorResponse.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package wooteco.subway.admin.dto;


public class ErrorResponse {

private String errorMessage;
Expand Down
13 changes: 6 additions & 7 deletions src/main/java/wooteco/subway/admin/dto/LineDetailResponse.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package wooteco.subway.admin.dto;

import wooteco.subway.admin.domain.Line;
import wooteco.subway.admin.domain.Station;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import wooteco.subway.admin.domain.Line;
import wooteco.subway.admin.domain.Station;

public class LineDetailResponse {
private Long id;
Expand Down Expand Up @@ -38,10 +36,11 @@ public LineDetailResponse(Long id, String name, String backgroundColor,
}

public static LineDetailResponse of(Line line, List<Station> stations) {
return new LineDetailResponse(line.getId(), line.getName(), line.getBackgroundColor(), line.getStartTime(), line.getEndTime(), line.getIntervalTime(), line.getCreatedAt(), line.getUpdatedAt(), stations);
return new LineDetailResponse(line.getId(), line.getName(), line.getBackgroundColor(),
line.getStartTime(), line.getEndTime(), line.getIntervalTime(), line.getCreatedAt(),
line.getUpdatedAt(), stations);
}


public Long getId() {
return id;
}
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/wooteco/subway/admin/dto/LineRequest.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package wooteco.subway.admin.dto;

import wooteco.subway.admin.domain.Line;

import java.time.LocalTime;
import java.util.ArrayList;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

import wooteco.subway.admin.domain.Line;

public class LineRequest {

@NotBlank(message = "노선 이름은 필수 입력 요소입니다.")
Expand Down
1 change: 1 addition & 0 deletions src/main/java/wooteco/subway/admin/dto/PathRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import javax.validation.constraints.NotBlank;

public class PathRequest {

@NotBlank(message = "이전 역 이름은 필수 입력 요소입니다.")
private String sourceName;
@NotBlank(message = "대상 역 이름은 필수 입력 요소입니다.")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package wooteco.subway.admin.dto;


import javax.validation.constraints.NotBlank;

import wooteco.subway.admin.domain.Station;
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/wooteco/subway/admin/dto/StationResponse.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package wooteco.subway.admin.dto;

import wooteco.subway.admin.domain.Station;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

import wooteco.subway.admin.domain.Station;

public class StationResponse {
private Long id;
private String name;
Expand All @@ -17,8 +17,8 @@ public static StationResponse of(Station station) {

public static List<StationResponse> listOf(List<Station> stations) {
return stations.stream()
.map(StationResponse::of)
.collect(Collectors.toList());
.map(StationResponse::of)
.collect(Collectors.toList());
}

public StationResponse() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package wooteco.subway.admin.dto;

import java.util.List;
import java.util.Map;

public class WholeSubwayResponse {
private List<LineDetailResponse> lineDetailResponse;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
public class IllegalTypeNameException extends BusinessException {

public IllegalTypeNameException(String typeName) {
super(typeName +"방식의 경로는 지원하지 않습니다.");
super(typeName + "방식의 경로는 지원하지 않습니다.");
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package wooteco.subway.admin.repository;

import java.util.List;

import org.springframework.data.repository.CrudRepository;
import wooteco.subway.admin.domain.Line;

import java.util.List;
import wooteco.subway.admin.domain.Line;

public interface LineRepository extends CrudRepository<Line, Long> {
@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package wooteco.subway.admin.repository;

import java.util.List;
import java.util.Optional;

import org.springframework.data.jdbc.repository.query.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
import wooteco.subway.admin.domain.Station;

import java.util.List;
import java.util.Optional;
import wooteco.subway.admin.domain.Station;

public interface StationRepository extends CrudRepository<Station, Long> {
@Override
Expand Down
11 changes: 5 additions & 6 deletions src/main/java/wooteco/subway/admin/service/PathService.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import wooteco.subway.admin.domain.Graph;
import wooteco.subway.admin.domain.Line;
import wooteco.subway.admin.dto.GraphResponse;
import wooteco.subway.admin.dto.PathRequest;
Expand All @@ -20,22 +21,20 @@ public class PathService {

private final StationService stationService;
private final LineService lineService;
private final GraphService graphService;

public PathService(StationService stationService,
LineService lineService, GraphService graphService) {
public PathService(StationService stationService, LineService lineService) {
this.stationService = stationService;
this.lineService = lineService;
this.graphService = graphService;
}

public PathResponse findPath(PathRequest request) {
Long sourceId = stationService.findIdByName(request.getSourceName());
Long targetId = stationService.findIdByName(request.getTargetName());
List<Line> lines = lineService.findAll();

GraphResponse graphResponse = graphService.findPath(lines, sourceId, targetId,
PathType.of(request.getType()));
Graph graph = Graph.of(lines, PathType.of(request.getType()));
GraphResponse graphResponse = graph.findPath(sourceId, targetId);

List<Long> path = graphResponse.getPath();

List<StationResponse> stationResponses = StationResponse.listOf(
Expand Down
Loading

0 comments on commit 34a7f8a

Please sign in to comment.