Skip to content

Commit

Permalink
feat: running the command with user-supplied parameters
Browse files Browse the repository at this point in the history
Refs: #26
  • Loading branch information
nergal-perm committed Jul 3, 2024
1 parent 7f3fad6 commit 4cbd9ef
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/main/java/ru/ewc/checklogic/LogicChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/ru/ewc/checklogic/ServerContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> 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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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("<div class='mb-3'>")
.append("<label for='%1$s' class='form-label'>%1$s:</label>")
Expand Down
21 changes: 19 additions & 2 deletions src/main/java/ru/ewc/checklogic/server/CommandPage.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String, String> 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(
Expand All @@ -105,4 +109,17 @@ public Response commandInfo(final Request request) {
)
);
}

private static Map<String, String> extractArgsFrom(final Body body) {
final Map<String, String> 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;
}

}
1 change: 1 addition & 0 deletions src/test/resources/tic-tac-toe/application.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
locators:
- request
- table
- cells

1 comment on commit 4cbd9ef

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on 4cbd9ef Jul 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 25-7a14407d discovered in src/main/java/ru/ewc/checklogic/LogicChecker.java) and submitted as #31. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

Please sign in to comment.