MaCH repo
This commit is contained in:
45
06_todo_events/activity/activity.c
Normal file
45
06_todo_events/activity/activity.c
Normal file
@@ -0,0 +1,45 @@
|
||||
#include <mach.h>
|
||||
#include <sqlite.h>
|
||||
#include <session_auth.h>
|
||||
|
||||
config activity(){
|
||||
return (config) {"activity",
|
||||
.resources = {
|
||||
{"activity", "/activity", {logged_in()},
|
||||
.get = {
|
||||
query({"get_activities", .set_key = "activity", .db = "activity_db"}),
|
||||
render("activity")
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
.events = {
|
||||
{"todo_created", {
|
||||
query({"insert_activity", .db = "activity_db"})
|
||||
}}
|
||||
},
|
||||
|
||||
.context = {
|
||||
{"activity", (asset){
|
||||
#embed "activity.mustache.html"
|
||||
}},
|
||||
{"get_activities", (asset){
|
||||
#embed "get_activities.sql"
|
||||
}},
|
||||
{"insert_activity", (asset){
|
||||
#embed "insert_activity.sql"
|
||||
}}
|
||||
},
|
||||
|
||||
.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}});
|
||||
Reference in New Issue
Block a user