MaCH repo
This commit is contained in:
2
06_todo_events/todos/create_todo.sql
Normal file
2
06_todo_events/todos/create_todo.sql
Normal file
@@ -0,0 +1,2 @@
|
||||
insert into todos(user_id, title)
|
||||
values({{user_id}}, {{title}});
|
||||
7
06_todo_events/todos/create_todos_table.sql
Normal file
7
06_todo_events/todos/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);
|
||||
3
06_todo_events/todos/delete_todo.sql
Normal file
3
06_todo_events/todos/delete_todo.sql
Normal file
@@ -0,0 +1,3 @@
|
||||
delete from todos
|
||||
where user_id = {{user_id}}
|
||||
and id = {{id}};
|
||||
3
06_todo_events/todos/get_todos.sql
Normal file
3
06_todo_events/todos/get_todos.sql
Normal file
@@ -0,0 +1,3 @@
|
||||
select id, title, finished
|
||||
from todos
|
||||
where user_id = {{user_id}};
|
||||
45
06_todo_events/todos/todos.c
Normal file
45
06_todo_events/todos/todos.c
Normal file
@@ -0,0 +1,45 @@
|
||||
#include <mach.h>
|
||||
#include <sqlite.h>
|
||||
#include <pubsub.h>
|
||||
#include <session_auth.h>
|
||||
|
||||
module(todos){
|
||||
middleware(logged_in());
|
||||
middleware(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")
|
||||
}
|
||||
);
|
||||
}
|
||||
30
06_todo_events/todos/todos.mustache.html
Normal file
30
06_todo_events/todos/todos.mustache.html
Normal 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}}
|
||||
4
06_todo_events/todos/update_todo.sql
Normal file
4
06_todo_events/todos/update_todo.sql
Normal file
@@ -0,0 +1,4 @@
|
||||
update todos
|
||||
set finished = {{finished}}
|
||||
where user_id = {{user_id}}
|
||||
and id = {{id}};
|
||||
Reference in New Issue
Block a user