0% found this document useful (0 votes)
7 views3 pages

RestAPI Flow Search Parking Feature

The document outlines a process for fetching parking locations based on a city name input from a React frontend. It details the interaction between the frontend, a Spring Boot backend controller, and a service layer that retrieves data from a database using JPA. The flow includes building an API endpoint, handling requests, and returning filtered parking location data as a JSON response to the frontend for display.

Uploaded by

Joy Happy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views3 pages

RestAPI Flow Search Parking Feature

The document outlines a process for fetching parking locations based on a city name input from a React frontend. It details the interaction between the frontend, a Spring Boot backend controller, and a service layer that retrieves data from a database using JPA. The flow includes building an API endpoint, handling requests, and returning filtered parking location data as a JSON response to the frontend for display.

Uploaded by

Joy Happy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

React:

export async function fetchLocations(location) {


try {
const url = createUrl(`location/city/${location}`);
const token = sessionStorage.getItem("jwt");
const response = await axios.get(url);
return response.data;
} catch (ex) {
return { status: 'error', error: ex };
}
}

Parkinglocation Controller:

@RequestMapping("/location")
@RestController
public class ParkingLocationController{
@GetMapping("/city/{city}")
public ResponseEntity<?> getLocationByCity(@PathVariable String city)
{
return ResponseEntity.ok(parkingLocationService.getLocationByCity(city));
}
}

Service:

@Service
@Transactional
public class ParkingLocationServiceImpl
@Override
public List<ParkingLocation> getLocationByCity(String city) {
List<ParkingLocation> locations =
parkingLocationDao.findByAddressContainingIgnoreCase(city);
return location;

DAO :
public interface ParkingLocation extends JpaRepository<ParkingLocation,Long>{
List<ParkingLocation> findByAddressContainingIgnoreCase(String city);
}

// Query will fire by JPA

//SELECT * FROM parking_location WHERE LOWER(address) LIKE LOWER('%city


%')

Explanation:
 In the React frontend, the user types a city name like "Pune".
 The fetchLocations(location) function is called with the city name.
 It uses createUrl() to build the API endpoint /location/city/Pune.
 Axios sends a GET request to this backend endpoint.
 The request reaches the Spring Boot controller
ParkingLocationController.
 The controller method getLocationByCity(@PathVariable String city)
is triggered.
 It forwards the city name to the service layer method
getLocationByCity(city).
 The service contains business logic to handle the request.
 It calls the DAO method findByAddressContainingIgnoreCase(city).
 This is a Spring Data JPA derived query method.
 JPA translates it into SQL: SELECT * FROM parking_location WHERE
LOWER(address) LIKE LOWER('%pune%').
 The database returns a list of matching ParkingLocation entities.
 The service returns this list to the controller.
 The controller wraps it in a ResponseEntity and sends it as a JSON
response.
 The frontend receives the data and displays the filtered parking
locations.

You might also like