LocationBasedServices/sumoc-backend/pipeline/head This commit looks good
- notification logic still missing - Weather api missing
78 lines
2.8 KiB
Java
78 lines
2.8 KiB
Java
package SuMoC.backend.trip;
|
|
|
|
|
|
import SuMoC.backend.suggestion.services.AdHocSuggestionService;
|
|
import SuMoC.backend.trip.dtos.NewTripDTO;
|
|
import SuMoC.backend.trip.dtos.TripDTO;
|
|
import SuMoC.backend.trip.models.Trip;
|
|
import SuMoC.backend.user.models.User;
|
|
import SuMoC.backend.user.repository.UserRepository;
|
|
import io.swagger.v3.oas.annotations.Operation;
|
|
import jakarta.validation.Valid;
|
|
import lombok.RequiredArgsConstructor;
|
|
import org.springframework.http.HttpStatus;
|
|
import org.springframework.http.ResponseEntity;
|
|
import org.springframework.security.core.Authentication;
|
|
import org.springframework.security.core.context.SecurityContextHolder;
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
import java.util.List;
|
|
|
|
@RestController
|
|
@RequestMapping("/trip")
|
|
@RequiredArgsConstructor
|
|
public class TripController {
|
|
|
|
private final TripService tripService;
|
|
|
|
private final UserRepository userRepository;
|
|
|
|
private final AdHocSuggestionService adHocSuggestionService;
|
|
|
|
// TODO extend security context with user id to avoid unnecessary db query
|
|
private User getAuthenticatedUser() {
|
|
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
|
return userRepository.findByEmail(authentication.getName()).orElseThrow(() -> new RuntimeException("User not found"));
|
|
}
|
|
|
|
@Operation(summary = "User uploads a new trip from the App.")
|
|
@PostMapping("/uploadTrip")
|
|
public ResponseEntity<Void> uploadTrip(@Valid @RequestBody NewTripDTO newTripDTO) {
|
|
User user = getAuthenticatedUser();
|
|
Trip savedTrip = tripService.addTrip(user, newTripDTO);
|
|
|
|
// let the ad hoc service suggest an alternative more ecofriendly route if appropriate
|
|
adHocSuggestionService.checkNewTripForNotifiedAlternativeSuggestion(savedTrip);
|
|
|
|
return ResponseEntity.status(HttpStatus.CREATED).build();
|
|
}
|
|
|
|
@Operation(summary = "Retrieve all trips of the authenticated user.")
|
|
@GetMapping("/getAllUserTrips")
|
|
public ResponseEntity<List<TripDTO>> getAllUserTrips() {
|
|
User user = getAuthenticatedUser();
|
|
|
|
List<TripDTO> userTrips = tripService.getAllTripsOfUser(user);
|
|
return ResponseEntity.ok(userTrips);
|
|
}
|
|
|
|
@Operation(summary = "Validate a trip for the authenticated user.")
|
|
@PutMapping("/validateTrip/{tripId}")
|
|
public ResponseEntity<Void> validateTrip(@PathVariable Long tripId) {
|
|
User user = getAuthenticatedUser();
|
|
|
|
tripService.validateTrip(user, tripId);
|
|
return ResponseEntity.ok().build();
|
|
}
|
|
|
|
@Operation(summary = "Invalidate a trip for the authenticated user.")
|
|
@PutMapping("/invalidateTrip/{tripId}")
|
|
public ResponseEntity<Void> invalidateTrip(@PathVariable Long tripId) {
|
|
User user = getAuthenticatedUser();
|
|
|
|
tripService.invalidateTrip(user, tripId);
|
|
return ResponseEntity.ok().build();
|
|
}
|
|
|
|
}
|