MaCH repo

This commit is contained in:
2025-07-24 12:46:01 -05:00
committed by Nick Ricketts
commit f09b20e327
48 changed files with 4971 additions and 0 deletions

View File

@@ -0,0 +1,5 @@
{{< layout}}
{{$body}}
<p>not found</p>
{{/body}}
{{/layout}}

View File

@@ -0,0 +1,5 @@
{{< layout}}
{{$body}}
<p>error</p>
{{/body}}
{{/layout}}

View File

@@ -0,0 +1,5 @@
{{< layout}}
{{$body}}
<p>about us</p>
{{/body}}
{{/layout}}

View File

@@ -0,0 +1,5 @@
{{< layout}}
{{$body}}
<p>contact us</p>
{{/body}}
{{/layout}}

View File

@@ -0,0 +1,3 @@
insert into todos(user_id, title)
values({{user_id}}, {{title}})
returning id, title, finished;

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,4 @@
delete from todos
where user_id = {{user_id}}
and id = {{id}}
returning id;

View File

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

View File

@@ -0,0 +1,5 @@
{{< layout}}
{{$body}}
<p>home</p>
{{/body}}
{{/layout}}

View File

@@ -0,0 +1,25 @@
<html>
<head>
<link rel='icon' href='{{asset:favicon.png}}'>
{{$head}}
{{/head}}
</head>
<body>
<p>
{{^user}}
<a href='{{url:login}}'>sign in</a>
{{/user}}
{{#user}}
welcome, {{short_name}}
{{/user}}
</p>
<nav>
<a href='{{url:home}}'>home</a>
<a href='{{url:about}}'>about us</a>
<a href='{{url:contact}}'>contact us</a>
<a href='{{url:todos}}'>todos</a>
</nav>
{{$body}}
{{/body}}
</body>
</html>

116
05_todo_sse_datastar/main.c Normal file
View File

@@ -0,0 +1,116 @@
#include <mach.h>
#include <sqlite.h>
#include <datastar.h>
#include <session_auth.h>
string const create_todos_table = (asset){
#embed "create_todos_table.sql"
};
string const get_todos = (asset){
#embed "get_todos.sql"
};
string const create_todo = (asset){
#embed "create_todo.sql"
};
string const update_todo = (asset){
#embed "update_todo.sql"
};
string const delete_todo = (asset){
#embed "delete_todo.sql"
};
string const layout = (asset){
#embed "layout.mustache.html"
};
string const home = (asset){
#embed "home.mustache.html"
};
string const about = (asset){
#embed "about.mustache.html"
};
string const contact = (asset){
#embed "contact.mustache.html"
};
string const todos = (asset){
#embed "todos.mustache.html"
};
string const todo = (asset){
#embed "todo.mustache.html"
};
string const todo_item = (asset){
#embed "todo_item.mustache.html"
};
string const e5xx = (asset){
#embed "5xx.mustache.html"
};
string const e404 = (asset){
#embed "404.mustache.html"
};
void mach(){
middleware(session());
resource("home", "/",
.get = {mustache("home")}
);
resource("about", "/about",
.get = {mustache("about")}
);
resource("contact", "/contact",
.get = {mustache("contact")}
);
resource("todos", "/todos",
.all = {logged_in()},
.sse = {"todos:{{user_id}}"},
.get = {
sqlite_query({"todos_db", get_todos, "todos_data"}),
mustache("todos")
},
.post = {
input({"title", m_not_empty}),
sqlite_query({"todos_db", create_todo, "todo_data", .must_exist = true}),
datastar_sse("todos:{{user_id}}", .target = "todos", .mode = mode_prepend, .elements = {"todo"})
}
);
resource("todo", "/todos/:id",
.all = {
logged_in(),
input({"id", m_positive})
},
.patch = {
input({"finished", "1", "must be 1", .optional = true}),
sqlite_query({"todos_db", update_todo, "todo_data", .must_exist = true}),
datastar_sse("todos:{{user_id}}", .target = "todo_{{id}}", .mode = mode_replace, .elements = {"todo"})
},
.delete = {
sqlite_query({"todos_db", delete_todo, .must_exist = true}),
datastar_sse("todos:{{user_id}}", .target = "todo_{{id}}", .mode = mode_remove)
}
);
sqlite_database(
.name = "todos_db",
.connect = "file:todo.db?mode=rwc",
.migrations = {create_todos_table}
);
error(m_error, {mustache("5xx")});
error(m_not_found, {mustache("404")});
context("layout", layout);
context("home", home);
context("about", about);
context("contact", contact);
context("todos", todos);
context("todo", todo);
context("todo_item", todo_item);
context("5xx", e5xx);
context("404", e404);
module(sqlite);
module(datastar);
module(session_auth);
}

View File

View File

@@ -0,0 +1,3 @@
{{#todo}}
{{> todo_item}}
{{/todo}}

View File

@@ -0,0 +1,10 @@
<div id="todo_{{id}}">
{{^finished}}
<input type="checkbox" data-on:click__prevent="@patch('{{url:todo}}?finished=1')">
{{/finished}}
{{#finished}}
<input type="checkbox" checked data-on:click__prevent="@patch('{{url:todo}}')">
{{/finished}}
{{title}}
<button data-on:click="@delete('{{url:todo:id}}')">delete</button>
</div>

View File

@@ -0,0 +1,19 @@
{{< layout}}
{{$head}}
{{> datastar }}
{{/head}}
{{$body}}
<input type="text" placeholder="new todo" data-bind:title
data-on:keydown="evt.key === 'Enter' && @post('{{url:todos}}') && ($title = '');"
>
<button data-on:click="@post('{{url:todos}}') && ($title = '')">add</button>
<div id="todos" data-init="@get('{{url:todos}}?http_method=sse')">
{{^todos}}
<p>no todos</p>
{{/todos}}
{{#todos}}
{{> todo_item}}
{{/todos}}
</div>
{{/body}}
{{/layout}}

View File

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