63 lines
2.7 KiB
Java
63 lines
2.7 KiB
Java
package SuMoC.backend.routing.services;
|
|
|
|
|
|
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;
|
|
|
|
import java.util.Optional;
|
|
|
|
@Service
|
|
@RequiredArgsConstructor
|
|
public class RoutingPreferencesService {
|
|
|
|
private final RoutingPreferencesRepository routingPreferencesRepository;
|
|
|
|
private final RoutingPreferencesMapper routingPreferencesMapper;
|
|
|
|
private final CurrentUserService currentUserService;
|
|
|
|
public void updateUserRoutingPreferences(Long userId, RoutingPreferencesDTO newPreferencesDTO) {
|
|
Optional<RoutingPreferences> optionalPreferences = routingPreferencesRepository.findByUserId(userId);
|
|
|
|
RoutingPreferences preferences = optionalPreferences.orElseGet(RoutingPreferences::getDefaultPreferences);
|
|
|
|
preferences.setPreferPhysicalActivity(newPreferencesDTO.isPreferPhysicalActivity());
|
|
preferences.setConsiderWeatherConditions(newPreferencesDTO.isConsiderWeatherConditions());
|
|
preferences.setModalitiesToConsider(newPreferencesDTO.getModalitiesToConsider());
|
|
routingPreferencesRepository.save(preferences);
|
|
}
|
|
|
|
/**
|
|
* Get routing preferences for a specific user.
|
|
*/
|
|
public RoutingPreferencesDTO getUserRoutingPreferences(Long userId) {
|
|
Optional<RoutingPreferences> optionalPreferences = routingPreferencesRepository.findByUserId(userId);
|
|
|
|
RoutingPreferencesDTO preferencesDTO;
|
|
if (optionalPreferences.isEmpty()) {
|
|
// TODO after testing phase this should throw an exception or create new preferences for the user
|
|
preferencesDTO = routingPreferencesMapper.toRoutingPreferencesDTO(RoutingPreferences.getDefaultPreferences());
|
|
} else {
|
|
preferencesDTO = routingPreferencesMapper.toRoutingPreferencesDTO(optionalPreferences.get());
|
|
}
|
|
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());
|
|
}
|
|
}
|
|
}
|