MaCH repo
This commit is contained in:
5
05_todo_sse_datastar/404.mustache.html
Normal file
5
05_todo_sse_datastar/404.mustache.html
Normal file
@@ -0,0 +1,5 @@
|
||||
{{< layout}}
|
||||
{{$body}}
|
||||
<p>not found</p>
|
||||
{{/body}}
|
||||
{{/layout}}
|
||||
5
05_todo_sse_datastar/5xx.mustache.html
Normal file
5
05_todo_sse_datastar/5xx.mustache.html
Normal file
@@ -0,0 +1,5 @@
|
||||
{{< layout}}
|
||||
{{$body}}
|
||||
<p>error</p>
|
||||
{{/body}}
|
||||
{{/layout}}
|
||||
5
05_todo_sse_datastar/about.mustache.html
Normal file
5
05_todo_sse_datastar/about.mustache.html
Normal file
@@ -0,0 +1,5 @@
|
||||
{{< layout}}
|
||||
{{$body}}
|
||||
<p>about us</p>
|
||||
{{/body}}
|
||||
{{/layout}}
|
||||
5
05_todo_sse_datastar/contact.mustache.html
Normal file
5
05_todo_sse_datastar/contact.mustache.html
Normal file
@@ -0,0 +1,5 @@
|
||||
{{< layout}}
|
||||
{{$body}}
|
||||
<p>contact us</p>
|
||||
{{/body}}
|
||||
{{/layout}}
|
||||
3
05_todo_sse_datastar/create_todo.sql
Normal file
3
05_todo_sse_datastar/create_todo.sql
Normal file
@@ -0,0 +1,3 @@
|
||||
insert into todos(user_id, title)
|
||||
values({{user_id}}, {{title}})
|
||||
returning id, title, finished;
|
||||
7
05_todo_sse_datastar/create_todos_table.sql
Normal file
7
05_todo_sse_datastar/create_todos_table.sql
Normal 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);
|
||||
4
05_todo_sse_datastar/delete_todo.sql
Normal file
4
05_todo_sse_datastar/delete_todo.sql
Normal file
@@ -0,0 +1,4 @@
|
||||
delete from todos
|
||||
where user_id = {{user_id}}
|
||||
and id = {{id}}
|
||||
returning id;
|
||||
3
05_todo_sse_datastar/get_todos.sql
Normal file
3
05_todo_sse_datastar/get_todos.sql
Normal file
@@ -0,0 +1,3 @@
|
||||
select id, title, finished
|
||||
from todos
|
||||
where user_id = {{user_id}};
|
||||
5
05_todo_sse_datastar/home.mustache.html
Normal file
5
05_todo_sse_datastar/home.mustache.html
Normal file
@@ -0,0 +1,5 @@
|
||||
{{< layout}}
|
||||
{{$body}}
|
||||
<p>home</p>
|
||||
{{/body}}
|
||||
{{/layout}}
|
||||
25
05_todo_sse_datastar/layout.mustache.html
Normal file
25
05_todo_sse_datastar/layout.mustache.html
Normal 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>
|
||||
151
05_todo_sse_datastar/main.c
Normal file
151
05_todo_sse_datastar/main.c
Normal file
@@ -0,0 +1,151 @@
|
||||
#include <mach.h>
|
||||
#include <sqlite.h>
|
||||
#include <datastar.h>
|
||||
#include <session_auth.h>
|
||||
|
||||
config mach(){
|
||||
return (config) {
|
||||
.resources = {
|
||||
{"home", "/", {session()},
|
||||
.get = {
|
||||
render("home")
|
||||
}
|
||||
},
|
||||
|
||||
{"about", "/about", {session()},
|
||||
.get = {
|
||||
render("about")
|
||||
}
|
||||
},
|
||||
|
||||
{"contact", "/contact", {session()},
|
||||
.get = {
|
||||
render("contact")
|
||||
}
|
||||
},
|
||||
|
||||
{"todos", "/todos", {logged_in()},
|
||||
.sse = {"todos:{{user_id}}"},
|
||||
|
||||
.get = {
|
||||
query({"get_todos",
|
||||
.set_key = "todos",
|
||||
.db = "todos_db"
|
||||
}),
|
||||
render("todos")
|
||||
},
|
||||
|
||||
.post = {
|
||||
validate({"title",
|
||||
.validation = "^\\S{1,16}$",
|
||||
.message = "must be 1-16 characters, no spaces"
|
||||
}),
|
||||
query({"create_todo",
|
||||
.set_key = "todo",
|
||||
.db = "todos_db"
|
||||
}),
|
||||
ds_sse("todos:{{user_id}}",
|
||||
.target = "todos",
|
||||
.mode = mode_prepend,
|
||||
.elements = {"todo"}
|
||||
)
|
||||
}
|
||||
},
|
||||
|
||||
{"todo", "/todos/:id", {
|
||||
logged_in(),
|
||||
validate({"id",
|
||||
.validation = "^\\d{1,10}$",
|
||||
.message = "must be between 1-9999999999"
|
||||
})},
|
||||
|
||||
.patch = {
|
||||
validate({"finished",
|
||||
.optional = true,
|
||||
.validation = "1",
|
||||
.message = "must be 1"
|
||||
}),
|
||||
find({"update_todo",
|
||||
.set_key = "todo",
|
||||
.db = "todos_db"
|
||||
}),
|
||||
ds_sse("todos:{{user_id}}",
|
||||
.target = "todo_{{id}}",
|
||||
.mode = mode_replace,
|
||||
.elements = {"todo"}
|
||||
)
|
||||
},
|
||||
|
||||
.delete = {
|
||||
find({"delete_todo",
|
||||
.db = "todos_db"
|
||||
}),
|
||||
ds_sse("todos:{{user_id}}",
|
||||
.target = "todo_{{id}}",
|
||||
.mode = mode_remove
|
||||
)
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
.errors = {
|
||||
{http_error, {
|
||||
render("5xx")
|
||||
}},
|
||||
|
||||
{http_not_found, {
|
||||
render("404")
|
||||
}}
|
||||
},
|
||||
|
||||
.context = {
|
||||
{"layout", (asset){
|
||||
#embed "layout.mustache.html"
|
||||
}},
|
||||
{"home", (asset){
|
||||
#embed "home.mustache.html"
|
||||
}},
|
||||
{"about", (asset){
|
||||
#embed "about.mustache.html"
|
||||
}},
|
||||
{"contact", (asset){
|
||||
#embed "contact.mustache.html"
|
||||
}},
|
||||
{"5xx", (asset){
|
||||
#embed "5xx.mustache.html"
|
||||
}},
|
||||
{"404", (asset){
|
||||
#embed "404.mustache.html"
|
||||
}},
|
||||
{"todos", (asset){
|
||||
#embed "todos.mustache.html"
|
||||
}},
|
||||
{"todo", (asset){
|
||||
#embed "todo.mustache.html"
|
||||
}},
|
||||
{"get_todos", (asset){
|
||||
#embed "get_todos.sql"
|
||||
}},
|
||||
{"create_todo", (asset){
|
||||
#embed "create_todo.sql"
|
||||
}},
|
||||
{"update_todo", (asset){
|
||||
#embed "update_todo.sql"
|
||||
}},
|
||||
{"delete_todo", (asset){
|
||||
#embed "delete_todo.sql"
|
||||
}}
|
||||
},
|
||||
|
||||
.databases = {{
|
||||
.engine = sqlite_db,
|
||||
.name = "todos_db",
|
||||
.connect = "file:todo.db?mode=rwc",
|
||||
.migrations = {(asset){
|
||||
#embed "create_todos_table.sql"
|
||||
}}
|
||||
}},
|
||||
|
||||
.modules = {sqlite, datastar, session_auth}
|
||||
};
|
||||
}
|
||||
0
05_todo_sse_datastar/public/favicon.png
Normal file
0
05_todo_sse_datastar/public/favicon.png
Normal file
10
05_todo_sse_datastar/todo.mustache.html
Normal file
10
05_todo_sse_datastar/todo.mustache.html
Normal file
@@ -0,0 +1,10 @@
|
||||
<div id="todo_{{id}}">
|
||||
{{^finished}}
|
||||
<input type="checkbox" data-on:click__prevent="@patch('{{url:todo:id}}?finished=1')">
|
||||
{{/finished}}
|
||||
{{#finished}}
|
||||
<input type="checkbox" checked data-on:click__prevent="@patch('{{url:todo:id}}')">
|
||||
{{/finished}}
|
||||
{{title}}
|
||||
<button data-on:click="@delete('{{url:todo:id}}')">delete</button>
|
||||
</div>
|
||||
21
05_todo_sse_datastar/todos.mustache.html
Normal file
21
05_todo_sse_datastar/todos.mustache.html
Normal file
@@ -0,0 +1,21 @@
|
||||
{{< layout}}
|
||||
{{$head}}
|
||||
{{> datastar_script }}
|
||||
{{/head}}
|
||||
{{$body}}
|
||||
<input type="text" placeholder="new todo" data-bind:title
|
||||
data-on:keydown="evt.key === 'Enter' && @post('{{utl: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}}
|
||||
{{/.}}
|
||||
{{/todos}}
|
||||
</div>
|
||||
{{/body}}
|
||||
{{/layout}}
|
||||
5
05_todo_sse_datastar/update_todo.sql
Normal file
5
05_todo_sse_datastar/update_todo.sql
Normal file
@@ -0,0 +1,5 @@
|
||||
update todos
|
||||
set finished = {{finished}}
|
||||
where user_id = {{user_id}}
|
||||
and id = {{id}}
|
||||
returning id, title, finished;
|
||||
Reference in New Issue
Block a user