MaCH repo

This commit is contained in:
2025-07-24 12:46:01 -05:00
committed by Nick Ricketts
commit 608bd9f36e
77 changed files with 4666 additions and 0 deletions

View File

@@ -0,0 +1,25 @@
#include <mach.h>
#include <sqlite.h>
#include <pubsub.h>
#include <session_auth.h>
mach(activity){
middleware(logged_in(), session());
sqlite_database(
.name = "activity_db",
.connect = "file:activity.db?mode=rwc",
.migrations = {"create_activity_table"}
);
subscribe("todo_created", {
sqlite_query({"activity_db", "insert_activity"})
});
resource("activity", "/activity",
.get = {
sqlite_query({"activity_db", "get_activities", "activity"}),
mustache("activity")
}
);
}

View File

@@ -0,0 +1,13 @@
{{< layout}}
{{$body}}
{{^activity}}
<p>no activity</p>
{{/activity}}
{{#activity}}
<p>activity</p>
{{#.}}
<p>{{action}}: {{title}} ({{created_at}})</p>
{{/.}}
{{/activity}}
{{/body}}
{{/layout}}

View File

@@ -0,0 +1,8 @@
CREATE TABLE IF NOT EXISTS activity (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER NOT NULL,
action TEXT NOT NULL,
title TEXT NOT NULL,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX IF NOT EXISTS idx_activity_user_id ON activity(user_id);

View File

@@ -0,0 +1,5 @@
select action, title, created_at
from activity
where user_id = {{user_id}}
order by created_at desc
limit 50;

View File

@@ -0,0 +1,2 @@
insert into activity(user_id, action, title)
values({{user_id}}, 'created', {{title}});

12
06_todo_events/main.c Normal file
View File

@@ -0,0 +1,12 @@
#include <mach.h>
#include <session_auth.h>
#include "todos/todos.c"
#include "activity/activity.c"
mach(main){
middleware(session());
resource("home", "/",
.get = {mustache("home")}
);
}

View File

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>home</p>
{{/body}}
{{/layout}};

View File

@@ -0,0 +1,20 @@
<html>
<head>
<link rel='icon' href='{{asset:favicon.png}}'>
</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>
</nav>
{{$body}}
{{/body}}
</body>
</html>

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,44 @@
#include <mach.h>
#include <sqlite.h>
#include <pubsub.h>
#include <session_auth.h>
mach(todos){
middleware(logged_in(), session());
sqlite_database(
.name = "todos_db",
.connect = "file:todo.db?mode=rwc",
.migrations = {"create_todos_table"}
);
publish("todo_created",
.with = {"user_id", "title"}
);
resource("todos", "/todos",
.get = {
sqlite_query({"todos_db", "get_todos", "todos_data"}),
mustache("todos")
},
.post = {
input({"title", m_not_empty}),
sqlite_query({"todos_db", "create_todo"}),
emit("todo_created"),
redirect("todos")
}
);
resource("todo", "/todos/:id",
.all = {input({"id", m_positive})},
.patch = {
input({"finished", "1", "must be 1", .optional = true}),
sqlite_query({"todos_db", "update_todo"}),
redirect("todos")
},
.delete = {
sqlite_query({"todos_db", "delete_todo"}),
redirect("todos")
}
);
}

View File

@@ -0,0 +1,30 @@
{{< layout}}
{{$body}}
<form action="{{url:todos}}" method="post">
<input type="text" name="title" placeholder="new todo" required>
<button type="submit">add</button>
</form>
{{^todos_data}}
<p>no todos</p>
{{/todos_data}}
{{#todos_data}}
<div>
<form action="{{url:todo}}" 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="{{url:todo}}" method="post" style="display:inline">
<input type="hidden" name="http_method" value="delete">
<button type="submit">delete</button>
</form>
</div>
{{/todos_data}}
{{/body}}
{{/layout}}

View File

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