Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@

import edu.zut.bookrider.model.DistanceCache;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.math.BigDecimal;
import java.util.Optional;

public interface DistanceCacheRepository extends JpaRepository<DistanceCache, Integer> {
Optional<DistanceCache> findByStartLatitudeAndStartLongitudeAndEndLatitudeAndEndLongitude(
BigDecimal startLatitude,
BigDecimal startLongitude,
BigDecimal endLatitude,
BigDecimal endLongitude);
@Query("SELECT d FROM DistanceCache d WHERE " +
"(d.startLatitude = :lat1 AND d.startLongitude = :lon1 AND d.endLatitude = :lat2 AND d.endLongitude = :lon2) OR " +
"(d.startLatitude = :lat2 AND d.startLongitude = :lon2 AND d.endLatitude = :lat1 AND d.endLongitude = :lon1)")
Optional<DistanceCache> findByCoordinates(
@Param("lat1") BigDecimal lat1, @Param("lon1") BigDecimal lon1,
@Param("lat2") BigDecimal lat2, @Param("lon2") BigDecimal lon2);
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,7 @@ public BigDecimal getDistance(
BigDecimal endLat = BigDecimal.valueOf(endCoordinates.getLatitude());
BigDecimal endLon = BigDecimal.valueOf(endCoordinates.getLongitude());

Optional<DistanceCache> cachedDistance = distanceCacheRepository
.findByStartLatitudeAndStartLongitudeAndEndLatitudeAndEndLongitude(startLat, startLon, endLat, endLon);

if (cachedDistance.isEmpty()) {
cachedDistance = distanceCacheRepository
.findByStartLatitudeAndStartLongitudeAndEndLatitudeAndEndLongitude(endLat, endLon, startLat, startLon);
}
Optional<DistanceCache> cachedDistance = distanceCacheRepository.findByCoordinates(startLat, startLon, endLat, endLon);

if (cachedDistance.isPresent()) {
return cachedDistance.get().getDistance();
Expand Down