This project is a REST API that calculates the distance and duration between two pin codes using the Google Maps API. The API is designed to be REST-compliant, supports caching, and stores the distance and duration data in a database.
- ✅ Fetches route, distance, and duration between two pin codes.
- ✅ Caches responses to reduce unnecessary API calls to Google Maps.
- ✅ Saves distance, duration, and route information in a database.
- ✅ Uses TDD (Test-Driven Development) with JUnit.
- ✅ Optimized API calls for better efficiency.
The API integrates with geocoding api and routes api to calculate travel duration and distance between pin codes.
- First Call: API fetches data from Google Maps and stores it in the cache & database.
- Subsequent Calls: If the same request is made, the data is retrieved from the cache/database instead of hitting Google Maps API.
POST
##post
http://localhost:8080/api/calculate-distance
## json body
{
"originPincode": "141106",
"destinationPincode": "110060"
}
{
"from": "141106",
"to": "110060",
"distance": "250 km",
"duration": "4 hours 30 minutes",
"route": ["Route details..."]
}| Table Name | Columns |
|---|---|
| distance_cache | id, from_pincode, to_pincode, distance, duration, route, created_at |
| pincode_info | pincode, latitude, longitude, polygon_info |
| Call # | From Pincode | To Pincode | API Hits |
|---|---|---|---|
| 1st | 141106 | 110060 | ✅ Google Maps API |
| 2nd | 141106 | 560023 | ✅ Google Maps API |
| 3rd | 141106 | 110060 | ✅ Cached Response |
- Java 21
- Spring Boot (REST API)
- Google Maps API (Geocoding and Routes api)
- H2 Database (Testing) / MySQL (Production)
- JUnit (Testing)
- Postman (API Testing)
- Clone the repository:
git clone https://github.com/your-username/Backend-Assignment.git
cd Backend-Assignment- Configure Google Maps API Key:
- Go to
src/main/resources/application.properties - Add your API key:
- Go to
google.maps.api.key=YOUR_API_KEY- Run the application:
mvn spring-boot:run- Test the API using Postman
- Use the endpoint:
GET /distance?fromPincode=141106&toPincode=110060
- Use the endpoint:
Run unit tests using:
mvn test- 🔹 No UI - The API is meant for backend services and is testable via Postman.
- 🔹 Follows RESTful principles for clean and structured endpoints.
- 🔹 TDD approach - Uses JUnit to ensure robust testing.
👤 Shekhar
📧 Contact: shekharsuman0397@gmail.com