diff --git a/src/main/java/SuMoC/backend/carta/CartaClient.java b/src/main/java/SuMoC/backend/carta/CartaClient.java index 8fc2379..44859c0 100644 --- a/src/main/java/SuMoC/backend/carta/CartaClient.java +++ b/src/main/java/SuMoC/backend/carta/CartaClient.java @@ -52,7 +52,7 @@ public class CartaClient { return cartaApi.getDirection( origin, destination, - mode, + mode, // possible values: walking, driving, cycling arrivalTime, departureTime, alternatives, diff --git a/src/main/java/SuMoC/backend/routing/RoutingController.java b/src/main/java/SuMoC/backend/routing/RoutingController.java index d5e3624..16fa5c6 100644 --- a/src/main/java/SuMoC/backend/routing/RoutingController.java +++ b/src/main/java/SuMoC/backend/routing/RoutingController.java @@ -1,8 +1,8 @@ package SuMoC.backend.routing; -import SuMoC.backend.carta.CartaClient; -import SuMoC.backend.routing.dtos.RouteAlternativeDTO; -import SuMoC.backend.routing.dtos.RouteAlternativeRequestDTO; +import SuMoC.backend.routing.dtos.RouteDTO; +import SuMoC.backend.routing.dtos.RoutingAlternativeRequestDTO; +import SuMoC.backend.routing.dtos.RoutingPreferencesDTO; import jakarta.validation.Valid; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; @@ -17,34 +17,28 @@ import java.util.List; @RequestMapping("/routing/alternative") public class RoutingController { private final RoutingService routingService; - private final CartaClient cartaClient; @Autowired - public RoutingController(RoutingService routingService, CartaClient cartaClient) { + public RoutingController(RoutingService routingService) { this.routingService = routingService; - this.cartaClient = cartaClient; } // TODO operation description + how to handle preferences (optional, if not provided use saved users prefs or default @PostMapping("/request") - public ResponseEntity> requestAlternativeRoutes(@Valid @RequestBody RouteAlternativeRequestDTO routeAlternativeRequestDTO) { - var direction = cartaClient.getRoute( - routeAlternativeRequestDTO.getOrigin(), - routeAlternativeRequestDTO.getDestination(), - "walking", // mode - null, // arrivalTime - null, // departureTime - 1, // alternatives - null, // walkingSpeed - null, // transferTime - null, // accessible - null, // parking - null, // type - null, // range - null // location - ); - var dto = new RouteAlternativeDTO(); - dto.setDirection(direction); - return ResponseEntity.ok(List.of(dto)); + public ResponseEntity> requestAlternativeRoutes(@Valid @RequestBody RoutingAlternativeRequestDTO routingAlternativeRequestDTO) { + RoutingPreferencesDTO preferences = routingAlternativeRequestDTO.getRoutingPreferencesDTO(); + if (preferences == null) { // Use default preferences if none are provided + // TODO we might want to also save users preferences in the future + preferences = new RoutingPreferencesDTO(); // use default preferences + routingAlternativeRequestDTO.setRoutingPreferencesDTO(preferences); + } + + //var routeDTOs = routingService.getAlternativeRoute(routingAlternativeRequestDTO); + + // Mocking response for app development + var routeDTOs = List.of(RouteDTO.getMockRouteDTO()); + + + return ResponseEntity.ok(routeDTOs); } } diff --git a/src/main/java/SuMoC/backend/routing/RoutingService.java b/src/main/java/SuMoC/backend/routing/RoutingService.java index e09a83e..e60273a 100644 --- a/src/main/java/SuMoC/backend/routing/RoutingService.java +++ b/src/main/java/SuMoC/backend/routing/RoutingService.java @@ -1,7 +1,47 @@ package SuMoC.backend.routing; +import SuMoC.backend.carta.CartaClient; +import SuMoC.backend.routing.dtos.RouteDTO; +import SuMoC.backend.routing.dtos.RoutingAlternativeRequestDTO; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import java.util.ArrayList; +import java.util.List; + @Service public class RoutingService { + + private final CartaClient cartaClient; + + @Autowired + public RoutingService(CartaClient cartaClient) { + this.cartaClient = cartaClient; + } + +// // TODO make async for parallel weather calls +// private Direction getDirectionFromCarta(RoutingAlternativeRequestDTO requestDTO) { +// return cartaClient.getRoute( +// requestDTO.getOrigin(), +// requestDTO.getDestination(), +// "walking", // mode +// null, // arrivalTime +// null, // departureTime +// 1, // alternatives +// null, // walkingSpeed +// null, // transferTime +// null, // accessible +// null, // parking +// null, // type +// null, // range +// null // location +// ); +// } + + public List getAlternativeRoute(RoutingAlternativeRequestDTO requestDTO) { + // TODO make parallel requests for different modes + compare with carta web how they request mode combined travel + +// var dto = getDirectionFromCarta(requestDTO); + return new ArrayList(); + } } diff --git a/src/main/java/SuMoC/backend/routing/dtos/RouteAlternativeDTO.java b/src/main/java/SuMoC/backend/routing/dtos/RouteAlternativeDTO.java deleted file mode 100644 index 77e80ce..0000000 --- a/src/main/java/SuMoC/backend/routing/dtos/RouteAlternativeDTO.java +++ /dev/null @@ -1,16 +0,0 @@ -package SuMoC.backend.routing.dtos; - -import sumoc.backend.carta.client.model.Direction; - -public class RouteAlternativeDTO { - private Direction direction; - - // Getters and setters - public Direction getDirection() { - return direction; - } - - public void setDirection(Direction direction) { - this.direction = direction; - } -} diff --git a/src/main/java/SuMoC/backend/routing/dtos/RouteAlternativeRequestDTO.java b/src/main/java/SuMoC/backend/routing/dtos/RouteAlternativeRequestDTO.java deleted file mode 100644 index 499629e..0000000 --- a/src/main/java/SuMoC/backend/routing/dtos/RouteAlternativeRequestDTO.java +++ /dev/null @@ -1,14 +0,0 @@ -package SuMoC.backend.routing.dtos; - -import lombok.Getter; -import lombok.Setter; - -import java.time.LocalDateTime; - -@Getter -@Setter -public class RouteAlternativeRequestDTO { - private String origin; // Lat long - private String destination; // Lat long - private LocalDateTime arrivalTime; -} diff --git a/src/main/java/SuMoC/backend/routing/dtos/RouteDTO.java b/src/main/java/SuMoC/backend/routing/dtos/RouteDTO.java new file mode 100644 index 0000000..3da416f --- /dev/null +++ b/src/main/java/SuMoC/backend/routing/dtos/RouteDTO.java @@ -0,0 +1,88 @@ +package SuMoC.backend.routing.dtos; + +import SuMoC.backend.trip.Modality; +import io.swagger.v3.oas.annotations.media.Schema; +import jakarta.annotation.Nullable; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +import java.time.LocalDateTime; +import java.util.ArrayList; +import java.util.List; + +@Getter +@Setter +@NoArgsConstructor +@AllArgsConstructor +@Schema(description = "This DTO represents a suggested route as a response to a requested alternative route.") +public class RouteDTO { + @Schema(description = "The actual address of the start location.") + private String startAddress; + @Schema(description = "The name of the first stop if the route uses public transport. E.g. \"Essen West\" if the route starts with a footpath to the first train leaving there.") + @Nullable + private String firstStopName; + @Schema(description = "The latitude of the start address.") + private double startLat; + @Schema(description = "The longitude of the start address.") + private double startLon; + @Schema(description = "The actual address of the end location.") + private String endAddress; + @Schema(description = "The name of the last stop if the route uses public transport. E.g. \"Essen Hbf\" if the route ends with a train arriving there (might still contain a footpath afterwards).") + @Nullable + private String lastStopName; + @Schema(description = "The latitude of the end address.") + private String endLat; + @Schema(description = "The longitude of the end address.") + private String endLon; + @Schema(description = "The start date and time of the route.") + private LocalDateTime startDateTime; + @Schema(description = "The end date and time of the route.") + private LocalDateTime endDateTime; + @Schema(description = "The steps the route consists of.") + private List steps; + @Schema(description = "Amount of CO2e savings in gramm compared to a car trip for the same route.") + private float co2Savings; + @Schema(description = "The temporal difference in minutes compared to the original route the user requested alternatives for.") + private float timeDifferenceMinutes; + + /** + * Mock method for testing purposes + * + * @return RouteDto with mock data + */ + public static RouteDTO getMockRouteDTO() { + var dto = new RouteDTO( + "Janssenstraße 28a, 45147 Essen, Germany", + "Rubensstraße", + 51.440395, + 6.984785, + "Schützenbahn 70, 45127 Essen, Germany", + "Viehhofer Platz", + "51.459167", + "7.014928", + LocalDateTime.now().minusMinutes(30), + LocalDateTime.now(), + new ArrayList(), + 120.5f, + +16.3f + ); + dto.steps.add(new StepDTO( + Modality.WALK, + null, + 400f + )); + dto.steps.add(new StepDTO( + Modality.TRAM, + "106", + 5000f + )); + dto.steps.add(new StepDTO( + Modality.WALK, + null, + 300f + )); + return dto; + } +} diff --git a/src/main/java/SuMoC/backend/routing/dtos/RoutingAlternativeRequestDTO.java b/src/main/java/SuMoC/backend/routing/dtos/RoutingAlternativeRequestDTO.java new file mode 100644 index 0000000..de0552f --- /dev/null +++ b/src/main/java/SuMoC/backend/routing/dtos/RoutingAlternativeRequestDTO.java @@ -0,0 +1,29 @@ +package SuMoC.backend.routing.dtos; + +import io.swagger.v3.oas.annotations.media.Schema; +import jakarta.annotation.Nullable; +import jakarta.validation.constraints.NotNull; +import lombok.Getter; +import lombok.Setter; + +import java.time.LocalDateTime; + +@Getter +@Setter +@Schema(description = "This DTO represents a request for alternative routes between an origin and a destination.") +public class RoutingAlternativeRequestDTO { + @NotNull + private String originLat; + @NotNull + private String originLon; + @NotNull + private String destinationLat; + @NotNull + private String destinationLon; + @NotNull + private LocalDateTime arrivalTime; + @NotNull + private LocalDateTime departureTime; // We need both times to calculate duration to compare alternatives + @Nullable + private RoutingPreferencesDTO routingPreferencesDTO; +} diff --git a/src/main/java/SuMoC/backend/routing/dtos/RoutingPreferencesDTO.java b/src/main/java/SuMoC/backend/routing/dtos/RoutingPreferencesDTO.java index 33c7f51..92c2777 100644 --- a/src/main/java/SuMoC/backend/routing/dtos/RoutingPreferencesDTO.java +++ b/src/main/java/SuMoC/backend/routing/dtos/RoutingPreferencesDTO.java @@ -1,18 +1,20 @@ package SuMoC.backend.routing.dtos; import SuMoC.backend.trip.Modality; +import io.swagger.v3.oas.annotations.media.Schema; import lombok.Getter; import lombok.Setter; +import java.util.ArrayList; import java.util.List; -/** - * This DTO represents the user preferences for routing / alternative route suggestions. - * Provides default values for all fields. - */ @Getter @Setter +@Schema(description = "This DTO represents the user preferences for routing / alternative route suggestions.") public class RoutingPreferencesDTO { private boolean preferPhysicalActivity = false; - private List excludedModalities = List.of(); + private boolean considerWeatherConditions = true; + private List modalitiesToConsider = new ArrayList<>( + List.of(Modality.WALK, Modality.BIKE, Modality.ESCOOTER, Modality.BUS, Modality.TRAM, Modality.TRAIN) + ); } diff --git a/src/main/java/SuMoC/backend/routing/dtos/StepDTO.java b/src/main/java/SuMoC/backend/routing/dtos/StepDTO.java new file mode 100644 index 0000000..6dad149 --- /dev/null +++ b/src/main/java/SuMoC/backend/routing/dtos/StepDTO.java @@ -0,0 +1,19 @@ +package SuMoC.backend.routing.dtos; + +import SuMoC.backend.trip.Modality; +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +@Getter +@Setter +@NoArgsConstructor +@AllArgsConstructor +@Schema(description = "A single step within a suggested alternative route.") +public class StepDTO { + private Modality modality; + private String lineNumber; + private float distanceMeters; +} diff --git a/src/main/java/SuMoC/backend/trip/Modality.java b/src/main/java/SuMoC/backend/trip/Modality.java index 676a7dc..7f2301b 100644 --- a/src/main/java/SuMoC/backend/trip/Modality.java +++ b/src/main/java/SuMoC/backend/trip/Modality.java @@ -2,7 +2,7 @@ package SuMoC.backend.trip; public enum Modality { IDLE, - WALKING, + WALK, BIKE, ESCOOTER, BUS, diff --git a/src/main/java/SuMoC/backend/trip/dtos/TripDTO.java b/src/main/java/SuMoC/backend/trip/dtos/TripDTO.java index fda5728..2c6ae17 100644 --- a/src/main/java/SuMoC/backend/trip/dtos/TripDTO.java +++ b/src/main/java/SuMoC/backend/trip/dtos/TripDTO.java @@ -1,11 +1,17 @@ package SuMoC.backend.trip.dtos; +import lombok.Getter; +import lombok.Setter; + import java.time.LocalDateTime; import java.util.List; /** * DTO representing a trip already persisted in the database. */ +// TODO refactor to UploadedTripDTO for clarity against RouteDTO +@Setter +@Getter public class TripDTO { private long tripId; private int userId; @@ -16,7 +22,8 @@ public class TripDTO { private float mobilityScore; private List tripSegments; - public TripDTO() {} + public TripDTO() { + } public TripDTO(int userId, String startPlace, String endPlace, LocalDateTime startTime, LocalDateTime endTime, float mobilityScore, List tripSegmentDTOS) { this.userId = userId; @@ -28,67 +35,4 @@ public class TripDTO { this.tripSegments = tripSegmentDTOS; } - public long getTripId() { - return tripId; - } - - public void setTripId(long tripId) { - this.tripId = tripId; - } - - public int getUserId() { - return userId; - } - - public void setUserId(int userId) { - this.userId = userId; - } - - public String getStartPlace() { - return startPlace; - } - - public void setStartPlace(String startPlace) { - this.startPlace = startPlace; - } - - public String getEndPlace() { - return endPlace; - } - - public void setEndPlace(String endPlace) { - this.endPlace = endPlace; - } - - public LocalDateTime getStartTime() { - return startTime; - } - - public void setStartTime(LocalDateTime startTime) { - this.startTime = startTime; - } - - public LocalDateTime getEndTime() { - return endTime; - } - - public void setEndTime(LocalDateTime endTime) { - this.endTime = endTime; - } - - public float getMobilityScore() { - return mobilityScore; - } - - public void setMobilityScore(float mobilityScore) { - this.mobilityScore = mobilityScore; - } - - public List getTripSegments() { - return tripSegments; - } - - public void setTripSegments(List tripSegments) { - this.tripSegments = tripSegments; - } }