Created
October 9, 2024 09:07
-
-
Save ramkumarrammohan/704010dd00a437cd1d4e475ced63ca06 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.rest_ev.rest_ev.service; | |
| import java.util.ArrayList; | |
| import java.util.List; | |
| import java.util.Optional; | |
| import org.springframework.stereotype.Service; | |
| import com.rest_ev.rest_ev.entity.EVBunk; | |
| import com.rest_ev.rest_ev.repository.EVBunkRepository; | |
| import lombok.AllArgsConstructor; | |
| @Service | |
| @AllArgsConstructor | |
| public class EVBunkServiceImpl implements EVBunkService { | |
| // Radius of the Earth in kilometers | |
| private static final double EARTH_RADIUS = 6371.01; | |
| private EVBunkRepository evBunkRepository; | |
| @Override | |
| public EVBunk createEVBunk(EVBunk details) { | |
| return evBunkRepository.save(details); | |
| } | |
| @Override | |
| public EVBunk getEVBunkById(Long id) { | |
| Optional<EVBunk> bunkdetails = evBunkRepository.findById(id); | |
| return bunkdetails.get(); | |
| } | |
| @Override | |
| public List<EVBunk> getAllEVBunk() { | |
| return evBunkRepository.findAll(); | |
| } | |
| @Override | |
| public List<EVBunk> getNearbyEVBunk(EVBunk location) { | |
| // all items from db | |
| List<EVBunk> allBunks = evBunkRepository.findAll(); | |
| List<EVBunk> nearByBunks = new ArrayList<>(); | |
| // location based filter here | |
| for (EVBunk evBunk : allBunks) { | |
| double distance = calculateDistance(evBunk.getLatitude(), evBunk.getLongitude(), location.getLatitude(), location.getLongitude()); | |
| if(distance < 3) // check for within 5km radius | |
| { | |
| nearByBunks.add(evBunk); | |
| } | |
| } | |
| return nearByBunks; | |
| } | |
| @Override | |
| public EVBunk updateEVBunk(EVBunk details) { | |
| // TODO Auto-generated method stub | |
| throw new UnsupportedOperationException("Unimplemented method 'updateEVBunk'"); | |
| } | |
| @Override | |
| public void deleteEVBunk(Long id) { | |
| // TODO Auto-generated method stub | |
| throw new UnsupportedOperationException("Unimplemented method 'deleteEVBunk'"); | |
| } | |
| private static double calculateDistance(double lat1, double lon1, double lat2, double lon2) { | |
| // Convert degrees to radians | |
| double lat1Rad = Math.toRadians(lat1); | |
| double lon1Rad = Math.toRadians(lon1); | |
| double lat2Rad = Math.toRadians(lat2); | |
| double lon2Rad = Math.toRadians(lon2); | |
| // Haversine formula | |
| double dlat = lat2Rad - lat1Rad; | |
| double dlon = lon2Rad - lon1Rad; | |
| double a = Math.pow(Math.sin(dlat / 2), 2) | |
| + Math.cos(lat1Rad) * Math.cos(lat2Rad) * Math.pow(Math.sin(dlon / 2), 2); | |
| double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); | |
| // Distance in kilometers | |
| return EARTH_RADIUS * c; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment