MaCH repo

This commit is contained in:
2025-07-24 12:46:01 -05:00
committed by Nick Ricketts
commit 9be17aae82
106 changed files with 6345 additions and 0 deletions

View File

@@ -0,0 +1,2 @@
insert into todos(user_id, title)
values({{user_id}}, {{title}});

View File

@@ -0,0 +1,7 @@
CREATE TABLE IF NOT EXISTS todos (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER NOT NULL,
title TEXT NOT NULL,
finished INTEGER CHECK(finished IN (1))
);
CREATE INDEX IF NOT EXISTS idx_todos_user_id ON todos(user_id);

View File

@@ -0,0 +1,3 @@
delete from todos
where user_id = {{user_id}}
and id = {{id}};

View File

@@ -0,0 +1,3 @@
select id, title, finished
from todos
where user_id = {{user_id}};

View File

@@ -0,0 +1,17 @@
<div>
<form action="/todos/{{id}}" method="post" style="display:inline">
<input type="hidden" name="http_method" value="PATCH">
{{^finished}}
<input type="checkbox" name="finished" value="1">
{{/finished}}
{{#finished}}
<input type="checkbox" name="finished" value="1" checked>
{{/finished}}
{{title}}
<button type="submit">save</button>
</form>
<form action="/todos/{{id}}" method="post" style="display:inline">
<input type="hidden" name="http_method" value="delete">
<button type="submit">delete</button>
</form>
</div>

View File

@@ -0,0 +1,108 @@
#include <mach.h>
#include <sqlite.h>
#include <session_auth.h>
config todos(){
return (config) {
.name = "todos",
.resources = {{
{"todos", "/todos",
.get = {{
query((d){
.set = "todos",
.db = "todos_db",
.query = (asset){
#embed "get_todos.sql"
}
}),
render((r){(asset){
#embed "todos.mustache.html"
}})
}},
.post = {{
validate((v){
.name = "title",
.validation = "^\\S{1,16}$",
.message = "must be 1-16 characters, no spaces"
}),
query((d){
.db = "todos_db",
.query = (asset){
#embed "create_todo.sql"
}
}),
emit((e){"todo_created"}),
redirect((u){"todos"})
}},
.context = {
{"todo", (asset){
#embed "todo.mustache.html"
}}
}
},
{"todo", "/todos/:id",
.patch = {{
validate((v){
.name = "finished",
.optional = true,
.validation = "1",
.message = "must be 1"
}),
find((d){
.db = "todos_db",
.query = (asset){
#embed "update_todo.sql"
}
}),
redirect((u){"todos"})
}},
.delete = {{
find((d){
.db = "todos_db",
.query = (asset){
#embed "delete_todo.sql"
}
}),
redirect((u){"todos"})
}},
.before = {
validate((v){
.name = "id",
.validation = "^\\d{1,10}$",
.message = "must be between 1-9999999999"
})
}
}},
.before = {
session(),
logged_in()
}
},
.databases = {{
.engine = sqlite_db,
.name = "todos_db",
.connect = "file:todo.db?mode=rwc",
.migrations = {(asset){
#embed "create_todos_table.sql"
}}
}},
.publishes = {
{"todo_created",
.with = {
"user_id",
"title"
}
}
},
.modules = {sqlite, session_auth}
};
}

View File

@@ -0,0 +1,16 @@
{{< layout}}
{{$body}}
<form action="/todos" method="post">
<input type="text" name="title" placeholder="new todo" required>
<button type="submit">add</button>
</form>
{{^todos}}
<p>no todos</p>
{{/todos}}
{{#todos}}
{{#.}}
{{> todo}}
{{/.}}
{{/todos}}
{{/body}}
{{/layout}}

View File

@@ -0,0 +1,4 @@
update todos
set finished = {{finished}}
where user_id = {{user_id}}
and id = {{id}};