This commit is contained in:
Magnus Åhall 2024-12-03 06:53:31 +01:00
parent 42b66714aa
commit ac8b334eee
35 changed files with 541 additions and 675 deletions

510
node.go
View file

@ -5,6 +5,7 @@ import (
"github.com/jmoiron/sqlx"
// Standard
"database/sql"
"time"
)
@ -24,6 +25,18 @@ type ChecklistGroup struct {
Items []ChecklistItem
}
type TreeNode struct {
UUID string
ParentUUID string `db:"parent_uuid"`
Name string
Created time.Time
Updated time.Time
Deleted bool
CreatedSeq uint64 `db:"created_seq"`
UpdatedSeq uint64 `db:"updated_seq"`
DeletedSeq sql.NullInt64 `db:"deleted_seq"`
}
type Node struct {
ID int
UserID int `db:"user_id"`
@ -44,45 +57,32 @@ type Node struct {
Markdown bool
}
func NodeTree(userID, startNodeID int) (nodes []Node, err error) { // {{{
func NodeTree(userID int, synced uint64) (nodes []TreeNode, maxSeq uint64, 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,
uuid,
COALESCE(parent_uuid, '') AS parent_uuid,
name,
created,
updated,
level
FROM nodetree
deleted IS NOT NULL AS deleted,
created_seq,
updated_seq,
deleted_seq
FROM
public.node
WHERE
user_id = $1 AND (
created_seq > $2 OR
updated_seq > $2 OR
deleted_seq > $2
)
ORDER BY
path ASC
created ASC
`,
userID,
startNodeID,
synced,
)
if err != nil {
return
@ -94,17 +94,14 @@ func NodeTree(userID, startNodeID int) (nodes []Node, err error) { // {{{
Level int
}
nodes = []Node{}
nodes = []TreeNode{}
for rows.Next() {
node := Node{}
node.Complete = false
node.Crumbs = []Node{}
node.Children = []Node{}
node.Files = []File{}
node := TreeNode{}
if err = rows.StructScan(&node); err != nil {
return
}
nodes = append(nodes, node)
maxSeq = max(maxSeq, node.CreatedSeq, node.UpdatedSeq, uint64(node.DeletedSeq.Int64))
}
return
@ -362,442 +359,3 @@ func NodeCrumbs(nodeID int) (nodes []Node, err error) { // {{{
}
return
} // }}}
/*
func CreateNode(userID, parentID int, name string) (node Node, err error) { // {{{
var rows *sqlx.Rows
rows, err = service.Db.Conn.Queryx(`
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
`,
userID,
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{}
node.Files = []File{}
node.Complete = true
}
node.Crumbs, err = NodeCrumbs(node.ID)
return
} // }}}
func UpdateNode(userID, nodeID, timeOffset int, content string, cryptoKeyID int, markdown bool) (err error) { // {{{
if nodeID == 0 {
return
}
var timezone string
row := service.Db.Conn.QueryRow(`SELECT timezone FROM _webservice.user WHERE id=$1`, userID)
err = row.Scan(&timezone)
if err != nil {
err = werr.Wrap(err).WithCode("002-000F")
return
}
var scannedSchedules, dbSchedules, add, remove []Schedule
scannedSchedules = ScanForSchedules(timezone, content)
for i := range scannedSchedules {
scannedSchedules[i].Node.ID = nodeID
scannedSchedules[i].UserID = userID
}
var tsx *sql.Tx
tsx, err = service.Db.Conn.Begin()
if err != nil {
return
}
dbSchedules, err = RetrieveSchedules(userID, nodeID)
if err != nil {
tsx.Rollback()
return
}
for _, scanned := range scannedSchedules {
found := false
for _, db := range dbSchedules {
if scanned.IsEqual(db) {
found = true
break
}
}
if !found {
add = append(add, scanned)
}
}
for _, db := range dbSchedules {
found := false
for _, scanned := range scannedSchedules {
if db.IsEqual(scanned) {
found = true
break
}
}
if !found {
remove = append(remove, db)
}
}
for _, event := range remove {
err = event.Delete(tsx)
if err != nil {
tsx.Rollback()
return
}
}
for _, event := range add {
err = event.Insert(tsx)
if err != nil {
tsx.Rollback()
return
}
}
if cryptoKeyID > 0 {
_, err = tsx.Exec(`
UPDATE node
SET
content = '',
content_encrypted = $1,
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,
userID,
markdown,
)
} else {
_, err = tsx.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,
userID,
markdown,
)
}
if err != nil {
tsx.Rollback()
return
}
err = tsx.Commit()
return
} // }}}
func RenameNode(userID, nodeID int, name string) (err error) { // {{{
_, err = service.Db.Conn.Exec(`
UPDATE node SET name = $1 WHERE user_id = $2 AND id = $3
`,
name,
userID,
nodeID,
)
return
} // }}}
func DeleteNode(userID, nodeID int) (err error) { // {{{
_, err = service.Db.Conn.Exec(`
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
)`,
userID,
nodeID,
)
return
} // }}}
func SearchNodes(userID int, search string) (nodes []Node, err error) { // {{{
nodes = []Node{}
var rows *sqlx.Rows
rows, err = service.Db.Conn.Queryx(`
SELECT
id,
user_id,
COALESCE(parent_id, 0) AS parent_id,
name,
updated
FROM node
WHERE
user_id = $1 AND
crypto_key_id IS NULL AND
(
content ~* $2 OR
name ~* $2
)
ORDER BY
updated DESC
`, userID, search)
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
} // }}}
func ChecklistGroupAdd(userID, nodeID int, label string) (item ChecklistGroup, err error) { // {{{
var row *sqlx.Row
row = service.Db.Conn.QueryRowx(
`
INSERT INTO checklist_group(node_id, "order", "label")
(
SELECT
$1,
MAX("order")+1 AS "order",
$2 AS "label"
FROM checklist_group g
INNER JOIN node n ON g.node_id = n.id
WHERE
user_id = $3 AND
node_id = $1
GROUP BY
node_id
) UNION (
SELECT
node.id AS node_id,
0 AS "order",
$2 AS "label"
FROM node
WHERE
user_id = $3 AND
node.id = $1
)
ORDER BY "order" DESC
LIMIT 1
RETURNING
*
`,
nodeID,
label,
userID,
)
err = row.StructScan(&item)
return
} // }}}
func ChecklistGroupLabel(userID, checklistGroupID int, label string) (item ChecklistItem, err error) { // {{{
_, err = service.Db.Conn.Exec(
`
UPDATE checklist_group g
SET label = $3
FROM node n
WHERE
g.node_id = n.id AND
n.user_id = $1 AND
g.id = $2;
`,
userID,
checklistGroupID,
label,
)
return
} // }}}
func ChecklistGroupItemAdd(userID, checklistGroupID int, label string) (item ChecklistItem, err error) { // {{{
var row *sqlx.Row
row = service.Db.Conn.QueryRowx(
`
INSERT INTO checklist_item(checklist_group_id, "order", "label")
(
SELECT
checklist_group_id,
MAX("order")+1 AS "order",
$1 AS "label"
FROM checklist_item
WHERE
checklist_group_id = $2
GROUP BY
checklist_group_id
) UNION (
SELECT $2 AS checklist_group_id, 0 AS "order", $1 AS "label"
)
ORDER BY "order" DESC
LIMIT 1
RETURNING
*
`,
label,
checklistGroupID,
)
err = row.StructScan(&item)
return
} // }}}
func ChecklistGroupDelete(userID, checklistGroupID int) (err error) { // {{{
_, err = service.Db.Conn.Exec(
`
DELETE
FROM checklist_group g
USING
node n
WHERE
g.id = $2 AND
g.node_id = n.id AND
n.user_id = $1
`,
userID,
checklistGroupID,
)
return
} // }}}
func ChecklistItemState(userID, checklistItemID int, state bool) (err error) { // {{{
_, err = service.Db.Conn.Exec(
`
UPDATE checklist_item i
SET checked = $3
FROM checklist_group g, node n
WHERE
i.checklist_group_id = g.id AND
g.node_id = n.id AND
n.user_id = $1 AND
i.id = $2;
`,
userID,
checklistItemID,
state,
)
return
} // }}}
func ChecklistItemLabel(userID, checklistItemID int, label string) (err error) { // {{{
_, err = service.Db.Conn.Exec(
`
UPDATE checklist_item i
SET label = $3
FROM checklist_group g, node n
WHERE
i.checklist_group_id = g.id AND
g.node_id = n.id AND
n.user_id = $1 AND
i.id = $2;
`,
userID,
checklistItemID,
label,
)
return
} // }}}
func ChecklistItemDelete(userID, checklistItemID int) (err error) { // {{{
_, err = service.Db.Conn.Exec(
`
DELETE
FROM checklist_item i
USING
checklist_group g,
node n
WHERE
i.id = $2 AND
i.checklist_group_id = g.id AND
g.node_id = n.id AND
n.user_id = $1
`,
userID,
checklistItemID,
)
return
} // }}}
func ChecklistItemMove(userID, checklistItemID, afterItemID int) (err error) { // {{{
_, err = service.Db.Conn.Exec(
`
WITH
"to" AS (
SELECT
i.checklist_group_id AS group_id,
i."order"
FROM checklist_item i
INNER JOIN checklist_group g ON i.checklist_group_id = g.id
INNER JOIN node n ON g.node_id = n.id
WHERE
n.user_id = $1 AND
i.id = $3
),
update_order AS (
UPDATE checklist_item
SET
"order" =
CASE
WHEN checklist_item."order" <= "to"."order" THEN checklist_item."order" - 1
WHEN checklist_item."order" > "to"."order" THEN checklist_item."order" + 1
END
FROM "to"
WHERE
checklist_item.id != $2 AND
checklist_item.checklist_group_id = "to".group_id
)
UPDATE checklist_item
SET
checklist_group_id = "to".group_id,
"order" = "to"."order"
FROM "to"
WHERE
checklist_item.id = $2
`,
userID,
checklistItemID,
afterItemID,
)
return
} // }}}
*/