feat(cw2): /api/v1/droneDetails impl
This commit is contained in:
parent
3c96f9d5af
commit
44d510ddd4
7 changed files with 141 additions and 14 deletions
|
|
@ -1,5 +1,6 @@
|
|||
package io.github.js0ny.ilp_coursework.controller;
|
||||
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import io.github.js0ny.ilp_coursework.service.DroneInfoService;
|
||||
|
|
@ -43,15 +44,15 @@ public class DroneController {
|
|||
return droneService.dronesWithCooling(state);
|
||||
}
|
||||
|
||||
// @GetMapping("/droneDetails/{id}")
|
||||
// public DroneDto getDroneDetail(@PathVariable int id) {
|
||||
// String droneUrl = baseUrl + "drones";
|
||||
//
|
||||
// DroneDto[] drones = restTemplate.getForObject(droneUrl, DroneDto[].class);
|
||||
//
|
||||
// return new DroneDto();
|
||||
//
|
||||
// }
|
||||
@GetMapping("/droneDetails/{id}")
|
||||
public ResponseEntity<DroneDto> getDroneDetail(@PathVariable String id) {
|
||||
try {
|
||||
DroneDto drone = droneService.droneDetail(id);
|
||||
return ResponseEntity.ok(drone);
|
||||
} catch (IllegalArgumentException ex) {
|
||||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
}
|
||||
|
||||
@PostMapping("queryAvailableDrones")
|
||||
public int queryAvailableDrones() {
|
||||
|
|
|
|||
|
|
@ -56,18 +56,32 @@ public class DroneInfoService {
|
|||
toArray();
|
||||
}
|
||||
|
||||
// TODO: This is function is WIP
|
||||
public Stream<DroneDto> droneDetail(int id) {
|
||||
/**
|
||||
* Return a {@link DroneDto}-style json data structure with the given {@code id}
|
||||
*
|
||||
* @param id The id of the drone
|
||||
* @return drone json body of given id
|
||||
* @throws NullPointerException when cannot fetch available drones from remote
|
||||
* @throws IllegalArgumentException when drone with given {@code id} cannot be found
|
||||
* this should lead to a 404
|
||||
*/
|
||||
public DroneDto droneDetail(String id) {
|
||||
String droneUrl = baseUrl + dronesEndpoint;
|
||||
|
||||
DroneDto[] drones = restTemplate.getForObject(droneUrl, DroneDto[].class);
|
||||
|
||||
if (drones == null) {
|
||||
throw new IllegalArgumentException("drone with that ID cannot be found");
|
||||
throw new NullPointerException("drone cannot be found");
|
||||
}
|
||||
|
||||
return Arrays.stream(drones).
|
||||
filter(drone -> Integer.parseInt(String.valueOf(drone.id())) == id);
|
||||
for (var drone : drones) {
|
||||
if (drone.id().equals(id)) {
|
||||
return drone;
|
||||
}
|
||||
}
|
||||
|
||||
throw new IllegalArgumentException("drone with that ID cannot be found");
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue