Implementation of controllers

The following code is the TaxiController, which caters to the registering, searching, status updating, and so on, of Taxis; it is available in spring-boot-2-taxi-service:

@RequestMapping("/taxis")
@RestController
public class TaxiController {

private final TaxiService taxiService;

public TaxiController(TaxiService taxiService) {
this.taxiService = taxiService;
}

@GetMapping
public Flux<TaxiAvailableResponseDTO>
getAvailableTaxis(@RequestParam("type") TaxiType taxiType,
@RequestParam("latitude") Double latitude, @RequestParam("longitude")
Double longitude, @RequestParam(value = "radius", defaultValue = "1")
Double radius) {
Flux<GeoResult<RedisGeoCommands.GeoLocation<String>>> availableTaxisFlux
= taxiService.getAvailableTaxis(taxiType, latitude, longitude, radius);
return availableTaxisFlux.map(r -> new
TaxiAvailableResponseDTO(r.getContent().getName()));
}

@GetMapping("/{taxiId}/status")
public Mono<TaxiStatusDTO> getTaxiStatus(@PathVariable("taxiId") String
taxiId) {
return taxiService.getTaxiStatus(taxiId).map(s -> new
TaxiStatusDTO(taxiId, s));
}

@PutMapping("/{taxiId}/status")
public Mono<TaxiStatusDTO> updateTaxiStatus(@PathVariable("taxiId") String
taxiId, @RequestParam("status") TaxiStatus taxiStatus) {
return taxiService.updateTaxiStatus(taxiId, taxiStatus).map(t -> new
TaxiStatusDTO(t.getTaxiId(), t.getTaxiStatus()));
}

@PutMapping("/{taxiId}/location")
public Mono<TaxiLocationUpdatedEventResponseDTO>
updateLocation(@PathVariable("taxiId") String taxiId, @RequestBody
LocationDTO locationDTO) {
return taxiService.updateLocation(taxiId, locationDTO).map(t -> new
TaxiLocationUpdatedEventResponseDTO(taxiId));
}

@PostMapping
public Mono<TaxiRegisterEventResponseDTO> register(@RequestBody
TaxiRegisterEventDTO taxiRegisterEventDTO) {
return taxiService.register(taxiRegisterEventDTO).map(t -> new
TaxiRegisterEventResponseDTO(t.getTaxiId()));
}

}

We can understand the following from the previous code:

  • The getAvailableTaxis function is mapped to the URL /taxis and accepts taxi type, latitude, longitude, and radius in kilometers, and returns Taxis available in that geographical area
  • The getTaxiStatus function is mapped to the URL /taxis/{taxiId}/status and returns the status of the Taxi identified by the taxiId path variable
  • The updateTaxiStatus function is mapped to the URL /taxis/{taxiId}/status with the request method PUT and updates the status of the Taxi identified by the taxiId path variable
  • The updateLocation function is mapped to the URL /taxis/{taxiId}/location with the request method PUT and updates the location of the Taxi identified by the taxiId path variable
  • The register function is mapped to the URL /taxis with request method POST and registers a new Taxi into the system

The following code is TaxiBookingController, which caters to the registering, searching, status updating, and so on, of Taxis; it is available in spring-boot-2-taxi-book-service:

@RequestMapping("/taxibookings")
@RestController
public class TaxiBookingController {

private final TaxiBookingService taxiBookingService;

public TaxiBookingController(TaxiBookingService taxiBookingService) {
this.taxiBookingService = taxiBookingService;
}

@PostMapping
public Mono<TaxiBookedEventResponseDTO> book(@RequestBody TaxiBookedEventDTO
taxiBookedEventDTO) {
return taxiBookingService.book(taxiBookedEventDTO).map(t -> new
TaxiBookedEventResponseDTO(t.getTaxiBookingId()));
}

@PutMapping("/{taxiBookingId}/cancel")
public Mono<TaxiBookingCanceledEventResponseDTO>
cancel(@PathVariable("taxiBookingId") String taxiBookingId, @RequestBody
TaxiBookingCanceledEventDTO taxiBookingCanceledEventDTO) {
return taxiBookingService.cancel(taxiBookingId,
taxiBookingCanceledEventDTO).map(t -> new
TaxiBookingCanceledEventResponseDTO(t.getTaxiBookingId()));
}

@PutMapping("/{taxiBookingId}/accept")
public Mono<TaxiBookingAcceptedEventResponseDTO>
accept(@PathVariable("taxiBookingId") String taxiBookingId, @RequestBody
TaxiBookingAcceptedEventDTO taxiBookingAcceptedEventDTO) {
return taxiBookingService.accept(taxiBookingId,
taxiBookingAcceptedEventDTO).map(t -> new
TaxiBookingAcceptedEventResponseDTO(t.getTaxiBookingId(), t.getTaxiId(),
t.getAcceptedTime()));
}

@GetMapping
public Flux<TaxiBookingResponseDTO> getBookings(@RequestParam("type")
TaxiType taxiType, @RequestParam("latitude") Double latitude,
@RequestParam("longitude") Double longitude, @RequestParam(value = "radius",
defaultValue = "1") Double radius) {
return taxiBookingService.getBookings(taxiType, latitude, longitude,
radius).map(r -> new TaxiBookingResponseDTO(r.getContent().getName()));
}


}

We infer the following from the previous code:

  • The book function is mapped to the URL /taxibookings with request method POST and creates a Taxi booking for a particular Taxi type with start and end location
  • The cancel function is mapped to the URL /taxibookings/{taxiBookingId}/cancel and cancels a Taxi booking identified by the taxiBookingId path variable
  • The accept function is mapped to the URL /taxibookings/{taxiBookingId}/accept and enables a driver to accept a Taxi booking identified by the taxiBookingId path variable
  • The getBookings function is mapped to the URL /taxibookings and accepts taxi type, latitude, longitude, radius in a kilometers, and returns Taxi bookings available in that  geographical area
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.189.178.237