trips: add controller, service, mappers, dtos etc.
This commit is contained in:
@@ -1,38 +0,0 @@
|
||||
package SuMoC.backend.controller;
|
||||
|
||||
|
||||
import SuMoC.backend.models.Trip;
|
||||
import SuMoC.backend.security.services.TripService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import org.apache.commons.lang3.NotImplementedException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/trip")
|
||||
public class TripController {
|
||||
private TripService tripService;
|
||||
|
||||
@Autowired
|
||||
public TripController(TripService tripService) {
|
||||
this.tripService = tripService;
|
||||
}
|
||||
|
||||
@Operation(summary = "User uploads a new trip from the App.")
|
||||
@PostMapping
|
||||
public ResponseEntity<Void> uploadTrip() {
|
||||
// TODO implement upload trip
|
||||
throw new NotImplementedException();
|
||||
return ResponseEntity.ok().build();
|
||||
}
|
||||
|
||||
// TODO might be userfull when user switched devices? discuss in team - low priority
|
||||
public ResponseEntity<List<Trip>> getAllUserTrips() {
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,6 @@ package SuMoC.backend.controller;
|
||||
|
||||
import SuMoC.backend.models.dto.MinimalUserDTO;
|
||||
import SuMoC.backend.models.enums.VisibilityPrivacy;
|
||||
import SuMoC.backend.security.services.Userservice;
|
||||
import SuMoC.backend.service.UserService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.media.Content;
|
||||
|
||||
@@ -2,6 +2,7 @@ package SuMoC.backend.models;
|
||||
|
||||
|
||||
import SuMoC.backend.models.enums.Modality;
|
||||
import SuMoC.backend.trip.models.Trip;
|
||||
import jakarta.persistence.*;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@@ -2,6 +2,7 @@ package SuMoC.backend.models;
|
||||
|
||||
import SuMoC.backend.models.enums.RoleName;
|
||||
import SuMoC.backend.models.enums.VisibilityPrivacy;
|
||||
import SuMoC.backend.trip.models.Trip;
|
||||
import jakarta.persistence.*;
|
||||
|
||||
import java.util.HashSet;
|
||||
|
||||
@@ -1,17 +0,0 @@
|
||||
package SuMoC.backend.models.dto;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* DTO representing a trip submitted from the App which is not yet persisted in the database.
|
||||
* TODO assumption is that cost of trip is calculated server-side
|
||||
*/
|
||||
public class NewTripDTO {
|
||||
private String startPlace; // High level description of the trip's starting location
|
||||
private String endPlace; // High level description of the trip's ending location
|
||||
private LocalDateTime startTime;
|
||||
private LocalDateTime endTime;
|
||||
private float mobilityScore;
|
||||
private Set<NewTripSegmentDTO> tripSegments;
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
package SuMoC.backend.models.dto;
|
||||
|
||||
import SuMoC.backend.models.enums.Modality;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* DTO representing a trip segment submitted from the App which is not yet persisted in the database.
|
||||
*/
|
||||
public class NewTripSegmentDTO {
|
||||
private Modality modality;
|
||||
private LocalDateTime startTime;
|
||||
float distanceMeters;
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
package SuMoC.backend.security.services;
|
||||
|
||||
|
||||
import SuMoC.backend.repositories.TripRepository;
|
||||
import jakarta.transaction.Transactional;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
|
||||
@Service
|
||||
@Transactional
|
||||
public class TripService {
|
||||
private TripRepository tripRepository;
|
||||
|
||||
@Autowired
|
||||
public TripService(TripRepository tripRepository) {this.tripRepository = tripRepository;}
|
||||
}
|
||||
+8
-2
@@ -15,7 +15,7 @@ import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
public class Userservice implements UserDetailsService {
|
||||
public class UserSecurityService implements UserDetailsService {
|
||||
|
||||
private PasswordEncoder encoder;
|
||||
|
||||
@@ -23,11 +23,17 @@ public class Userservice implements UserDetailsService {
|
||||
|
||||
|
||||
@Autowired
|
||||
public Userservice(PasswordEncoder encoder, UserRepository userRepository){
|
||||
public UserSecurityService(PasswordEncoder encoder, UserRepository userRepository){
|
||||
this.encoder = encoder;
|
||||
this.userRepository = userRepository;
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads user based on email address.
|
||||
* @param email
|
||||
* @return
|
||||
* @throws UsernameNotFoundException
|
||||
*/
|
||||
@Override
|
||||
public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
|
||||
// Jetzt suchen wir nach der E-Mail-Adresse
|
||||
@@ -0,0 +1,43 @@
|
||||
package SuMoC.backend.trip;
|
||||
|
||||
|
||||
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 org.apache.commons.lang3.NotImplementedException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
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")
|
||||
public class TripController {
|
||||
private TripService tripService;
|
||||
|
||||
@Autowired
|
||||
public TripController(TripService tripService) {
|
||||
this.tripService = tripService;
|
||||
}
|
||||
|
||||
@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();
|
||||
}
|
||||
|
||||
@Operation(summary = "Retrieve all trips of the authenticated user.")
|
||||
@GetMapping("/getAllUserTrips")
|
||||
public ResponseEntity<List<TripDTO>> getAllUserTrips(Authentication authentication) {
|
||||
String username = SecurityContextHolder.getContext().getAuthentication().getName();
|
||||
|
||||
List<TripDTO> userTrips = tripService.getAllTripsOfUser(username);
|
||||
return ResponseEntity.ok(userTrips);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package SuMoC.backend.trip;
|
||||
|
||||
import SuMoC.backend.trip.dtos.TripDTO;
|
||||
import SuMoC.backend.trip.models.Trip;
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.Mapping;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Mapper(componentModel = "spring", uses = TripSegmentMapper.class)
|
||||
public interface TripMapper {
|
||||
@Mapping(source = "user.id", target = "userId")
|
||||
TripDTO toTripDTO(Trip trip);
|
||||
|
||||
List<TripDTO> toTripDTOs(List<Trip> trips);
|
||||
}
|
||||
+5
-2
@@ -1,10 +1,13 @@
|
||||
package SuMoC.backend.repositories;
|
||||
package SuMoC.backend.trip;
|
||||
|
||||
import SuMoC.backend.models.Trip;
|
||||
import SuMoC.backend.models.User;
|
||||
import SuMoC.backend.trip.models.Trip;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public interface TripRepository extends JpaRepository<Trip,Long> {
|
||||
Optional<Trip> findTripById(Long id);
|
||||
Optional<List<Trip>> findAllByUser(User user);
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package SuMoC.backend.trip;
|
||||
|
||||
import SuMoC.backend.models.TripSegment;
|
||||
import SuMoC.backend.trip.dtos.TripSegmentDTO;
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.Mapping;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Mapper(componentModel = "spring")
|
||||
public interface TripSegmentMapper {
|
||||
@Mapping(source = "trip.id", target = "tripId")
|
||||
TripSegmentDTO toTripSegmentDTO(TripSegment tripSegment);
|
||||
|
||||
List<TripSegmentDTO> toTripSegmentDTOs(List<TripSegment> tripSegments);
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package SuMoC.backend.trip;
|
||||
|
||||
|
||||
import SuMoC.backend.repositories.UserRepository;
|
||||
import SuMoC.backend.trip.dtos.TripDTO;
|
||||
import SuMoC.backend.trip.models.Trip;
|
||||
import jakarta.transaction.Transactional;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
@Transactional
|
||||
public class TripService {
|
||||
private final TripRepository tripRepository;
|
||||
private final TripMapper tripMapper;
|
||||
private final UserRepository userRepository;
|
||||
|
||||
@Autowired
|
||||
public TripService(TripRepository tripRepository,
|
||||
TripMapper tripMapper,
|
||||
UserRepository userRepository
|
||||
) {
|
||||
this.tripRepository = tripRepository;
|
||||
this.tripMapper = tripMapper;
|
||||
this.userRepository = userRepository;
|
||||
}
|
||||
|
||||
public List<TripDTO> getAllTripsOfUser(String email) {
|
||||
var user = userRepository.findByEmail(email).orElseThrow();
|
||||
List<Trip> trips = tripRepository.findAllByUser(user).orElseThrow();
|
||||
return tripMapper.toTripDTOs(trips);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
package SuMoC.backend.trip.dtos;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* DTO representing a trip submitted from the App which is not yet persisted in the database.
|
||||
* TODO assumption is that cost of trip is calculated server-side
|
||||
*/
|
||||
@Schema(description = "DTO for creating a new Trip")
|
||||
public class NewTripDTO {
|
||||
private String startPlace; // High level description of the trip's starting location
|
||||
private String endPlace; // High level description of the trip's ending location
|
||||
private LocalDateTime startTime;
|
||||
private LocalDateTime endTime;
|
||||
private float mobilityScore;
|
||||
private Set<NewTripSegmentDTO> tripSegments;
|
||||
|
||||
public NewTripDTO() {}
|
||||
|
||||
public NewTripDTO (String startPlace, String endPlace, LocalDateTime startTime, LocalDateTime endTime, float mobilityScore, Set<NewTripSegmentDTO> tripSegments) {
|
||||
this.startPlace = startPlace;
|
||||
this.endPlace = endPlace;
|
||||
this.startTime = startTime;
|
||||
this.endTime = endTime;
|
||||
this.mobilityScore = mobilityScore;
|
||||
this.tripSegments = tripSegments;
|
||||
}
|
||||
|
||||
public String getStartPlace() {
|
||||
return startPlace;
|
||||
}
|
||||
|
||||
public void setStartPlace(String startPlace) {
|
||||
this.startPlace = startPlace;
|
||||
}
|
||||
|
||||
public String getEndPlace() {
|
||||
return endPlace;
|
||||
}
|
||||
|
||||
public void setEndPlace(String endPlace) {
|
||||
this.endPlace = endPlace;
|
||||
}
|
||||
|
||||
public LocalDateTime getStartTime() {
|
||||
return startTime;
|
||||
}
|
||||
|
||||
public void setStartTime(LocalDateTime startTime) {
|
||||
this.startTime = startTime;
|
||||
}
|
||||
|
||||
public LocalDateTime getEndTime() {
|
||||
return endTime;
|
||||
}
|
||||
|
||||
public void setEndTime(LocalDateTime endTime) {
|
||||
this.endTime = endTime;
|
||||
}
|
||||
|
||||
public float getMobilityScore() {
|
||||
return mobilityScore;
|
||||
}
|
||||
|
||||
public void setMobilityScore(float mobilityScore) {
|
||||
this.mobilityScore = mobilityScore;
|
||||
}
|
||||
|
||||
public Set<NewTripSegmentDTO> getTripSegments() {
|
||||
return tripSegments;
|
||||
}
|
||||
|
||||
public void setTripSegments(Set<NewTripSegmentDTO> tripSegments) {
|
||||
this.tripSegments = tripSegments;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package SuMoC.backend.trip.dtos;
|
||||
|
||||
import SuMoC.backend.models.enums.Modality;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* DTO representing a trip segment submitted from the App which is not yet persisted in the database.
|
||||
*/
|
||||
public class NewTripSegmentDTO {
|
||||
private Modality modality;
|
||||
private LocalDateTime startTime;
|
||||
private float distanceMeters;
|
||||
|
||||
public NewTripSegmentDTO() { }
|
||||
|
||||
public NewTripSegmentDTO(Modality modality, LocalDateTime startTime, float distanceMeters) {
|
||||
this.modality = modality;
|
||||
this.startTime = startTime;
|
||||
this.distanceMeters = distanceMeters;
|
||||
}
|
||||
|
||||
public Modality getModality() {
|
||||
return modality;
|
||||
}
|
||||
|
||||
public LocalDateTime getStartTime() {
|
||||
return startTime;
|
||||
}
|
||||
|
||||
public float getDistanceMeters() {
|
||||
return distanceMeters;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
package SuMoC.backend.trip.dtos;
|
||||
|
||||
import SuMoC.backend.models.TripSegment;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* DTO representing a trip already persisted in the database.
|
||||
*/
|
||||
public class TripDTO {
|
||||
private int tripId;
|
||||
private int userId;
|
||||
private String startPlace;
|
||||
private String endPlace;
|
||||
private LocalDateTime startTime;
|
||||
private LocalDateTime endTime;
|
||||
private float mobilityScore;
|
||||
private List<TripSegmentDTO> tripSegments;
|
||||
|
||||
public TripDTO() {}
|
||||
|
||||
public TripDTO(int userId, String startPlace, String endPlace, LocalDateTime startTime, LocalDateTime endTime, float mobilityScore, List<TripSegmentDTO> tripSegmentDTOS) {
|
||||
this.userId = userId;
|
||||
this.startPlace = startPlace;
|
||||
this.endPlace = endPlace;
|
||||
this.startTime = startTime;
|
||||
this.endTime = endTime;
|
||||
this.mobilityScore = mobilityScore;
|
||||
this.tripSegments = tripSegmentDTOS;
|
||||
}
|
||||
|
||||
public int getTripId() {
|
||||
return tripId;
|
||||
}
|
||||
|
||||
public void setTripId(int tripId) {
|
||||
this.tripId = tripId;
|
||||
}
|
||||
|
||||
public int getUserId() {
|
||||
return userId;
|
||||
}
|
||||
|
||||
public void setUserId(int userId) {
|
||||
this.userId = userId;
|
||||
}
|
||||
|
||||
public String getStartPlace() {
|
||||
return startPlace;
|
||||
}
|
||||
|
||||
public void setStartPlace(String startPlace) {
|
||||
this.startPlace = startPlace;
|
||||
}
|
||||
|
||||
public String getEndPlace() {
|
||||
return endPlace;
|
||||
}
|
||||
|
||||
public void setEndPlace(String endPlace) {
|
||||
this.endPlace = endPlace;
|
||||
}
|
||||
|
||||
public LocalDateTime getStartTime() {
|
||||
return startTime;
|
||||
}
|
||||
|
||||
public void setStartTime(LocalDateTime startTime) {
|
||||
this.startTime = startTime;
|
||||
}
|
||||
|
||||
public LocalDateTime getEndTime() {
|
||||
return endTime;
|
||||
}
|
||||
|
||||
public void setEndTime(LocalDateTime endTime) {
|
||||
this.endTime = endTime;
|
||||
}
|
||||
|
||||
public float getMobilityScore() {
|
||||
return mobilityScore;
|
||||
}
|
||||
|
||||
public void setMobilityScore(float mobilityScore) {
|
||||
this.mobilityScore = mobilityScore;
|
||||
}
|
||||
|
||||
public List<TripSegmentDTO> getTripSegments() {
|
||||
return tripSegments;
|
||||
}
|
||||
|
||||
public void setTripSegments(List<TripSegmentDTO> tripSegments) {
|
||||
this.tripSegments = tripSegments;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package SuMoC.backend.trip.dtos;
|
||||
|
||||
import SuMoC.backend.models.enums.Modality;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* DTO representing a trip segment already persisted in the database.
|
||||
*/
|
||||
public class TripSegmentDTO {
|
||||
private int tripId;
|
||||
private Modality modality;
|
||||
private LocalDateTime startTime;
|
||||
private float distanceMeters;
|
||||
|
||||
public TripSegmentDTO() {}
|
||||
|
||||
public TripSegmentDTO(int tripId, Modality modality, LocalDateTime startTime, float distanceMeters) {
|
||||
this.tripId = tripId;
|
||||
this.modality = modality;
|
||||
this.startTime = startTime;
|
||||
this.distanceMeters = distanceMeters;
|
||||
}
|
||||
|
||||
public int getTripId() {
|
||||
return tripId;
|
||||
}
|
||||
|
||||
public void setTripId(int tripId) {
|
||||
this.tripId = tripId;
|
||||
}
|
||||
|
||||
public Modality getModality() {
|
||||
return modality;
|
||||
}
|
||||
|
||||
public void setModality(Modality modality) {
|
||||
this.modality = modality;
|
||||
}
|
||||
|
||||
public LocalDateTime getStartTime() {
|
||||
return startTime;
|
||||
}
|
||||
|
||||
public void setStartTime(LocalDateTime startTime) {
|
||||
this.startTime = startTime;
|
||||
}
|
||||
|
||||
public float getDistanceMeters() {
|
||||
return distanceMeters;
|
||||
}
|
||||
|
||||
public void setDistanceMeters(float distanceMeters) {
|
||||
this.distanceMeters = distanceMeters;
|
||||
}
|
||||
}
|
||||
+10
-8
@@ -1,6 +1,8 @@
|
||||
package SuMoC.backend.models;
|
||||
package SuMoC.backend.trip.models;
|
||||
|
||||
|
||||
import SuMoC.backend.models.TripSegment;
|
||||
import SuMoC.backend.models.User;
|
||||
import jakarta.persistence.*;
|
||||
|
||||
import java.util.HashSet;
|
||||
@@ -34,7 +36,7 @@ public class Trip {
|
||||
* Mobilityscore for the trip. TODO determine if this needs an own data type
|
||||
*/
|
||||
@Column(nullable = false)
|
||||
private float score;
|
||||
private float mobilityScore;
|
||||
|
||||
@Column(nullable = true)
|
||||
private float cost;
|
||||
@@ -45,14 +47,14 @@ public class Trip {
|
||||
|
||||
public Trip() {}
|
||||
|
||||
public Trip(Long id, User user, String startPlace, String endPlace, LocalDateTime startTime, LocalDateTime endTime, float score, 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> segments) {
|
||||
this.id = id;
|
||||
this.user = user;
|
||||
this.startPlace = startPlace;
|
||||
this.endPlace = endPlace;
|
||||
this.startTime = startTime;
|
||||
this.endTime = endTime;
|
||||
this.score = score;
|
||||
this.mobilityScore = mobilityScore;
|
||||
this.cost = cost;
|
||||
this.segments = segments;
|
||||
}
|
||||
@@ -105,12 +107,12 @@ public class Trip {
|
||||
this.endTime = endTime;
|
||||
}
|
||||
|
||||
public float getScore() {
|
||||
return score;
|
||||
public float getMobilityScore() {
|
||||
return mobilityScore;
|
||||
}
|
||||
|
||||
public void setScore(float score) {
|
||||
this.score = score;
|
||||
public void setMobilityScore(float score) {
|
||||
this.mobilityScore = score;
|
||||
}
|
||||
|
||||
public float getCost() {
|
||||
Reference in New Issue
Block a user