add trip validation and invalidation functionality in TripService and TripController
LocationBasedServices/sumoc-backend/pipeline/head This commit looks good
LocationBasedServices/sumoc-backend/pipeline/head This commit looks good
This commit is contained in:
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ package SuMoC.backend.trip;
|
||||
import SuMoC.backend.trip.dtos.NewTripDTO;
|
||||
import SuMoC.backend.trip.dtos.TripDTO;
|
||||
import SuMoC.backend.trip.models.Trip;
|
||||
import SuMoC.backend.user.repository.UserRepository;
|
||||
import SuMoC.backend.user.models.User;
|
||||
import jakarta.transaction.Transactional;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
@@ -16,28 +16,41 @@ import java.util.List;
|
||||
public class TripService {
|
||||
private final TripRepository tripRepository;
|
||||
private final TripMapper tripMapper;
|
||||
private final UserRepository userRepository;
|
||||
|
||||
@Autowired
|
||||
public TripService(TripRepository tripRepository,
|
||||
TripMapper tripMapper,
|
||||
UserRepository userRepository
|
||||
TripMapper tripMapper
|
||||
) {
|
||||
this.tripRepository = tripRepository;
|
||||
this.tripMapper = tripMapper;
|
||||
this.userRepository = userRepository;
|
||||
}
|
||||
|
||||
public void addTrip(String email, NewTripDTO newTripDTO) {
|
||||
var user = userRepository.findByEmail(email).orElseThrow();
|
||||
public void invalidateTrip(User user, Long tripId) {
|
||||
Trip trip = tripRepository.findById(tripId).orElseThrow();
|
||||
if (!trip.getUser().equals(user)) {
|
||||
throw new RuntimeException("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();
|
||||
if (!trip.getUser().equals(user)) {
|
||||
throw new RuntimeException("Trip does not belong to user");
|
||||
}
|
||||
trip.setInvalidated(false);
|
||||
tripRepository.save(trip);
|
||||
}
|
||||
|
||||
public void addTrip(User user, NewTripDTO newTripDTO) {
|
||||
Trip trip = tripMapper.toTrip(newTripDTO);
|
||||
trip.setUser(user);
|
||||
trip.getTripSegments().forEach(tripSegment -> tripSegment.setTrip(trip));
|
||||
tripRepository.save(trip);
|
||||
}
|
||||
|
||||
public List<TripDTO> getAllTripsOfUser(String email) {
|
||||
var user = userRepository.findByEmail(email).orElseThrow();
|
||||
public List<TripDTO> getAllTripsOfUser(User user) {
|
||||
List<Trip> trips = tripRepository.findAllByUser(user).orElseThrow();
|
||||
return tripMapper.toTripDTOs(trips);
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@ public class TripDTO {
|
||||
private LocalDateTime endTime;
|
||||
private float mobilityScore;
|
||||
private List<TripSegmentDTO> tripSegments;
|
||||
private boolean invalidated;
|
||||
|
||||
public TripDTO() {
|
||||
}
|
||||
@@ -92,4 +93,12 @@ public class TripDTO {
|
||||
public void setTripSegments(List<TripSegmentDTO> tripSegments) {
|
||||
this.tripSegments = tripSegments;
|
||||
}
|
||||
|
||||
public boolean isInvalidated() {
|
||||
return invalidated;
|
||||
}
|
||||
|
||||
public void setInvalidated(boolean invalidated) {
|
||||
this.invalidated = invalidated;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,6 +40,9 @@ public class Trip {
|
||||
@Column(nullable = true)
|
||||
private float cost;
|
||||
|
||||
@Column(nullable = false)
|
||||
private boolean invalidated = false;
|
||||
|
||||
@OneToMany(mappedBy = "trip", cascade = CascadeType.ALL, orphanRemoval = true)
|
||||
private Set<TripSegment> tripSegments = new HashSet<>();
|
||||
|
||||
@@ -130,4 +133,12 @@ public class Trip {
|
||||
public void setTripSegments(Set<TripSegment> segments) {
|
||||
this.tripSegments = segments;
|
||||
}
|
||||
|
||||
public boolean isInvalidated() {
|
||||
return invalidated;
|
||||
}
|
||||
|
||||
public void setInvalidated(boolean invalidated) {
|
||||
this.invalidated = invalidated;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user