add endpoint to delete trip with given id

This commit is contained in:
2026-02-22 17:23:38 +01:00
parent 906050ad89
commit 776b8857b6
2 changed files with 26 additions and 5 deletions
@@ -7,7 +7,9 @@ import SuMoC.backend.trip.models.Trip;
import SuMoC.backend.user.models.User;
import jakarta.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.web.server.ResponseStatusException;
import java.util.List;
@@ -26,18 +28,20 @@ public class TripService {
}
public void invalidateTrip(User user, Long tripId) {
Trip trip = tripRepository.findById(tripId).orElseThrow();
Trip trip = tripRepository.findById(tripId)
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Trip not found"));
if (!trip.getUser().equals(user)) {
throw new RuntimeException("Trip does not belong to user");
throw new ResponseStatusException(HttpStatus.FORBIDDEN, "Trip does not belong to user");
}
trip.setInvalidated(true);
tripRepository.save(trip);
}
public void validateTrip(User user, Long tripId) {
Trip trip = tripRepository.findById(tripId).orElseThrow();
Trip trip = tripRepository.findById(tripId)
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Trip not found"));
if (!trip.getUser().equals(user)) {
throw new RuntimeException("Trip does not belong to user");
throw new ResponseStatusException(HttpStatus.FORBIDDEN, "Trip does not belong to user");
}
trip.setInvalidated(false);
tripRepository.save(trip);
@@ -55,4 +59,13 @@ public class TripService {
List<Trip> trips = tripRepository.findAllByUser(user).orElseThrow();
return tripMapper.toTripDTOs(trips);
}
public void deleteTrip(User user, Long tripId) {
Trip trip = tripRepository.findById(tripId)
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Trip not found"));
if (!trip.getUser().equals(user)) {
throw new ResponseStatusException(HttpStatus.FORBIDDEN, "Trip does not belong to user");
}
tripRepository.delete(trip);
}
}