Static dir configuration and tree view of notes

This commit is contained in:
Magnus Åhall 2023-06-19 15:02:52 +02:00
parent 99a15aa567
commit cb9d95bcb2
6 changed files with 125 additions and 3 deletions

66
node.go
View file

@ -5,7 +5,7 @@ import (
"github.com/jmoiron/sqlx"
// Standard
"time"
)
type Node struct {
@ -14,11 +14,75 @@ type Node struct {
ParentID int `db:"parent_id"`
Name string
Content string
Updated time.Time
Children []Node
Crumbs []Node
Complete bool
Level int
}
func (session Session) NodeTree(startNodeID int) (nodes []Node, err error) {// {{{
var rows *sqlx.Rows
rows, err = db.Queryx(`
WITH RECURSIVE nodetree AS (
SELECT
*,
array[name::text] AS path,
0 AS level
FROM node
WHERE
user_id = $1 AND
CASE $2::int
WHEN 0 THEN parent_id IS NULL
ELSE parent_id = $2
END
UNION ALL
SELECT
n.*,
path||n.name::text AS path,
nt.level + 1 AS level
FROM node n
INNER JOIN nodetree nt ON n.parent_id = nt.id
)
SELECT
id,
user_id,
COALESCE(parent_id, 0) AS parent_id,
name,
updated,
level
FROM nodetree
ORDER BY
path ASC
`,
session.UserID,
startNodeID,
)
if err != nil {
return
}
defer rows.Close()
type resultRow struct {
Node
Level int
}
nodes = []Node{}
for rows.Next() {
node := Node{}
node.Complete = false
if err = rows.StructScan(&node); err != nil {
return
}
nodes = append(nodes, node)
}
return
}// }}}
func (session Session) RootNode() (node Node, err error) {// {{{
var rows *sqlx.Rows
rows, err = db.Queryx(`