Skip to content

Commit 43ab144

Browse files
committed
feat(state): ability to reset state from the web UI
Closes: #68
1 parent 773a6ff commit 43ab144

File tree

4 files changed

+75
-2
lines changed

4 files changed

+75
-2
lines changed

src/main/java/ru/ewc/checklogic/LogicChecker.java

+2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import ru.ewc.checklogic.server.CommandPage;
3131
import ru.ewc.checklogic.server.ContextPage;
3232
import ru.ewc.checklogic.server.Endpoints;
33+
import ru.ewc.checklogic.server.StatePage;
3334
import ru.ewc.checklogic.server.config.ConfigPage;
3435

3536
/**
@@ -58,6 +59,7 @@ public static void main(final String[] args) {
5859
registerEndpoints(web, new CommandPage(context));
5960
registerEndpoints(web, new ContextPage(context, factory.configuration()));
6061
registerEndpoints(web, new AllEndpoints(context, factory.configuration()));
62+
registerEndpoints(web, new StatePage(context));
6163
minum.block();
6264
}
6365

src/main/java/ru/ewc/checklogic/server/Endpoints.java

+5
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ public interface Endpoints {
4444
*/
4545
RequestLine.Method POST = RequestLine.Method.POST;
4646

47+
/**
48+
* The shortcut to define an endpoint for a DELETE method.
49+
*/
50+
RequestLine.Method DELETE = RequestLine.Method.DELETE;
51+
4752
/**
4853
* Content type for CSS files.
4954
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2024 Decision-Driven Development
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
package ru.ewc.checklogic.server;
25+
26+
import com.renomad.minum.web.Request;
27+
import com.renomad.minum.web.RequestLine;
28+
import com.renomad.minum.web.Response;
29+
import com.renomad.minum.web.WebFramework;
30+
import java.util.Map;
31+
import ru.ewc.checklogic.ServerInstance;
32+
33+
/**
34+
* I am a class providing access to the state page.
35+
*
36+
* @since 0.4.1
37+
*/
38+
public final class StatePage implements Endpoints {
39+
/**
40+
* The context that stores the current state of the system.
41+
*/
42+
private final ServerInstance context;
43+
44+
public StatePage(final ServerInstance context) {
45+
this.context = context;
46+
}
47+
48+
@Override
49+
public void register(final WebFramework web) {
50+
web.registerPath(RequestLine.Method.DELETE, "state", this::resetState);
51+
}
52+
53+
private Response resetState(final Request request) {
54+
assert request.requestLine().getMethod().equals(RequestLine.Method.DELETE);
55+
this.context.initialize();
56+
return Response.htmlOk("OK", Map.of("HX-Redirect", "/state"));
57+
}
58+
}

src/main/resources/templates/state.html

+10-2
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,19 @@
2424
<div class="row">
2525
<div class="col">
2626
<h1>State entities</h1>
27-
{{ state }}
27+
<div class="row">
28+
<div class="col col-4">
29+
<button class="btn btn-primary" hx-delete="/state" hx-trigger="click">Reset</button>
30+
</div>
31+
</div>
32+
<div class="row">
33+
{{ state }}
34+
</div>
2835
</div>
2936
<div class="col">
3037
<h1>Context</h1>
31-
<form class="mb-3" hx-post="/context" hx-target="#commands" hx-swap="innerHTML" hx-trigger="submit, load">
38+
<form class="mb-3" hx-post="/context" hx-target="#commands" hx-swap="innerHTML"
39+
hx-trigger="submit, load">
3240
<div class="mb-3">
3341
<label class="col-form-label" for="reqValues">Request values:</label>
3442
<textarea class="form-control" id="reqValues" name="reqValues" rows="3"

0 commit comments

Comments
 (0)