Implement removeStop() and find() (2.1.1.1)

This commit is contained in:
2021-04-27 15:03:12 +02:00
parent b19a2d2ca2
commit 852527755c
2 changed files with 50 additions and 8 deletions
@@ -14,6 +14,7 @@ import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
@Named("ITripService")
@Singleton
@@ -166,6 +167,31 @@ public class TripService implements ITripService {
} catch (InvalidTripException e) {
trip.setFare(null);
}
updateStops(trip, savedTrip);
return true;
}
@Transactional
@Override
public boolean removeStop(TripDTO trip, Long locationId) throws EntityNotFoundException, IllegalStateException {
ITrip savedTrip = getTrip(trip.getId());
if (savedTrip.getState() != TripState.CREATED)
throw new IllegalStateException("Trip with id " + trip.getId() + " is not in CREATED state");
getLocation(locationId);
if (trip.getStops().remove(locationId)) {
try {
trip.setFare(matchingService.calculateFare(trip));
} catch (InvalidTripException e) {
trip.setFare(null);
}
updateStops(trip, savedTrip);
return true;
}
return false;
}
private void updateStops(TripDTO trip, ITrip savedTrip) {
ILocationDAO locationDAO = daoFactory.createLocationDAO();
List<ILocation> locations = new ArrayList<>();
for (Long location : trip.getStops()) {
@@ -173,22 +199,35 @@ public class TripService implements ITripService {
}
savedTrip.setStops(locations);
entityManager.merge(savedTrip);
return true;
}
@Override
public boolean removeStop(TripDTO trip, Long locationId) throws EntityNotFoundException, IllegalStateException {
return false;
}
@Transactional
@Override
public void delete(Long tripId) throws EntityNotFoundException {
ITrip trip = getTrip(tripId);
entityManager.remove(trip);
}
@Override
public TripDTO find(Long tripId) {
return null;
TripDTO tripDTO = new TripDTO();
ITrip trip;
try {
trip = getTrip(tripId);
} catch (EntityNotFoundException e) {
return null;
}
tripDTO.setId(tripId);
tripDTO.setPickupId(trip.getPickup().getId());
tripDTO.setDestinationId(trip.getDestination().getId());
tripDTO.setRiderId(trip.getRider().getId());
tripDTO.setStops(trip.getStops().stream().map(ILocation::getId).collect(Collectors.toList()));
try {
tripDTO.setFare(matchingService.calculateFare(tripDTO));
} catch (InvalidTripException e) {
tripDTO.setFare(null);
}
return tripDTO;
}
private IRider getRider(Long riderId) throws EntityNotFoundException {