MaCH repo
This commit is contained in:
5
04_todo/404.mustache.html
Normal file
5
04_todo/404.mustache.html
Normal file
@@ -0,0 +1,5 @@
|
||||
{{< layout}}
|
||||
{{$body}}
|
||||
<p>not found</p>
|
||||
{{/body}}
|
||||
{{/layout}}
|
||||
5
04_todo/5xx.mustache.html
Normal file
5
04_todo/5xx.mustache.html
Normal file
@@ -0,0 +1,5 @@
|
||||
{{< layout}}
|
||||
{{$body}}
|
||||
<p>error</p>
|
||||
{{/body}}
|
||||
{{/layout}}
|
||||
5
04_todo/about.mustache.html
Normal file
5
04_todo/about.mustache.html
Normal file
@@ -0,0 +1,5 @@
|
||||
{{< layout}}
|
||||
{{$body}}
|
||||
<p>about us</p>
|
||||
{{/body}}
|
||||
{{/layout}}
|
||||
5
04_todo/contact.mustache.html
Normal file
5
04_todo/contact.mustache.html
Normal file
@@ -0,0 +1,5 @@
|
||||
{{< layout}}
|
||||
{{$body}}
|
||||
<p>contact us</p>
|
||||
{{/body}}
|
||||
{{/layout}}
|
||||
2
04_todo/create_todo.sql
Normal file
2
04_todo/create_todo.sql
Normal file
@@ -0,0 +1,2 @@
|
||||
insert into todos(user_id, title)
|
||||
values({{user_id}}, {{title}});
|
||||
7
04_todo/create_todos_table.sql
Normal file
7
04_todo/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
04_todo/delete_todo.sql
Normal file
3
04_todo/delete_todo.sql
Normal file
@@ -0,0 +1,3 @@
|
||||
delete from todos
|
||||
where user_id = {{user_id}}
|
||||
and id = {{id}};
|
||||
3
04_todo/get_todos.sql
Normal file
3
04_todo/get_todos.sql
Normal file
@@ -0,0 +1,3 @@
|
||||
select id, title, finished
|
||||
from todos
|
||||
where user_id = {{user_id}};
|
||||
5
04_todo/home.mustache.html
Normal file
5
04_todo/home.mustache.html
Normal file
@@ -0,0 +1,5 @@
|
||||
{{< layout}}
|
||||
{{$body}}
|
||||
<p>home</p>
|
||||
{{/body}}
|
||||
{{/layout}}
|
||||
23
04_todo/layout.mustache.html
Normal file
23
04_todo/layout.mustache.html
Normal file
@@ -0,0 +1,23 @@
|
||||
<html>
|
||||
<head>
|
||||
<link rel='icon' href='{{asset:favicon.png}}'>
|
||||
</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>
|
||||
102
04_todo/main.c
Normal file
102
04_todo/main.c
Normal file
@@ -0,0 +1,102 @@
|
||||
#include <mach.h>
|
||||
#include <sqlite.h>
|
||||
#include <session_auth.h>
|
||||
|
||||
config mach(){
|
||||
return (config) {"main", {session()},
|
||||
.resources = {
|
||||
{"home", "/",
|
||||
.get = {render("home")}
|
||||
},
|
||||
|
||||
{"about", "/about",
|
||||
.get = {render("about")}
|
||||
},
|
||||
|
||||
{"contact", "/contact",
|
||||
.get = {render("contact")}
|
||||
},
|
||||
|
||||
{"todos", "/todos", {logged_in()},
|
||||
.get = {
|
||||
query({"get_todos", .set_key = "todos", .db = "todos_db"}),
|
||||
render("todos")
|
||||
},
|
||||
|
||||
.post = {
|
||||
validate({"title", .validation = validate_not_empty}),
|
||||
query({"create_todo", .db = "todos_db"}),
|
||||
redirect("todos")
|
||||
}
|
||||
},
|
||||
|
||||
{"todo", "/todos/:id", {
|
||||
logged_in(),
|
||||
validate({"id", .validation = validate_positive})},
|
||||
|
||||
.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")
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
.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"
|
||||
}},
|
||||
{"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:{{user_id}}_todo.db?mode=rwc",
|
||||
.migrations = {(asset){
|
||||
#embed "create_todos_table.sql"
|
||||
}}
|
||||
}},
|
||||
|
||||
.modules = {sqlite, session_auth}
|
||||
};
|
||||
}
|
||||
0
04_todo/public/favicon.png
Normal file
0
04_todo/public/favicon.png
Normal file
32
04_todo/todos.mustache.html
Normal file
32
04_todo/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
04_todo/update_todo.sql
Normal file
4
04_todo/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