Notes/node.go

426 lines
7.2 KiB
Go
Raw Normal View History

2023-06-17 09:11:14 +02:00
package main
import (
// External
"github.com/jmoiron/sqlx"
// Standard
"time"
2023-06-17 09:11:14 +02:00
)
type Node struct {
ID int
UserID int `db:"user_id"`
ParentID int `db:"parent_id"`
2023-07-12 22:35:38 +02:00
CryptoKeyID int `db:"crypto_key_id"`
2023-06-17 09:11:14 +02:00
Name string
Content string
Updated time.Time
2023-06-17 09:11:14 +02:00
Children []Node
Crumbs []Node
2023-06-22 08:28:51 +02:00
Files []File
2023-06-17 09:11:14 +02:00
Complete bool
Level int
ContentEncrypted string `db:"content_encrypted" json:"-"`
2024-01-09 16:28:40 +01:00
Markdown bool
2023-06-17 09:11:14 +02:00
}
2024-01-09 16:28:40 +01:00
func NodeTree(userID, startNodeID int) (nodes []Node, err error) { // {{{
var rows *sqlx.Rows
2024-01-05 20:00:02 +01:00
rows, err = service.Db.Conn.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
`,
2024-01-05 20:00:02 +01:00
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
2024-01-05 21:14:55 +01:00
node.Crumbs = []Node{}
node.Children = []Node{}
node.Files = []File{}
if err = rows.StructScan(&node); err != nil {
return
}
nodes = append(nodes, node)
}
return
2024-01-09 16:28:40 +01:00
} // }}}
func RootNode(userID int) (node Node, err error) { // {{{
2023-06-17 09:11:14 +02:00
var rows *sqlx.Rows
2024-01-05 20:00:02 +01:00
rows, err = service.Db.Conn.Queryx(`
2023-06-17 09:11:14 +02:00
SELECT
id,
user_id,
0 AS parent_id,
name
FROM node
WHERE
user_id = $1 AND
parent_id IS NULL
`,
2024-01-05 20:00:02 +01:00
userID,
2023-06-17 09:11:14 +02:00
)
if err != nil {
return
}
defer rows.Close()
node.Name = "Start"
2024-01-05 20:00:02 +01:00
node.UserID = userID
2023-06-17 09:11:14 +02:00
node.Complete = true
node.Children = []Node{}
2024-01-09 16:28:40 +01:00
node.Crumbs = []Node{}
node.Files = []File{}
2023-06-17 09:11:14 +02:00
for rows.Next() {
row := Node{}
if err = rows.StructScan(&row); err != nil {
return
}
node.Children = append(node.Children, Node{
ID: row.ID,
UserID: row.UserID,
ParentID: row.ParentID,
Name: row.Name,
})
}
return
2024-01-09 16:28:40 +01:00
} // }}}
func RetrieveNode(userID, nodeID int) (node Node, err error) { // {{{
2023-06-17 09:11:14 +02:00
if nodeID == 0 {
2024-01-05 20:00:02 +01:00
return RootNode(userID)
2023-06-17 09:11:14 +02:00
}
var rows *sqlx.Rows
2024-01-05 20:00:02 +01:00
rows, err = service.Db.Conn.Queryx(`
2023-06-17 09:11:14 +02:00
WITH RECURSIVE recurse AS (
SELECT
id,
user_id,
COALESCE(parent_id, 0) AS parent_id,
2023-07-12 22:35:38 +02:00
COALESCE(crypto_key_id, 0) AS crypto_key_id,
2023-06-17 09:11:14 +02:00
name,
content,
content_encrypted,
2024-01-09 16:28:40 +01:00
markdown,
2023-06-17 09:11:14 +02:00
0 AS level
FROM node
WHERE
user_id = $1 AND
id = $2
UNION
SELECT
n.id,
n.user_id,
n.parent_id,
2023-07-12 22:35:38 +02:00
COALESCE(n.crypto_key_id, 0) AS crypto_key_id,
2023-06-17 09:11:14 +02:00
n.name,
'' AS content,
'' AS content_encrypted,
2024-01-09 16:28:40 +01:00
false AS markdown,
2023-06-17 09:11:14 +02:00
r.level + 1 AS level
FROM node n
INNER JOIN recurse r ON n.parent_id = r.id AND r.level = 0
WHERE
n.user_id = $1
)
SELECT * FROM recurse ORDER BY level ASC
`,
2024-01-05 20:00:02 +01:00
userID,
2023-06-17 09:11:14 +02:00
nodeID,
)
if err != nil {
return
}
defer rows.Close()
type resultRow struct {
Node
Level int
}
2023-06-18 20:13:35 +02:00
node = Node{}
node.Children = []Node{}
2023-06-17 09:11:14 +02:00
for rows.Next() {
row := resultRow{}
if err = rows.StructScan(&row); err != nil {
return
}
if row.Level == 0 {
2024-01-09 16:28:40 +01:00
node.ID = row.ID
node.UserID = row.UserID
node.ParentID = row.ParentID
2023-07-12 22:35:38 +02:00
node.CryptoKeyID = row.CryptoKeyID
2024-01-09 16:28:40 +01:00
node.Name = row.Name
node.Complete = true
node.Markdown = row.Markdown
if node.CryptoKeyID > 0 {
node.Content = row.ContentEncrypted
} else {
node.Content = row.Content
}
2023-06-17 09:11:14 +02:00
}
if row.Level == 1 {
node.Children = append(node.Children, Node{
2023-07-12 22:35:38 +02:00
ID: row.ID,
UserID: row.UserID,
ParentID: row.ParentID,
CryptoKeyID: row.CryptoKeyID,
Name: row.Name,
2023-06-17 09:11:14 +02:00
})
}
}
2024-01-05 20:00:02 +01:00
node.Crumbs, err = NodeCrumbs(node.ID)
node.Files, err = Files(userID, node.ID, 0)
2023-06-17 09:11:14 +02:00
return
2024-01-09 16:28:40 +01:00
} // }}}
func NodeCrumbs(nodeID int) (nodes []Node, err error) { // {{{
2023-06-17 09:11:14 +02:00
var rows *sqlx.Rows
2024-01-05 20:00:02 +01:00
rows, err = service.Db.Conn.Queryx(`
2023-06-17 09:11:14 +02:00
WITH RECURSIVE nodes AS (
SELECT
id,
COALESCE(parent_id, 0) AS parent_id,
name
FROM node
WHERE
id = $1
UNION
SELECT
n.id,
COALESCE(n.parent_id, 0) AS parent_id,
n.name
FROM node n
INNER JOIN nodes nr ON n.id = nr.parent_id
)
SELECT * FROM nodes
`, nodeID)
if err != nil {
return
}
defer rows.Close()
2024-01-09 16:28:40 +01:00
2023-06-17 09:11:14 +02:00
nodes = []Node{}
for rows.Next() {
node := Node{}
if err = rows.StructScan(&node); err != nil {
return
}
nodes = append(nodes, node)
}
return
2024-01-09 16:28:40 +01:00
} // }}}
func CreateNode(userID, parentID int, name string) (node Node, err error) { // {{{
2023-06-18 20:13:35 +02:00
var rows *sqlx.Rows
2024-01-05 20:00:02 +01:00
rows, err = service.Db.Conn.Queryx(`
2023-06-18 20:13:35 +02:00
INSERT INTO node(user_id, parent_id, name)
VALUES($1, NULLIF($2, 0)::integer, $3)
RETURNING
id,
user_id,
COALESCE(parent_id, 0) AS parent_id,
name,
content
`,
2024-01-05 20:00:02 +01:00
userID,
2023-06-18 20:13:35 +02:00
parentID,
name,
)
if err != nil {
return
}
defer rows.Close()
for rows.Next() {
node = Node{}
if err = rows.StructScan(&node); err != nil {
return
}
node.Children = []Node{}
2023-06-22 08:28:51 +02:00
node.Files = []File{}
2023-06-18 20:13:35 +02:00
node.Complete = true
}
2024-01-05 20:00:02 +01:00
node.Crumbs, err = NodeCrumbs(node.ID)
2023-06-18 20:13:35 +02:00
return
2024-01-09 16:28:40 +01:00
} // }}}
func UpdateNode(userID, nodeID int, content string, cryptoKeyID int, markdown bool) (err error) { // {{{
if cryptoKeyID > 0 {
2024-01-05 20:00:02 +01:00
_, err = service.Db.Conn.Exec(`
UPDATE node
SET
content = '',
content_encrypted = $1,
2024-01-09 16:28:40 +01:00
markdown = $5,
crypto_key_id = CASE $2::int
WHEN 0 THEN NULL
ELSE $2
END
WHERE
id = $3 AND
user_id = $4
`,
content,
cryptoKeyID,
nodeID,
2024-01-05 20:00:02 +01:00
userID,
2024-01-09 16:28:40 +01:00
markdown,
)
} else {
2024-01-05 20:00:02 +01:00
_, err = service.Db.Conn.Exec(`
UPDATE node
SET
content = $1,
content_encrypted = '',
markdown = $5,
crypto_key_id = CASE $2::int
WHEN 0 THEN NULL
ELSE $2
END
WHERE
id = $3 AND
user_id = $4
`,
content,
cryptoKeyID,
nodeID,
2024-01-05 20:00:02 +01:00
userID,
markdown,
)
}
2023-06-18 20:13:35 +02:00
return
2024-01-09 16:28:40 +01:00
} // }}}
func RenameNode(userID, nodeID int, name string) (err error) { // {{{
2024-01-05 20:00:02 +01:00
_, err = service.Db.Conn.Exec(`
2023-06-18 22:05:10 +02:00
UPDATE node SET name = $1 WHERE user_id = $2 AND id = $3
`,
name,
2024-01-05 20:00:02 +01:00
userID,
2023-06-18 22:05:10 +02:00
nodeID,
)
return
2024-01-09 16:28:40 +01:00
} // }}}
func DeleteNode(userID, nodeID int) (err error) { // {{{
2024-01-05 20:00:02 +01:00
_, err = service.Db.Conn.Exec(`
2023-06-18 22:05:10 +02:00
WITH RECURSIVE nodetree AS (
SELECT
id, parent_id
FROM node
WHERE
user_id = $1 AND id = $2
UNION
SELECT
n.id, n.parent_id
FROM node n
INNER JOIN nodetree nt ON n.parent_id = nt.id
)
DELETE FROM node WHERE id IN (
SELECT id FROM nodetree
)`,
2024-01-05 20:00:02 +01:00
userID,
2023-06-18 22:05:10 +02:00
nodeID,
)
return
2024-01-09 16:28:40 +01:00
} // }}}
func SearchNodes(userID int, search string) (nodes []Node, err error) { // {{{
2023-07-19 10:00:36 +02:00
nodes = []Node{}
var rows *sqlx.Rows
2024-01-05 20:00:02 +01:00
rows, err = service.Db.Conn.Queryx(`
2023-07-19 10:00:36 +02:00
SELECT
id,
user_id,
COALESCE(parent_id, 0) AS parent_id,
name,
updated
FROM node
WHERE
2024-01-05 21:14:55 +01:00
user_id = $1 AND
2023-07-19 10:00:36 +02:00
crypto_key_id IS NULL AND
2023-07-20 07:27:29 +02:00
(
2024-01-05 20:00:02 +01:00
content ~* $2 OR
name ~* $2
2023-07-20 07:27:29 +02:00
)
2023-07-19 10:00:36 +02:00
ORDER BY
updated DESC
2024-01-05 20:00:02 +01:00
`, userID, search)
2023-07-19 10:00:36 +02:00
if err != nil {
return
}
defer rows.Close()
for rows.Next() {
node := Node{}
node.Complete = false
if err = rows.StructScan(&node); err != nil {
return
}
nodes = append(nodes, node)
}
return
2024-01-09 16:28:40 +01:00
} // }}}
2023-06-17 09:11:14 +02:00
// vim: foldmethod=marker