nerack repo

This commit is contained in:
2026-09-13 15:31:00 -05:00
committed by nightshade
commit abac587d14
99 changed files with 4772 additions and 0 deletions
@@ -0,0 +1,2 @@
insert into todos(user_id, title)
values({{user_id}}, {{title}});
@@ -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);
@@ -0,0 +1,3 @@
delete from todos
where user_id = {{user_id}}
and id = {{id}};
@@ -0,0 +1,3 @@
select id, title, finished
from todos
where user_id = {{user_id}};
+52
View File
@@ -0,0 +1,52 @@
#include <nerack.h>
#include <http.h>
#include <sqlite.h>
#include <pubsub.h>
#include <cookie_auth.h>
module(todos){
sqlite(
"todos_db",
"file:todo.db?mode=rwc",
{"create_todos_table"}
);
publish("todo_created",
.with = {"user_id", "title"}
);
http("todos", "/todos",
.all = {
cookie_logged_in(),
cookie_session()
},
.get = {
sqlite_query({"todos_db", "get_todos", "todos_data"}),
html("todos", "todos_s"),
http_response("todos_s")
},
.post = {
input({"title", not_empty_input}),
sqlite_query({"todos_db", "create_todo"}),
emit("todo_created"),
http_redirect("todos")
}
);
http("todo", "/todos/:id",
.all = {
cookie_logged_in(),
cookie_session(),
input({"id", positive_integer_input})
},
.patch = {
input({"finished", "^1$", "must be 1", .optional = true}),
sqlite_query({"todos_db", "update_todo"}),
http_redirect("todos")
},
.delete = {
sqlite_query({"todos_db", "delete_todo"}),
http_redirect("todos")
}
);
}
@@ -0,0 +1,28 @@
{{< layout}}
{{$body}}
<form action="{{url:post: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:patch:todo}}" method="post" style="display:inline">
{{^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:delete:todo}}" method="post" style="display:inline">
<button type="submit">delete</button>
</form>
</div>
{{/todos_data}}
{{/body}}
{{/layout}}
@@ -0,0 +1,4 @@
update todos
set finished = {{finished}}
where user_id = {{user_id}}
and id = {{id}};