Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,19 @@
import lombok.Getter;
import lombok.RequiredArgsConstructor;

import java.util.Map;

@Getter
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
public class RouteDetailDTO {
@Schema(description = "다음 이정표까지의 거리", example = "17.38721484")
private final double dist;
@Schema(description = "좌회전, 우회전, 위험요소 등 정보", example = "RIGHT")
private final DirectionType directionType;
@Schema(description = "상세 경로의 좌표", example = "{\"lag\": 127.123456, \"lat\": 37.123456}")
private final Map<String, Double> coordinates;

public static RouteDetailDTO of(double dist, DirectionType directionType) {
return new RouteDetailDTO(dist, directionType);
public static RouteDetailDTO of(double dist, DirectionType directionType, Map<String, Double> coordinates) {
return new RouteDetailDTO(dist, directionType, coordinates);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.softeer5.uniro_backend.route.dto;

import com.softeer5.uniro_backend.node.entity.Node;
import com.softeer5.uniro_backend.route.entity.CautionType;
import com.softeer5.uniro_backend.route.entity.Route;
import io.swagger.v3.oas.annotations.media.Schema;
Expand All @@ -22,10 +23,10 @@ public class RouteInfoDTO {
@Schema(description = "위험 요소 타입 리스트", example = "[\"SLOPE\", \"STAIRS\"]")
private final Set<CautionType> cautionFactors;

public static RouteInfoDTO of(Route route) {
public static RouteInfoDTO of(Route route, Node node1, Node node2) {
return new RouteInfoDTO(route.getId(),
route.getNode1().getXY(),
route.getNode2().getXY(),
node1.getXY(),
node2.getXY(),
route.getCautionFactors());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public FastestRouteResDTO calculateFastestRoute(Long univId, Long startNodeId, L
double totalDistance = 0.0;

List<RouteInfoDTO> routeInfoDTOS = new ArrayList<>();

Node currentNode = startNode;
// 외부 변수를 수정해야하기 때문에 for-loop문 사용
for (Route route : shortestRoutes) {
totalCost += route.getCost();
Expand All @@ -78,7 +78,15 @@ public FastestRouteResDTO calculateFastestRoute(Long univId, Long startNodeId, L
hasCaution = true;
}

routeInfoDTOS.add(RouteInfoDTO.of(route));
Node firstNode = route.getNode1();
Node secondNode = route.getNode2();
if(currentNode.getId().equals(secondNode.getId())){
Node temp = firstNode;
firstNode = secondNode;
secondNode = temp;
}

routeInfoDTOS.add(RouteInfoDTO.of(route, firstNode, secondNode));
}

List<RouteDetailDTO> details = getRouteDetail(startNode, endNode, shortestRoutes);
Expand Down Expand Up @@ -191,6 +199,8 @@ private List<RouteDetailDTO> getRouteDetail(Node startNode, Node endNode, List<R
List<RouteDetailDTO> details = new ArrayList<>();
double accumulatedDistance = 0.0;
Node now = startNode;
Map<String,Double> checkPointNodeCoordinates = startNode.getXY();
DirectionType checkPointType = DirectionType.STRAIGHT;

// 길찾기 결과 상세정보 정리
for(int i=0;i<shortestRoutes.size();i++){
Expand All @@ -199,12 +209,15 @@ private List<RouteDetailDTO> getRouteDetail(Node startNode, Node endNode, List<R
accumulatedDistance += calculateDistance(nowRoute);

if(!nowRoute.getCautionFactors().isEmpty()){
details.add(RouteDetailDTO.of(accumulatedDistance, DirectionType.CAUTION));
accumulatedDistance = 0.0;
details.add(RouteDetailDTO.of(accumulatedDistance - calculateDistance(nowRoute)/2, checkPointType, checkPointNodeCoordinates));
accumulatedDistance = calculateDistance(nowRoute)/2;
checkPointNodeCoordinates = getCenter(now, nxt);
checkPointType = DirectionType.CAUTION;
Comment on lines +213 to +215
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

getCenter 메서드를 Route 내부로 넣어두는건 어떻게 생각하시는지 궁금합니다 !!

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

오 그게 훨씬 더 좋은 방향인것 같습니다! 반영한 뒤 머지하겠습니다!!

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

생각해보니 getCenter를 Route 내부에서 정의하게되면 메서드가

	public Map<String,Double> getCenter(){
		return Map.of("lat", (node1.getCoordinates().getY() + node2.getCoordinates().getY())/2
				, "lng", (node1.getCoordinates().getX() + node2.getCoordinates().getX())/2);
	}

이렇게 될텐데, 만약 Lazy loading 상태인(fetch join을 하지 않은) Route에서 getCenter를 호출할 경우 추가 쿼리문이 나갈 위험성이 있을것 같습니다.

게다가 현재 Route에서 node1, node2가 (fetch = LAZY) 로 선언되어있기 때문에 더더욱 추가 쿼리문의 가능성이 높아보입니다.

의견 남겨주시면 확인 후 머지하겠습니다!

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

말씀주신 것에 동감합니다!

그래서 전 항상 기능 개발할때 "해당 기능에서 발생하는 쿼리도 의도대로 잘 동작하는지" 까지도 고려해서 기능개발하는 게 좋다고 생각하는데요! 어떻게 생각하시는지 궁금합니다!

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

네 좋습니다! 의도대로 동작하는지 스스로도 점검하고, 앞으로 코드리뷰할때도 꼼꼼하게 보겠습니다!

}

if(nxt.equals(endNode)){
details.add(RouteDetailDTO.of(accumulatedDistance, DirectionType.FINISH));
details.add(RouteDetailDTO.of(accumulatedDistance, checkPointType, checkPointNodeCoordinates));
details.add(RouteDetailDTO.of(0, DirectionType.FINISH, nxt.getXY()));
break;
}
if(nxt.isCore()){
Expand All @@ -213,7 +226,9 @@ private List<RouteDetailDTO> getRouteDetail(Node startNode, Node endNode, List<R
now = nxt;
continue;
}
details.add(RouteDetailDTO.of(accumulatedDistance, directionType));
details.add(RouteDetailDTO.of(accumulatedDistance, checkPointType, checkPointNodeCoordinates));
checkPointNodeCoordinates = nxt.getXY();
checkPointType = directionType;
accumulatedDistance = 0.0;
}

Expand All @@ -223,5 +238,10 @@ private List<RouteDetailDTO> getRouteDetail(Node startNode, Node endNode, List<R
return details;
}

private Map<String,Double> getCenter(Node n1, Node n2){
return Map.of("lat", (n1.getCoordinates().getY() + n2.getCoordinates().getY())/2
, "lng", (n1.getCoordinates().getX() + n2.getCoordinates().getX())/2);
}


}