diff --git a/src/main/java/SuMoC/backend/routing/controller/RoutingController.java b/src/main/java/SuMoC/backend/routing/controller/RoutingController.java index 8a8e4d7..ca642d8 100644 --- a/src/main/java/SuMoC/backend/routing/controller/RoutingController.java +++ b/src/main/java/SuMoC/backend/routing/controller/RoutingController.java @@ -2,11 +2,11 @@ package SuMoC.backend.routing.controller; import SuMoC.backend.routing.dtos.RouteDTO; import SuMoC.backend.routing.dtos.RoutingAlternativeRequestDTO; -import SuMoC.backend.routing.dtos.RoutingPreferencesDTO; +import SuMoC.backend.routing.services.RoutingPreferencesService; import SuMoC.backend.routing.services.RoutingService; import io.swagger.v3.oas.annotations.media.Schema; import jakarta.validation.Valid; -import org.springframework.beans.factory.annotation.Autowired; +import lombok.RequiredArgsConstructor; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; @@ -20,25 +20,22 @@ import java.util.logging.Logger; @RestController @RequestMapping("/routing/alternative") +@RequiredArgsConstructor public class RoutingController { private final Logger logger = Logger.getLogger(RoutingController.class.getName()); private final RoutingService routingService; - @Autowired - public RoutingController(RoutingService routingService) { - this.routingService = routingService; - } + private final RoutingPreferencesService routingPreferencesService; @PostMapping("/request") @Schema(description = "Calculates alternative, sustainable routes for the request. The delta of departure and arrival in the requestdto is used as baseline to determine if alternatives are acceptable.") public ResponseEntity> requestAlternativeRoutes(@Valid @RequestBody RoutingAlternativeRequestDTO routingAlternativeRequestDTO) { try { - if (routingAlternativeRequestDTO.getRoutingPreferencesDTO() == null) { // Use default preferences if none are provided - // TODO we might want to also save users preferences in the future - var preferences = new RoutingPreferencesDTO(); // use default preferences - routingAlternativeRequestDTO.setRoutingPreferencesDTO(preferences); + if (routingAlternativeRequestDTO.getRoutingPreferencesDTO() == null) { + // This automatically uses default preferences for unauthenticated users + routingAlternativeRequestDTO.setRoutingPreferencesDTO(routingPreferencesService.getRoutingPreferencesForCurrentUser()); } var routeDTOs = routingService.determineAlternativeRoutesOnUserRequest(routingAlternativeRequestDTO); diff --git a/src/main/java/SuMoC/backend/routing/services/RoutingPreferencesService.java b/src/main/java/SuMoC/backend/routing/services/RoutingPreferencesService.java index 6056ce7..558ac21 100644 --- a/src/main/java/SuMoC/backend/routing/services/RoutingPreferencesService.java +++ b/src/main/java/SuMoC/backend/routing/services/RoutingPreferencesService.java @@ -5,6 +5,7 @@ import SuMoC.backend.routing.dtos.RoutingPreferencesDTO; import SuMoC.backend.routing.mapper.RoutingPreferencesMapper; import SuMoC.backend.routing.models.RoutingPreferences; import SuMoC.backend.routing.repository.RoutingPreferencesRepository; +import SuMoC.backend.user.service.CurrentUserService; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; @@ -18,6 +19,8 @@ public class RoutingPreferencesService { private final RoutingPreferencesMapper routingPreferencesMapper; + private final CurrentUserService currentUserService; + public void updateUserRoutingPreferences(Long userId, RoutingPreferencesDTO newPreferencesDTO) { Optional optionalPreferences = routingPreferencesRepository.findByUserId(userId); @@ -29,6 +32,9 @@ public class RoutingPreferencesService { routingPreferencesRepository.save(preferences); } + /** + * Get routing preferences for a specific user. + */ public RoutingPreferencesDTO getUserRoutingPreferences(Long userId) { Optional optionalPreferences = routingPreferencesRepository.findByUserId(userId); @@ -41,4 +47,16 @@ public class RoutingPreferencesService { } return preferencesDTO; } + + /** + * Gets routing preferences for the current user, if user is logged in returns his preferences, if not logged in returns default preferences. + */ + public RoutingPreferencesDTO getRoutingPreferencesForCurrentUser() { + if (currentUserService.isAuthenticated()) { + var currentUser = currentUserService.get(); + return getUserRoutingPreferences(currentUser.getId()); + } else { + return routingPreferencesMapper.toRoutingPreferencesDTO(RoutingPreferences.getDefaultPreferences()); + } + } } diff --git a/src/main/java/SuMoC/backend/suggestion/services/AdHocSuggestionService.java b/src/main/java/SuMoC/backend/suggestion/services/AdHocSuggestionService.java index ad90735..be623a8 100644 --- a/src/main/java/SuMoC/backend/suggestion/services/AdHocSuggestionService.java +++ b/src/main/java/SuMoC/backend/suggestion/services/AdHocSuggestionService.java @@ -6,6 +6,7 @@ import SuMoC.backend.notification.services.NotificationSchedulerService; import SuMoC.backend.routing.dtos.RouteDTO; import SuMoC.backend.routing.dtos.RoutingAlternativeRequestDTO; import SuMoC.backend.routing.dtos.RoutingPreferencesDTO; +import SuMoC.backend.routing.services.RoutingPreferencesService; import SuMoC.backend.routing.services.RoutingService; import SuMoC.backend.suggestion.config.AdHocSuggestionProperties; import SuMoC.backend.trip.Modality; @@ -24,6 +25,8 @@ public class AdHocSuggestionService { private final RoutingService routingService; + private final RoutingPreferencesService routingPreferencesService; + private final AdHocSuggestionProperties adHocSuggestionProperties; private final NotificationSchedulerService notificationSchedulerService; @@ -36,15 +39,14 @@ public class AdHocSuggestionService { public void checkNewTripForNotifiedAlternativeSuggestion(Trip newTrip) { if (!isRecentTrip(newTrip)) return; if (!(newTrip.getMainModality().equals(Modality.CAR) || newTrip.getMainModality().equals(Modality.MOTORCYCLE) || newTrip.getMainModality().equals(Modality.TAXI))) { - return ; // only suggest alternatives for trips mainly done by car, motorcycle or taxi + return; // only suggest alternatives for trips mainly done by car, motorcycle or taxi } var viableAlternatives = routingService.determineAlternativeRoutesAdHoc(alternativeRequestFromTrip(newTrip)); if (!viableAlternatives.isEmpty()) { // Since notification bodies have a size limit, we only send the best alternative - // TODO we assume default preferences, in the future we should store user preferences and use them here - RoutingPreferencesDTO preferencesDTO = new RoutingPreferencesDTO(); + RoutingPreferencesDTO preferencesDTO = routingPreferencesService.getUserRoutingPreferences(newTrip.getUser().getId()); List bestAlternatives = pickBestAlternatives(viableAlternatives, preferencesDTO); notificationSchedulerService.schedule( @@ -90,8 +92,7 @@ public class AdHocSuggestionService { } private RoutingAlternativeRequestDTO alternativeRequestFromTrip(Trip trip) { - // TODO for now we assume the preferences here, it would be better if we store these per user - var preferences = new RoutingPreferencesDTO(); + var preferences = routingPreferencesService.getUserRoutingPreferences(trip.getUser().getId()); return RoutingAlternativeRequestDTO.builder() .originLat(trip.getStartLatitude()) .originLon(trip.getStartLongitude()) diff --git a/src/main/java/SuMoC/backend/suggestion/services/RegularTripSuggestionService.java b/src/main/java/SuMoC/backend/suggestion/services/RegularTripSuggestionService.java index df69c8f..3ffc48b 100644 --- a/src/main/java/SuMoC/backend/suggestion/services/RegularTripSuggestionService.java +++ b/src/main/java/SuMoC/backend/suggestion/services/RegularTripSuggestionService.java @@ -7,6 +7,7 @@ import SuMoC.backend.regulartrip.dtos.DetectedRegularTripDTO; import SuMoC.backend.regulartrip.services.RegularTripDetectionService; import SuMoC.backend.routing.dtos.RoutingAlternativeRequestDTO; import SuMoC.backend.routing.dtos.RoutingPreferencesDTO; +import SuMoC.backend.routing.services.RoutingPreferencesService; import SuMoC.backend.routing.services.RoutingService; import SuMoC.backend.suggestion.config.RegularTripSuggestionProperties; import com.uber.h3core.util.LatLng; @@ -26,6 +27,8 @@ public class RegularTripSuggestionService { private final RoutingService routingService; + private final RoutingPreferencesService routingPreferencesService; + private final RegularTripSuggestionProperties properties; private final NotificationSchedulerService notificationSchedulerService; @@ -46,8 +49,7 @@ public class RegularTripSuggestionService { public void checkAlternativesAndNotify(HashMap> detectedRegularTripsByUser) { for (Long userId : detectedRegularTripsByUser.keySet()) { var detectedRegularTrips = detectedRegularTripsByUser.get(userId); - // TODO for now we use default preferences, in the future we should store user preferences and use them here - RoutingPreferencesDTO preferencesDTO = new RoutingPreferencesDTO(); + RoutingPreferencesDTO preferencesDTO = routingPreferencesService.getUserRoutingPreferences(userId); // For each detected regular trip, determine alternatives and notify the user if applicable for (var detectedRegularTrip : detectedRegularTrips) { diff --git a/src/main/java/SuMoC/backend/user/service/CurrentUserService.java b/src/main/java/SuMoC/backend/user/service/CurrentUserService.java index e8e5554..c4a6f19 100644 --- a/src/main/java/SuMoC/backend/user/service/CurrentUserService.java +++ b/src/main/java/SuMoC/backend/user/service/CurrentUserService.java @@ -31,4 +31,9 @@ public class CurrentUserService { HttpStatus.NOT_FOUND, "Der Nutzer existiert nicht")); } + + public boolean isAuthenticated() { + Authentication auth = SecurityContextHolder.getContext().getAuthentication(); + return auth != null && auth.isAuthenticated(); + } } \ No newline at end of file