MaCH repo
This commit is contained in:
59
05_todo_sse_datastar/main.c
Normal file
59
05_todo_sse_datastar/main.c
Normal file
@@ -0,0 +1,59 @@
|
||||
#include <mach.h>
|
||||
#include <sqlite.h>
|
||||
#include <datastar.h>
|
||||
#include <session_auth.h>
|
||||
|
||||
void mach(){
|
||||
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")});
|
||||
|
||||
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)
|
||||
}
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user