MaCH repo
This commit is contained in:
105
04_todo/main.c
Normal file
105
04_todo/main.c
Normal file
@@ -0,0 +1,105 @@
|
||||
#include <mach.h>
|
||||
#include <sqlite.h>
|
||||
#include <session_auth.h>
|
||||
|
||||
string const create_todos_table = (asset){
|
||||
#embed "create_todos_table.sql"
|
||||
};
|
||||
string const get_todos = (asset){
|
||||
#embed "get_todos.sql"
|
||||
};
|
||||
string create_todo = (asset){
|
||||
#embed "create_todo.sql"
|
||||
};
|
||||
string const update_todo = (asset){
|
||||
#embed "update_todo.sql"
|
||||
};
|
||||
string const delete_todo = (asset){
|
||||
#embed "delete_todo.sql"
|
||||
};
|
||||
string const layout = (asset){
|
||||
#embed "layout.mustache.html"
|
||||
};
|
||||
string const home = (asset){
|
||||
#embed "home.mustache.html"
|
||||
};
|
||||
string const about = (asset){
|
||||
#embed "about.mustache.html"
|
||||
};
|
||||
string const contact = (asset){
|
||||
#embed "contact.mustache.html"
|
||||
};
|
||||
string const todos = (asset){
|
||||
#embed "todos.mustache.html"
|
||||
};
|
||||
string const e5xx = (asset){
|
||||
#embed "5xx.mustache.html"
|
||||
};
|
||||
string const e404 = (asset){
|
||||
#embed "404.mustache.html"
|
||||
};
|
||||
|
||||
void mach(){
|
||||
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()},
|
||||
.get = {
|
||||
sqlite_query({"todos_db", get_todos, "todos_data"}),
|
||||
mustache("todos")
|
||||
},
|
||||
.post = {
|
||||
input({"title", m_not_empty}),
|
||||
sqlite_query({"todos_db", create_todo}),
|
||||
redirect("todos")
|
||||
}
|
||||
);
|
||||
|
||||
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}),
|
||||
redirect("todos")
|
||||
},
|
||||
.delete = {
|
||||
sqlite_query({"todos_db", delete_todo}),
|
||||
redirect("todos")
|
||||
}
|
||||
);
|
||||
|
||||
sqlite_database(
|
||||
.name ="todos_db",
|
||||
.connect = "file:{{user_id}}_todo.db?mode=rwc",
|
||||
.migrations = {create_todos_table}
|
||||
);
|
||||
|
||||
error(m_error, {mustache("5xx")});
|
||||
error(m_not_found, {mustache("404")});
|
||||
|
||||
context("layout", layout);
|
||||
context("home", home);
|
||||
context("about", about);
|
||||
context("contact", contact);
|
||||
context("todos", todos);
|
||||
context("5xx", e5xx);
|
||||
context("404", e404);
|
||||
|
||||
module(sqlite);
|
||||
module(session_auth);
|
||||
}
|
||||
Reference in New Issue
Block a user