From 30b3a8832d0a4b9a5da580b0bffabccdfb0a7a53 Mon Sep 17 00:00:00 2001 From: RouxAntoine Date: Mon, 24 Jul 2023 19:45:21 +0200 Subject: [PATCH] feature: bind criteria to fetch request --- .../java/tk/antoine/roux/infrastructure/in/Criteria.java | 3 +++ .../antoine/roux/infrastructure/in/NodeController.java | 2 ++ src/main/resources/public/src/NodesComponent.js | 9 ++++----- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/main/java/tk/antoine/roux/infrastructure/in/Criteria.java b/src/main/java/tk/antoine/roux/infrastructure/in/Criteria.java index cec5c2b..db77eca 100644 --- a/src/main/java/tk/antoine/roux/infrastructure/in/Criteria.java +++ b/src/main/java/tk/antoine/roux/infrastructure/in/Criteria.java @@ -39,4 +39,7 @@ public record Criteria(String rawCriteria) { .getOrElse(false); } + public static boolean isEmpty(Criteria criteria) { + return criteria.rawCriteria.isEmpty(); + } } diff --git a/src/main/java/tk/antoine/roux/infrastructure/in/NodeController.java b/src/main/java/tk/antoine/roux/infrastructure/in/NodeController.java index 7fa99b1..7fb2ef3 100644 --- a/src/main/java/tk/antoine/roux/infrastructure/in/NodeController.java +++ b/src/main/java/tk/antoine/roux/infrastructure/in/NodeController.java @@ -15,6 +15,7 @@ import tk.antoine.roux.domain.usecases.Command.EmptyCommand; import tk.antoine.roux.domain.usecases.operation.GetNodesUseCase; import java.util.Optional; +import java.util.function.Predicate; @RestController @RequestMapping("${backoffice.configuration.api-prefix}/v1") @@ -31,6 +32,7 @@ public class NodeController { @GetMapping("/nodes") ResponseEntity listNode(@RequestParam(required = false, value = "criteria") Optional optionalCriteria) { return Option.ofOptional(optionalCriteria) + .filter(Predicate.not(Criteria::isEmpty)) .map(criteria -> criteria.toCommand().map(Command.class::cast)) .toEither(EMPTY_COMMAND) .getOrElseGet(Either::right) diff --git a/src/main/resources/public/src/NodesComponent.js b/src/main/resources/public/src/NodesComponent.js index 2db022b..bf5bd12 100644 --- a/src/main/resources/public/src/NodesComponent.js +++ b/src/main/resources/public/src/NodesComponent.js @@ -4,13 +4,12 @@ function Nodes({criteria}) { const [nodes, setNodes] = React.useState([]); React.useEffect(() => { - fetch("/api/v1/nodes") + let query = new URL("/api/v1/nodes", window.location.origin); + query.searchParams.append("criteria", criteria) + + fetch(query) .then(response => response.json()) .then(value => setNodes(value)); - }, []); - - React.useEffect(() => { - console.log(criteria); }, [criteria]); return (