add trip validation and invalidation functionality in TripService and TripController
LocationBasedServices/sumoc-backend/pipeline/head This commit looks good

This commit is contained in:
2026-01-03 19:28:21 +01:00
parent 45cf83aa78
commit e93bdde124
4 changed files with 77 additions and 14 deletions
@@ -3,11 +3,14 @@ package SuMoC.backend.trip;
import SuMoC.backend.trip.dtos.NewTripDTO;
import SuMoC.backend.trip.dtos.TripDTO;
import SuMoC.backend.user.models.User;
import SuMoC.backend.user.repository.UserRepository;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
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.*;
@@ -17,27 +20,54 @@ import java.util.List;
@RequestMapping("/trip")
public class TripController {
private final TripService tripService;
private final UserRepository userRepository;
@Autowired
public TripController(TripService tripService) {
public TripController(TripService tripService, UserRepository userRepository) {
this.tripService = tripService;
this.userRepository = userRepository;
}
// 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) {
String userEmail = SecurityContextHolder.getContext().getAuthentication().getName();
User user = getAuthenticatedUser();
tripService.addTrip(userEmail, newTripDTO);
tripService.addTrip(user, newTripDTO);
return ResponseEntity.status(HttpStatus.CREATED).build();
}
@Operation(summary = "Retrieve all trips of the authenticated user.")
@GetMapping("/getAllUserTrips")
public ResponseEntity<List<TripDTO>> getAllUserTrips() {
String userEmail = SecurityContextHolder.getContext().getAuthentication().getName();
User user = getAuthenticatedUser();
List<TripDTO> userTrips = tripService.getAllTripsOfUser(userEmail);
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();
}
}