Random tasks, more info

This commit is contained in:
Niklas Korz 2024-01-03 23:59:23 +01:00
parent 9187461ed4
commit f93ad89194
7 changed files with 288 additions and 100 deletions

21
pb_hooks/main.pb.js Normal file
View file

@ -0,0 +1,21 @@
routerAdd("GET", "/api/extras/random/:parent", (c) => {
const result = new DynamicModel({
"id": "",
"name": "",
});
const query = `WITH RECURSIVE descendants(id) AS (
VALUES({:parent}) UNION ALL
SELECT l.id FROM lists l JOIN descendants d ON d.id = l.parent
) SELECT t.id, t.name FROM tasks t JOIN descendants d ON t.list = d.id WHERE t.schedule <= DATE('now') ORDER BY RANDOM() LIMIT 1`;
const queryNull = `SELECT id, name FROM tasks WHERE schedule <= DATE('now') ORDER BY RANDOM() LIMIT 1`;
const parent = c.pathParam("parent");
$app.dao().db()
.newQuery(parent === "null" ? queryNull : query)
.bind({ parent })
.one(result)
return c.json(200, result)
})