add basic trip upload and retrieval functionality

This commit is contained in:
2025-12-12 12:56:47 +01:00
parent 32af17cd4d
commit d2b4d3b750
10 changed files with 70 additions and 30 deletions
@@ -29,16 +29,16 @@ public class TripSegment {
* Distance of the segment in meters.
*/
@Column(nullable = false)
private Long distance;
private Long distanceMeters;
public TripSegment() {}
public TripSegment(Long id, Trip trip, Modality modality, LocalDateTime startTime, Long distance) {
public TripSegment(Long id, Trip trip, Modality modality, LocalDateTime startTime, Long distanceMeters) {
this.id = id;
this.trip = trip;
this.modality = modality;
this.startTime = startTime;
this.distance = distance;
this.distanceMeters = distanceMeters;
}
public Long getId() {
@@ -73,11 +73,11 @@ public class TripSegment {
this.startTime = startTime;
}
public Long getDistance() {
return distance;
public Long getDistanceMeters() {
return distanceMeters;
}
public void setDistance(Long distance) {
this.distance = distance;
public void setDistanceMeters(Long distance) {
this.distanceMeters = distance;
}
}
@@ -5,8 +5,10 @@ import SuMoC.backend.trip.dtos.NewTripDTO;
import SuMoC.backend.trip.dtos.TripDTO;
import SuMoC.backend.trip.models.Trip;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.validation.Valid;
import org.apache.commons.lang3.NotImplementedException;
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;
@@ -26,18 +28,19 @@ public class TripController {
@Operation(summary = "User uploads a new trip from the App.")
@PostMapping("/uploadTrip")
public ResponseEntity<Void> uploadTrip(@RequestBody NewTripDTO newTripDTO) {
// TODO implement upload trip
throw new NotImplementedException();
//return ResponseEntity.ok().build();
public ResponseEntity<Void> uploadTrip(@Valid @RequestBody NewTripDTO newTripDTO) {
String userEmail = SecurityContextHolder.getContext().getAuthentication().getName();
tripService.addTrip(userEmail, newTripDTO);
return ResponseEntity.status(HttpStatus.CREATED).build();
}
@Operation(summary = "Retrieve all trips of the authenticated user.")
@GetMapping("/getAllUserTrips")
public ResponseEntity<List<TripDTO>> getAllUserTrips(Authentication authentication) {
String username = SecurityContextHolder.getContext().getAuthentication().getName();
public ResponseEntity<List<TripDTO>> getAllUserTrips() {
String userEmail = SecurityContextHolder.getContext().getAuthentication().getName();
List<TripDTO> userTrips = tripService.getAllTripsOfUser(username);
List<TripDTO> userTrips = tripService.getAllTripsOfUser(userEmail);
return ResponseEntity.ok(userTrips);
}
}
@@ -1,5 +1,6 @@
package SuMoC.backend.trip;
import SuMoC.backend.trip.dtos.NewTripDTO;
import SuMoC.backend.trip.dtos.TripDTO;
import SuMoC.backend.trip.models.Trip;
import org.mapstruct.Mapper;
@@ -9,8 +10,12 @@ import java.util.List;
@Mapper(componentModel = "spring", uses = TripSegmentMapper.class)
public interface TripMapper {
@Mapping(source = "id", target = "tripId")
@Mapping(source = "user.id", target = "userId")
TripDTO toTripDTO(Trip trip);
List<TripDTO> toTripDTOs(List<Trip> trips);
@Mapping(source = "tripSegments", target = "tripSegments")
Trip toTrip(NewTripDTO newTripDTO);
}
@@ -1,6 +1,7 @@
package SuMoC.backend.trip;
import SuMoC.backend.models.TripSegment;
import SuMoC.backend.trip.dtos.NewTripSegmentDTO;
import SuMoC.backend.trip.dtos.TripSegmentDTO;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
@@ -9,8 +10,11 @@ import java.util.List;
@Mapper(componentModel = "spring")
public interface TripSegmentMapper {
@Mapping(source = "id", target = "tripSegmentId")
@Mapping(source = "trip.id", target = "tripId")
TripSegmentDTO toTripSegmentDTO(TripSegment tripSegment);
List<TripSegmentDTO> toTripSegmentDTOs(List<TripSegment> tripSegments);
TripSegment toTripSegment(NewTripSegmentDTO newTripSegmentDTO);
}
@@ -2,6 +2,7 @@ package SuMoC.backend.trip;
import SuMoC.backend.repositories.UserRepository;
import SuMoC.backend.trip.dtos.NewTripDTO;
import SuMoC.backend.trip.dtos.TripDTO;
import SuMoC.backend.trip.models.Trip;
import jakarta.transaction.Transactional;
@@ -27,6 +28,14 @@ public class TripService {
this.userRepository = userRepository;
}
public void addTrip(String email, NewTripDTO newTripDTO) {
var user = userRepository.findByEmail(email).orElseThrow();
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();
List<Trip> trips = tripRepository.findAllByUser(user).orElseThrow();
@@ -1,6 +1,9 @@
package SuMoC.backend.trip.dtos;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import java.time.LocalDateTime;
import java.util.Set;
@@ -16,6 +19,8 @@ public class NewTripDTO {
private LocalDateTime startTime;
private LocalDateTime endTime;
private float mobilityScore;
@NotNull
@Size(min = 1, message = "Trip must contain at least one segment.")
private Set<NewTripSegmentDTO> tripSegments;
public NewTripDTO() {}
@@ -11,7 +11,7 @@ import java.util.Set;
* DTO representing a trip already persisted in the database.
*/
public class TripDTO {
private int tripId;
private long tripId;
private int userId;
private String startPlace;
private String endPlace;
@@ -32,11 +32,11 @@ public class TripDTO {
this.tripSegments = tripSegmentDTOS;
}
public int getTripId() {
public long getTripId() {
return tripId;
}
public void setTripId(int tripId) {
public void setTripId(long tripId) {
this.tripId = tripId;
}
@@ -8,25 +8,35 @@ import java.time.LocalDateTime;
* DTO representing a trip segment already persisted in the database.
*/
public class TripSegmentDTO {
private int tripId;
private long tripSegmentId;
private long tripId;
private Modality modality;
private LocalDateTime startTime;
private float distanceMeters;
public TripSegmentDTO() {}
public TripSegmentDTO(int tripId, Modality modality, LocalDateTime startTime, float distanceMeters) {
public TripSegmentDTO(long tripSegmentId, long tripId, Modality modality, LocalDateTime startTime, float distanceMeters) {
this.tripSegmentId = tripSegmentId;
this.tripId = tripId;
this.modality = modality;
this.startTime = startTime;
this.distanceMeters = distanceMeters;
}
public int getTripId() {
public long getTripSegmentId() {
return tripSegmentId;
}
public void setTripSegmentId(long tripSegmentId) {
this.tripSegmentId = tripSegmentId;
}
public long getTripId() {
return tripId;
}
public void setTripId(int tripId) {
public void setTripId(long tripId) {
this.tripId = tripId;
}
@@ -42,12 +42,12 @@ public class Trip {
private float cost;
@OneToMany(mappedBy = "trip", cascade = CascadeType.ALL, orphanRemoval = true)
private Set<TripSegment> segments = new HashSet<>();
private Set<TripSegment> tripSegments = new HashSet<>();
public Trip() {}
public Trip(Long id, User user, String startPlace, String endPlace, LocalDateTime startTime, LocalDateTime endTime, float mobilityScore, float cost, Set<TripSegment> segments) {
public Trip(Long id, User user, String startPlace, String endPlace, LocalDateTime startTime, LocalDateTime endTime, float mobilityScore, float cost, Set<TripSegment> tripSegments) {
this.id = id;
this.user = user;
this.startPlace = startPlace;
@@ -56,7 +56,7 @@ public class Trip {
this.endTime = endTime;
this.mobilityScore = mobilityScore;
this.cost = cost;
this.segments = segments;
this.tripSegments = tripSegments;
}
public Long getId() {
@@ -111,8 +111,8 @@ public class Trip {
return mobilityScore;
}
public void setMobilityScore(float score) {
this.mobilityScore = score;
public void setMobilityScore(float mobilityScore) {
this.mobilityScore = mobilityScore;
}
public float getCost() {
@@ -123,11 +123,11 @@ public class Trip {
this.cost = cost;
}
public Set<TripSegment> getSegments() {
return segments;
public Set<TripSegment> getTripSegments() {
return tripSegments;
}
public void setSegments(Set<TripSegment> segments) {
this.segments = segments;
public void setTripSegments(Set<TripSegment> segments) {
this.tripSegments = segments;
}
}