MaCH repo
This commit is contained in:
72
06_todo_events/todos/todos.c
Normal file
72
06_todo_events/todos/todos.c
Normal file
@@ -0,0 +1,72 @@
|
||||
#include <mach.h>
|
||||
#include <sqlite.h>
|
||||
#include <session_auth.h>
|
||||
|
||||
config todos(){
|
||||
return (config) {"todos",
|
||||
.resources = {
|
||||
{"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"}),
|
||||
emit("todo_created"),
|
||||
redirect("todos")
|
||||
}
|
||||
},
|
||||
|
||||
{"todo", "/todos/:id", {
|
||||
logged_in(),
|
||||
validate({"id", .validation = validate_positive})},
|
||||
|
||||
.patch = {
|
||||
validate({"finished", .optional = true, .validation = "1", .message = "must be 1"}),
|
||||
query({"update_todo", .db = "todos_db"}),
|
||||
redirect("todos")
|
||||
},
|
||||
|
||||
.delete = {
|
||||
query({"delete_todo", .db = "todos_db"}),
|
||||
redirect("todos")
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
.publishes = {
|
||||
{"todo_created", .with = {"user_id", "title"}}
|
||||
},
|
||||
|
||||
.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"
|
||||
}}
|
||||
},
|
||||
|
||||
.databases = {{
|
||||
.engine = sqlite_db,
|
||||
.name = "todos_db",
|
||||
.connect = "file:todo.db?mode=rwc",
|
||||
.migrations = {(asset){
|
||||
#embed "create_todos_table.sql"
|
||||
}}
|
||||
}},
|
||||
|
||||
.modules = {sqlite, session_auth}
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user