From a6916ba5daf06044132d289892d7324b3d33d6f7 Mon Sep 17 00:00:00 2001 From: js0ny Date: Fri, 21 Nov 2025 12:10:08 +0000 Subject: [PATCH] feat: template for all endpoints --- .../IlpCourseworkApplication.java | 7 +- .../controller/DroneController.java | 62 ++++++++++++++---- .../data/AttrComparatorDto.java | 4 ++ .../ilp_coursework/data/AttrOperator.java | 4 ++ .../ilp_coursework/data/DeliveryPathDto.java | 7 ++ .../ilp_coursework/data/DronePathDto.java | 4 ++ .../ilp_coursework/data/MedDispathRecDto.java | 11 ++++ .../data/MedRequirementDto.java | 8 +++ .../service/DroneInfoService.java | 65 ++++++++++++------- 9 files changed, 132 insertions(+), 40 deletions(-) create mode 100644 src/main/java/io/github/js0ny/ilp_coursework/data/AttrComparatorDto.java create mode 100644 src/main/java/io/github/js0ny/ilp_coursework/data/AttrOperator.java create mode 100644 src/main/java/io/github/js0ny/ilp_coursework/data/DeliveryPathDto.java create mode 100644 src/main/java/io/github/js0ny/ilp_coursework/data/DronePathDto.java create mode 100644 src/main/java/io/github/js0ny/ilp_coursework/data/MedDispathRecDto.java create mode 100644 src/main/java/io/github/js0ny/ilp_coursework/data/MedRequirementDto.java diff --git a/src/main/java/io/github/js0ny/ilp_coursework/IlpCourseworkApplication.java b/src/main/java/io/github/js0ny/ilp_coursework/IlpCourseworkApplication.java index 2d3370f..81b1147 100644 --- a/src/main/java/io/github/js0ny/ilp_coursework/IlpCourseworkApplication.java +++ b/src/main/java/io/github/js0ny/ilp_coursework/IlpCourseworkApplication.java @@ -6,8 +6,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class IlpCourseworkApplication { - public static void main(String[] args) { - SpringApplication.run(IlpCourseworkApplication.class, args); - } - + public static void main(String[] args) { + SpringApplication.run(IlpCourseworkApplication.class, args); + } } diff --git a/src/main/java/io/github/js0ny/ilp_coursework/controller/DroneController.java b/src/main/java/io/github/js0ny/ilp_coursework/controller/DroneController.java index ecdf1c4..cbddc24 100644 --- a/src/main/java/io/github/js0ny/ilp_coursework/controller/DroneController.java +++ b/src/main/java/io/github/js0ny/ilp_coursework/controller/DroneController.java @@ -1,30 +1,35 @@ package io.github.js0ny.ilp_coursework.controller; +import io.github.js0ny.ilp_coursework.data.AttrComparatorDto; +import io.github.js0ny.ilp_coursework.data.DeliveryPathDto; +import io.github.js0ny.ilp_coursework.data.DroneDto; +import io.github.js0ny.ilp_coursework.data.DronePathDto; +import io.github.js0ny.ilp_coursework.data.MedDispathRecDto; +import io.github.js0ny.ilp_coursework.service.DroneInfoService; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; - -import io.github.js0ny.ilp_coursework.service.DroneInfoService; -import io.github.js0ny.ilp_coursework.data.DroneDto; import org.springframework.web.client.RestTemplate; -import java.util.Arrays; - /** * Main Rest Controller for the ILP Coursework 2 application. *

- * This class handles incoming HTTP requests for the API under {@code /api/v1} path (defined in CW2) + * This class handles incoming HTTP requests for the API under {@code /api/v1} + * path (defined in CW2) * The business logic is delegated to {@link DroneInfoService} */ @RestController @RequestMapping("/api/v1") public class DroneController { + private final DroneInfoService droneService; private final RestTemplate restTemplate = new RestTemplate(); /** - * Constructor of the {@code DroneController} with the business logic dependency {@code DroneInfoService} + * Constructor of the {@code DroneController} with the business logic dependency + * {@code DroneInfoService} *

- * We handle the {@code baseUrl} here. Use a predefined URL if the environment variable {@code ILP_ENDPOINT} + * We handle the {@code baseUrl} here. Use a predefined URL if the environment + * variable {@code ILP_ENDPOINT} * is not given. * * @param droneService The service component that contains all business logic @@ -34,9 +39,11 @@ public class DroneController { } /** - * Handles GET requests to retrieve an array of drones (identified by id) that has the capability of cooling + * Handles GET requests to retrieve an array of drones (identified by id) that + * has the capability of cooling * - * @param state The path variable that indicates the return should have or not have the capability + * @param state The path variable that indicates the return should have or not + * have the capability * @return An array of drone id with cooling capability. */ @GetMapping("/dronesWithCooling/{state}") @@ -44,6 +51,13 @@ public class DroneController { return droneService.dronesWithCooling(state); } + /** + * Handles GET requests to retrieve the drone detail identified by id + * + * @param id The id of the drone to be queried. + * @return 200 with {@link DroneDto}-style json if success, 404 if {@code id} + * not found, 400 otherwise + */ @GetMapping("/droneDetails/{id}") public ResponseEntity getDroneDetail(@PathVariable String id) { try { @@ -54,9 +68,31 @@ public class DroneController { } } - @PostMapping("queryAvailableDrones") - public int queryAvailableDrones() { - return 1; + @GetMapping("/queryAsPath/{attrName}/{attrVal}") + public int[] getIdByAttrMap( + @PathVariable String attrName, + @PathVariable String attrVal) { + return droneService.dronesWithAttribute(attrName, attrVal); + } + + @PostMapping("/query") + public int[] getIdByAttrMapPost(@RequestBody AttrComparatorDto[] attrComparators) { + return new int[] {}; + } + + @PostMapping("/queryAvailableDrones") + public int[] queryAvailableDrones(@RequestBody MedDispathRecDto[] records) { + return new int[] {}; + } + + @PostMapping("/calcDeliveryPath") + public DeliveryPathDto calculateDeliveryPath(@RequestBody MedDispathRecDto[] record) { + return new DeliveryPathDto(0.0f, 0, new DronePathDto[] {}); + } + + @PostMapping("/calcDeliveryPathAsGeoJson") + public String calculateDeliveryPathAsGeoJson(@RequestBody MedDispathRecDto[] record) { + return "{}"; } } diff --git a/src/main/java/io/github/js0ny/ilp_coursework/data/AttrComparatorDto.java b/src/main/java/io/github/js0ny/ilp_coursework/data/AttrComparatorDto.java new file mode 100644 index 0000000..743e46c --- /dev/null +++ b/src/main/java/io/github/js0ny/ilp_coursework/data/AttrComparatorDto.java @@ -0,0 +1,4 @@ +package io.github.js0ny.ilp_coursework.data; + +public record AttrComparatorDto(String attribute, String operator, String value) { +} diff --git a/src/main/java/io/github/js0ny/ilp_coursework/data/AttrOperator.java b/src/main/java/io/github/js0ny/ilp_coursework/data/AttrOperator.java new file mode 100644 index 0000000..0570c36 --- /dev/null +++ b/src/main/java/io/github/js0ny/ilp_coursework/data/AttrOperator.java @@ -0,0 +1,4 @@ +package io.github.js0ny.ilp_coursework.data; + +public enum AttrOperator { +} diff --git a/src/main/java/io/github/js0ny/ilp_coursework/data/DeliveryPathDto.java b/src/main/java/io/github/js0ny/ilp_coursework/data/DeliveryPathDto.java new file mode 100644 index 0000000..cf5fc84 --- /dev/null +++ b/src/main/java/io/github/js0ny/ilp_coursework/data/DeliveryPathDto.java @@ -0,0 +1,7 @@ +package io.github.js0ny.ilp_coursework.data; + +public record DeliveryPathDto( + float totalCost, + int totalMoves, + DronePathDto[] dronePaths) { +} diff --git a/src/main/java/io/github/js0ny/ilp_coursework/data/DronePathDto.java b/src/main/java/io/github/js0ny/ilp_coursework/data/DronePathDto.java new file mode 100644 index 0000000..807c62f --- /dev/null +++ b/src/main/java/io/github/js0ny/ilp_coursework/data/DronePathDto.java @@ -0,0 +1,4 @@ +package io.github.js0ny.ilp_coursework.data; + +public record DronePathDto() { +} diff --git a/src/main/java/io/github/js0ny/ilp_coursework/data/MedDispathRecDto.java b/src/main/java/io/github/js0ny/ilp_coursework/data/MedDispathRecDto.java new file mode 100644 index 0000000..a1a8857 --- /dev/null +++ b/src/main/java/io/github/js0ny/ilp_coursework/data/MedDispathRecDto.java @@ -0,0 +1,11 @@ +package io.github.js0ny.ilp_coursework.data; + +import java.time.LocalDate; +import java.time.LocalTime; + +public record MedDispathRecDto( + int id, + LocalDate date, + LocalTime time, + MedRequirementDto requirements) { +} diff --git a/src/main/java/io/github/js0ny/ilp_coursework/data/MedRequirementDto.java b/src/main/java/io/github/js0ny/ilp_coursework/data/MedRequirementDto.java new file mode 100644 index 0000000..6cb2d4c --- /dev/null +++ b/src/main/java/io/github/js0ny/ilp_coursework/data/MedRequirementDto.java @@ -0,0 +1,8 @@ +package io.github.js0ny.ilp_coursework.data; + +public record MedRequirementDto( + float capacity, + boolean cooling, + boolean heating, + float maxCost +) {} diff --git a/src/main/java/io/github/js0ny/ilp_coursework/service/DroneInfoService.java b/src/main/java/io/github/js0ny/ilp_coursework/service/DroneInfoService.java index 2411f73..de7c52a 100644 --- a/src/main/java/io/github/js0ny/ilp_coursework/service/DroneInfoService.java +++ b/src/main/java/io/github/js0ny/ilp_coursework/service/DroneInfoService.java @@ -1,14 +1,10 @@ package io.github.js0ny.ilp_coursework.service; import io.github.js0ny.ilp_coursework.data.DroneDto; -import org.springframework.stereotype.Service; -import org.springframework.web.client.RestTemplate; -import org.springframework.web.util.UriBuilder; -import org.springframework.web.util.UriComponentsBuilder; - import java.net.URI; import java.util.Arrays; -import java.util.stream.Stream; +import org.springframework.stereotype.Service; +import org.springframework.web.client.RestTemplate; @Service public class DroneInfoService { @@ -22,7 +18,6 @@ public class DroneInfoService { * Constructor, handles the base url here. */ public DroneInfoService() { - String baseUrl = System.getenv("ILP_ENDPOINT"); if (baseUrl == null || baseUrl.isBlank()) { this.baseUrl = "https://ilp-rest-2025-bvh6e9hschfagrgy.ukwest-01.azurewebsites.net/"; @@ -39,21 +34,23 @@ public class DroneInfoService { * Return an array of ids of drones with/without cooling capability * * @param state determines the capability filtering - * @return if {@code state} is true, return ids of drones with cooling capability, else without cooling + * @return if {@code state} is true, return ids of drones with cooling + * capability, else without cooling */ public int[] dronesWithCooling(boolean state) { URI droneUrl = URI.create(baseUrl).resolve(dronesEndpoint); - - DroneDto[] drones = restTemplate.getForObject(droneUrl, DroneDto[].class); + DroneDto[] drones = restTemplate.getForObject( + droneUrl, + DroneDto[].class); if (drones == null) { - return new int[]{}; + return new int[] {}; } - return Arrays.stream(drones). - filter(drone -> drone.capability().cooling() == state). - mapToInt(drone -> Integer.parseInt(drone.id())). - toArray(); + return Arrays.stream(drones) + .filter(drone -> drone.capability().cooling() == state) + .mapToInt(drone -> Integer.parseInt(drone.id())) + .toArray(); } /** @@ -61,14 +58,17 @@ public class DroneInfoService { * * @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 + * @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); + URI droneUrl = URI.create(baseUrl).resolve(dronesEndpoint); + DroneDto[] drones = restTemplate.getForObject( + droneUrl, + DroneDto[].class); if (drones == null) { throw new NullPointerException("drone cannot be found"); @@ -80,8 +80,27 @@ public class DroneInfoService { } } - throw new IllegalArgumentException("drone with that ID cannot be found"); - - + throw new IllegalArgumentException( + "drone with that ID cannot be found"); } + + /** + * Return an array of ids of drones with a given attribute name and value + * + * @param attrName the attribute name to filter on + * @param attrVal the attribute value to filter on + * @return array of drone ids matching the attribute name and value + */ + public int[] dronesWithAttribute(String attrName, String attrVal) { + URI droneUrl = URI.create(baseUrl).resolve(dronesEndpoint); + DroneDto[] drones = restTemplate.getForObject( + droneUrl, + DroneDto[].class + ); + + // TODO: Logic unimplemented + return new int[] {}; + } + + public int[] dronesMatchesRequirements() }