MaCH repo
This commit is contained in:
52
06_todo_events/activity/activity.c
Normal file
52
06_todo_events/activity/activity.c
Normal file
@@ -0,0 +1,52 @@
|
||||
#include <mach.h>
|
||||
#include <sqlite.h>
|
||||
#include <session_auth.h>
|
||||
|
||||
config activity(){
|
||||
return (config) {
|
||||
.name = "activity",
|
||||
|
||||
.resources = {
|
||||
{"activity", "/activity", {logged_in()},
|
||||
.get = {
|
||||
query({"get_activities",
|
||||
.set_key = "activity",
|
||||
.db = "activity_db"
|
||||
}),
|
||||
render("activity")
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
.context = {
|
||||
{"activity", (asset){
|
||||
#embed "activity.mustache.html"
|
||||
}},
|
||||
{"get_activities", (asset){
|
||||
#embed "get_activities.sql"
|
||||
}},
|
||||
{"insert_activity", (asset){
|
||||
#embed "insert_activity.sql"
|
||||
}}
|
||||
},
|
||||
|
||||
.events = {
|
||||
{"todo_created", {
|
||||
query({"insert_activity",
|
||||
.db = "activity_db"
|
||||
})
|
||||
}}
|
||||
},
|
||||
|
||||
.databases = {{
|
||||
.name = "activity_db",
|
||||
.engine = sqlite_db,
|
||||
.connect = "file:activity.db?mode=rwc",
|
||||
.migrations = {(asset){
|
||||
#embed "create_activity_table.sql"
|
||||
}}
|
||||
}},
|
||||
|
||||
.modules = {sqlite, session_auth}
|
||||
};
|
||||
}
|
||||
13
06_todo_events/activity/activity.mustache.html
Normal file
13
06_todo_events/activity/activity.mustache.html
Normal 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}}
|
||||
8
06_todo_events/activity/create_activity_table.sql
Normal file
8
06_todo_events/activity/create_activity_table.sql
Normal 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);
|
||||
5
06_todo_events/activity/get_activities.sql
Normal file
5
06_todo_events/activity/get_activities.sql
Normal file
@@ -0,0 +1,5 @@
|
||||
select action, title, created_at
|
||||
from activity
|
||||
where user_id = {{user_id}}
|
||||
order by created_at desc
|
||||
limit 50;
|
||||
2
06_todo_events/activity/insert_activity.sql
Normal file
2
06_todo_events/activity/insert_activity.sql
Normal file
@@ -0,0 +1,2 @@
|
||||
insert into activity(user_id, action, title)
|
||||
values({{user_id}}, 'created', {{title}});
|
||||
43
06_todo_events/main.c
Normal file
43
06_todo_events/main.c
Normal file
@@ -0,0 +1,43 @@
|
||||
#include <mach.h>
|
||||
#include <session_auth.h>
|
||||
#include "todos/todos.c"
|
||||
#include "activity/activity.c"
|
||||
|
||||
config mach(){
|
||||
return (config) {
|
||||
.resources = {
|
||||
{"home", "/", {session()},
|
||||
.get = {
|
||||
render("home")
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
.errors = {
|
||||
{http_error, {
|
||||
render("5xx")
|
||||
}},
|
||||
|
||||
{http_not_found, {
|
||||
render("404")
|
||||
}}
|
||||
},
|
||||
|
||||
.context = {
|
||||
{"layout", (asset){
|
||||
#embed "static/layout.mustache.html"
|
||||
}},
|
||||
{"home", (asset){
|
||||
#embed "static/home.mustache.html"
|
||||
}},
|
||||
{"5xx", (asset){
|
||||
#embed "static/5xx.mustache.html"
|
||||
}},
|
||||
{"404", (asset){
|
||||
#embed "static/404.mustache.html"
|
||||
}}
|
||||
},
|
||||
|
||||
.modules = {todos, activity, session_auth}
|
||||
};
|
||||
}
|
||||
0
06_todo_events/public/favicon.png
Normal file
0
06_todo_events/public/favicon.png
Normal file
5
06_todo_events/static/404.mustache.html
Normal file
5
06_todo_events/static/404.mustache.html
Normal file
@@ -0,0 +1,5 @@
|
||||
{{< layout}}
|
||||
{{$body}}
|
||||
<p>not found</p>
|
||||
{{/body}}
|
||||
{{/layout}};
|
||||
5
06_todo_events/static/5xx.mustache.html
Normal file
5
06_todo_events/static/5xx.mustache.html
Normal file
@@ -0,0 +1,5 @@
|
||||
{{< layout}}
|
||||
{{$body}}
|
||||
<p>error</p>
|
||||
{{/body}}
|
||||
{{/layout}};
|
||||
5
06_todo_events/static/home.mustache.html
Normal file
5
06_todo_events/static/home.mustache.html
Normal file
@@ -0,0 +1,5 @@
|
||||
{{< layout}}
|
||||
{{$body}}
|
||||
<p>home</p>
|
||||
{{/body}}
|
||||
{{/layout}};
|
||||
20
06_todo_events/static/layout.mustache.html
Normal file
20
06_todo_events/static/layout.mustache.html
Normal file
@@ -0,0 +1,20 @@
|
||||
<html>
|
||||
<head>
|
||||
<link rel='icon' href='/favicon.png'>
|
||||
</head>
|
||||
<body>
|
||||
<p>
|
||||
{{^user}}
|
||||
<a href='/login'>sign in</a>
|
||||
{{/user}}
|
||||
{{#user}}
|
||||
welcome, {{short_name}}
|
||||
{{/user}}
|
||||
</p>
|
||||
<nav>
|
||||
<a href='/'>home</a>
|
||||
</nav>
|
||||
{{$body}}
|
||||
{{/body}}
|
||||
</body>
|
||||
</html>
|
||||
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}};
|
||||
98
06_todo_events/todos/todos.c
Normal file
98
06_todo_events/todos/todos.c
Normal file
@@ -0,0 +1,98 @@
|
||||
#include <mach.h>
|
||||
#include <sqlite.h>
|
||||
#include <session_auth.h>
|
||||
|
||||
config todos(){
|
||||
return (config) {
|
||||
.name = "todos",
|
||||
|
||||
.resources = {
|
||||
{"todos", "/todos", {logged_in()},
|
||||
.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",
|
||||
.db = "todos_db"
|
||||
}),
|
||||
emit("todo_created"),
|
||||
redirect("todos")
|
||||
}
|
||||
},
|
||||
|
||||
{"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",
|
||||
.db = "todos_db"
|
||||
}),
|
||||
redirect("todos")
|
||||
},
|
||||
|
||||
.delete = {
|
||||
find({"delete_todo",
|
||||
.db = "todos_db"
|
||||
}),
|
||||
redirect("todos")
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
.context = {
|
||||
{"todos", (asset){
|
||||
#embed "todos.mustache.html"
|
||||
}},
|
||||
{"get_todos", (asset){
|
||||
#embed "get_todos.sql"
|
||||
}},
|
||||
{"create_todos", (asset){
|
||||
#embed "create_todo.sql"
|
||||
}},
|
||||
{"update_todo", (asset){
|
||||
#embed "update_todo.sql"
|
||||
}},
|
||||
{"delete_todo", (asset){
|
||||
#embed "delete_todo.sql"
|
||||
}}
|
||||
},
|
||||
|
||||
.publishes = {
|
||||
{"todo_created",
|
||||
.with = {
|
||||
"user_id",
|
||||
"title"
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
.databases = {{
|
||||
.engine = sqlite_db,
|
||||
.name = "todos_db",
|
||||
.connect = "file:todo.db?mode=rwc",
|
||||
.migrations = {(asset){
|
||||
#embed "create_todos_table.sql"
|
||||
}}
|
||||
}},
|
||||
|
||||
.modules = {sqlite, session_auth}
|
||||
};
|
||||
}
|
||||
32
06_todo_events/todos/todos.mustache.html
Normal file
32
06_todo_events/todos/todos.mustache.html
Normal file
@@ -0,0 +1,32 @@
|
||||
{{< layout}}
|
||||
{{$body}}
|
||||
<form action="{{url:todos}}" method="post">
|
||||
<input type="text" name="title" placeholder="new todo" required>
|
||||
<button type="submit">add</button>
|
||||
</form>
|
||||
{{^todos}}
|
||||
<p>no todos</p>
|
||||
{{/todos}}
|
||||
{{#todos}}
|
||||
{{#.}}
|
||||
<div>
|
||||
<form action="{{url:todo:id}}" 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:id}}" method="post" style="display:inline">
|
||||
<input type="hidden" name="http_method" value="delete">
|
||||
<button type="submit">delete</button>
|
||||
</form>
|
||||
</div>
|
||||
{{/.}}
|
||||
{{/todos}}
|
||||
{{/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