Added stuff.

This commit is contained in:
Andrew Lalis 2025-09-05 11:47:45 -04:00
commit 0a969776dd
7 changed files with 239 additions and 0 deletions

16
.gitignore vendored Normal file
View File

@ -0,0 +1,16 @@
.dub
docs.json
__dummy.html
docs/
/party-signup-api
party-signup-api.so
party-signup-api.dylib
party-signup-api.dll
party-signup-api.a
party-signup-api.lib
party-signup-api-test-*
*.exe
*.pdb
*.o
*.obj
*.lst

View File

@ -0,0 +1,16 @@
{
"entries": [
{
"comment": "Potatoes",
"name": "Andrew"
},
{
"comment": "Soup!",
"name": "Andrew"
},
{
"comment": "Cakes",
"name": "Grace"
}
]
}

12
api/dub.json Normal file
View File

@ -0,0 +1,12 @@
{
"authors": [
"Andrew Lalis"
],
"copyright": "Copyright © 2025, Andrew Lalis",
"dependencies": {
"handy-http-starter": "~>1.6.0"
},
"description": "A minimal D application.",
"license": "MIT",
"name": "party-signup-api"
}

19
api/dub.selections.json Normal file
View File

@ -0,0 +1,19 @@
{
"fileVersion": 1,
"versions": {
"asdf": "0.7.17+commit.5.g7f77a30",
"dxml": "0.4.5",
"handy-http-data": "1.3.0",
"handy-http-handlers": "1.1.0",
"handy-http-primitives": "1.8.1",
"handy-http-starter": "1.6.0",
"handy-http-transport": "1.8.0",
"handy-http-websockets": "1.2.0",
"mir-algorithm": "3.22.4",
"mir-core": "1.7.3",
"path-matcher": "1.2.0",
"silly": "1.1.1",
"slf4d": "4.1.1",
"streams": "3.6.0"
}
}

BIN
api/party-signup-api Executable file

Binary file not shown.

60
api/source/app.d Normal file
View File

@ -0,0 +1,60 @@
import slf4d;
import handy_http_starter;
void main() {
Http1TransportConfig transportConfig = defaultConfig();
transportConfig.port = 8080;
HttpTransport transport = new TaskPoolHttp1Transport(new AppHandler(), transportConfig);
transport.start();
}
struct Entry {
string partyName;
string name;
string comment;
}
class AppHandler : HttpRequestHandler {
void handle(ref ServerHttpRequest request, ref ServerHttpResponse response) {
response.headers.add("Access-Control-Allow-Origin", "*");
response.headers.add("Access-Control-Allow-Methods", "*");
response.headers.add("Access-Control-Allow-Headers", "Content-Type");
if (request.method == HttpMethod.GET) {
getEntries(request.getParamAs!string("party"), response);
} else if (request.method == HttpMethod.POST) {
auto payload = readJsonBodyAs!Entry(request);
addEntry(payload);
}
}
}
void getEntries(string partyName, ref ServerHttpResponse response) {
import std.file;
import std.path;
import std.json;
if (!exists("data")) return;
const filename = buildPath("data", partyName ~ ".json");
JSONValue root = parseJSON(readText(filename));
response.writeBodyString(root.object["entries"].toJSON(), ContentTypes.APPLICATION_JSON);
}
void addEntry(in Entry payload) {
import std.file;
import std.path;
import std.json;
if (!exists("data")) mkdir("data");
const filename = buildPath("data", payload.partyName ~ ".json");
if (!exists(filename)) {
JSONValue obj = JSONValue.emptyObject;
obj.object["entries"] = JSONValue.emptyArray;
write(filename, obj.toPrettyString());
}
JSONValue root = parseJSON(readText(filename));
JSONValue node = JSONValue.emptyObject;
node.object["name"] = JSONValue(payload.name);
node.object["comment"] = JSONValue(payload.comment);
root.object["entries"].array ~= node;
write(filename, root.toPrettyString());
info("Added entry.");
}

116
app.html Normal file
View File

@ -0,0 +1,116 @@
<!DOCTYPE html>
<html>
<head>
<title>Party Signup 🎉</title>
<style>
table {
border-collapse: collapse;
width: 600px;
}
th, td {
border-collapse: collapse;
border: 1px solid black;
padding: 0.25rem 0.5rem;
}
</style>
</head>
<body>
<h1>Party Signup 🎉</h1>
<p>A simple list of people and what they're bringing!</p>
<table id="entries-table">
<thead>
<tr>
<th>Name</th>
<th>Bringing</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
<h3>Register below!</h3>
<div>
<label for="name-input">Name</label>
<input id="name-input" />
</div>
<div>
<label for="comment-input">What are you bringing?</label>
<input id="comment-input" />
</div>
<div>
<button type="button" id="register-button">Register</button>
</div>
<script>
const partyName = "grace-birthday"
const entriesTable = document.getElementById("entries-table")
const tableBody = entriesTable.getElementsByTagName("tbody")[0]
const nameInput = document.getElementById("name-input")
const commentInput = document.getElementById("comment-input")
const registerButton = document.getElementById("register-button")
registerButton.onclick = addEntry
nameInput.onchange = validateInput
nameInput.onkeyup = validateInput
commentInput.onchange = validateInput
commentInput.onkeyup = validateInput
validateInput()
window.onload = () => {
loadEntries()
}
async function loadEntries() {
tableBody.innerHTML = ''
const response = await fetch("http://localhost:8080?party=" + partyName)
if (!response.ok) {
console.error(await response.text())
} else {
for (const entry of await response.json()) {
const row = document.createElement("tr")
const td1 = document.createElement("td")
td1.innerText = entry.name
const td2 = document.createElement("td")
td2.innerText = entry.comment
row.appendChild(td1)
row.appendChild(td2)
tableBody.appendChild(row)
}
}
}
async function addEntry() {
const name = nameInput.value
const comment = commentInput.value
const payload = {
name: name,
comment: comment,
partyName: partyName
}
const response = await fetch("http://localhost:8080", {
method: "POST",
body: JSON.stringify(payload)
})
if (!response.ok) {
console.error(await response.text())
} else {
nameInput.value = ''
commentInput.value = ''
validateInput()
await loadEntries()
}
}
function validateInput() {
const name = nameInput.value
const comment = commentInput.value
const valid = name.trim().length >= 3 && comment.trim().length >= 3
console.log(valid)
registerButton.disabled = !valid
}
</script>
</body>
</html>