From 4cbd9efb5c77902d7653bea5d62c59c0022c7a28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=95=D0=B2=D0=B3=D0=B5=D0=BD=D0=B8=D0=B9=20=D0=A2=D0=B5?= =?UTF-8?q?=D1=80=D0=B5=D1=85=D0=BE=D0=B2?= Date: Wed, 3 Jul 2024 11:34:19 +0400 Subject: [PATCH] feat: running the command with user-supplied parameters Refs: #26 --- .../java/ru/ewc/checklogic/LogicChecker.java | 1 + .../java/ru/ewc/checklogic/ServerContext.java | 11 ++++++++++ .../checklogic/server/CommandMetadata.java | 2 +- .../ru/ewc/checklogic/server/CommandPage.java | 21 +++++++++++++++++-- .../resources/tic-tac-toe/application.yaml | 1 + 5 files changed, 33 insertions(+), 3 deletions(-) diff --git a/src/main/java/ru/ewc/checklogic/LogicChecker.java b/src/main/java/ru/ewc/checklogic/LogicChecker.java index 283a07a..a26a81c 100644 --- a/src/main/java/ru/ewc/checklogic/LogicChecker.java +++ b/src/main/java/ru/ewc/checklogic/LogicChecker.java @@ -50,6 +50,7 @@ private LogicChecker() { // Utility class } + // @todo #25 Recreate ComputationContext for each request public static void main(final String[] args) throws IOException { if (args.length != 1) { throw new IllegalArgumentException("Please provide the path to the resources"); diff --git a/src/main/java/ru/ewc/checklogic/ServerContext.java b/src/main/java/ru/ewc/checklogic/ServerContext.java index abbb827..5107eba 100644 --- a/src/main/java/ru/ewc/checklogic/ServerContext.java +++ b/src/main/java/ru/ewc/checklogic/ServerContext.java @@ -47,6 +47,17 @@ public ServerContext(final ComputationContext context) { } public void perform(final String command) { + this.perform(command, Map.of()); + } + + public void perform(final String command, final Map args) { + args.forEach( + (key, value) -> { + if (!value.isEmpty()) { + final String[] split = key.split("::"); + this.context.setValueFor(split[0], split[1], value); + } + }); this.context.perform(command); } diff --git a/src/main/java/ru/ewc/checklogic/server/CommandMetadata.java b/src/main/java/ru/ewc/checklogic/server/CommandMetadata.java index a66df80..ba0f94e 100644 --- a/src/main/java/ru/ewc/checklogic/server/CommandMetadata.java +++ b/src/main/java/ru/ewc/checklogic/server/CommandMetadata.java @@ -80,7 +80,7 @@ public String namesAsHtmlList() { * @return The command arguments as an HTML form to be used in a page template. */ public String commandArgsAsHtmlForm(final String command, final ServerContext context) { - return this.metadata.get(command).stream().map( + return this.metadata.get(command).stream().distinct().map( arg -> new StringBuilder() .append("
") .append("") diff --git a/src/main/java/ru/ewc/checklogic/server/CommandPage.java b/src/main/java/ru/ewc/checklogic/server/CommandPage.java index 2a93c03..9b54150 100644 --- a/src/main/java/ru/ewc/checklogic/server/CommandPage.java +++ b/src/main/java/ru/ewc/checklogic/server/CommandPage.java @@ -24,9 +24,13 @@ package ru.ewc.checklogic.server; import com.renomad.minum.templating.TemplateProcessor; +import com.renomad.minum.web.Body; import com.renomad.minum.web.Request; import com.renomad.minum.web.Response; import com.renomad.minum.web.WebFramework; +import java.net.URLDecoder; +import java.nio.charset.StandardCharsets; +import java.util.HashMap; import java.util.Map; import ru.ewc.checklogic.ServerContext; import ru.ewc.decisions.api.DecitaException; @@ -73,12 +77,12 @@ public void register(final WebFramework web) { web.registerPath(GET, "command", this::commandInfo); } - // @todo #25 Pass the filled parameters to the command public Response executeCommand(final Request request) { final String command = request.body().asString("command"); + final Map args = CommandPage.extractArgsFrom(request.body()); Response response; try { - this.computation.perform(command); + this.computation.perform(command, args); response = Response.htmlOk("OK", Map.of("HX-Redirect", "/")); } catch (final DecitaException exception) { response = Response.htmlOk( @@ -105,4 +109,17 @@ public Response commandInfo(final Request request) { ) ); } + + private static Map extractArgsFrom(final Body body) { + final Map result = new HashMap<>(); + body.getKeys().forEach( + key -> { + final String decoded = URLDecoder.decode(key, StandardCharsets.UTF_8); + if (decoded.contains("::") && !body.asString(key).endsWith("=")) { + result.put(decoded, body.asString(key)); + } + }); + return result; + } + } diff --git a/src/test/resources/tic-tac-toe/application.yaml b/src/test/resources/tic-tac-toe/application.yaml index 136fc42..86179b4 100644 --- a/src/test/resources/tic-tac-toe/application.yaml +++ b/src/test/resources/tic-tac-toe/application.yaml @@ -1,3 +1,4 @@ locators: + - request - table - cells \ No newline at end of file