Work on cryptokeys

This commit is contained in:
Magnus Åhall 2023-07-12 22:35:38 +02:00
parent 56a6e7145f
commit 87a802e210
12 changed files with 637 additions and 83 deletions

41
node.go
View file

@ -12,6 +12,7 @@ type Node struct {
ID int
UserID int `db:"user_id"`
ParentID int `db:"parent_id"`
CryptoKeyID int `db:"crypto_key_id"`
Name string
Content string
Updated time.Time
@ -138,6 +139,7 @@ func (session Session) Node(nodeID int) (node Node, err error) {// {{{
id,
user_id,
COALESCE(parent_id, 0) AS parent_id,
COALESCE(crypto_key_id, 0) AS crypto_key_id,
name,
content,
0 AS level
@ -152,6 +154,7 @@ func (session Session) Node(nodeID int) (node Node, err error) {// {{{
n.id,
n.user_id,
n.parent_id,
COALESCE(n.crypto_key_id, 0) AS crypto_key_id,
n.name,
'' AS content,
r.level + 1 AS level
@ -185,20 +188,22 @@ func (session Session) Node(nodeID int) (node Node, err error) {// {{{
}
if row.Level == 0 {
node.ID = row.ID
node.UserID = row.UserID
node.ParentID = row.ParentID
node.Name = row.Name
node.Content = row.Content
node.Complete = true
node.ID = row.ID
node.UserID = row.UserID
node.ParentID = row.ParentID
node.CryptoKeyID = row.CryptoKeyID
node.Name = row.Name
node.Content = row.Content
node.Complete = true
}
if row.Level == 1 {
node.Children = append(node.Children, Node{
ID: row.ID,
UserID: row.UserID,
ParentID: row.ParentID,
Name: row.Name,
ID: row.ID,
UserID: row.UserID,
ParentID: row.ParentID,
CryptoKeyID: row.CryptoKeyID,
Name: row.Name,
})
}
}
@ -281,13 +286,23 @@ func (session Session) CreateNode(parentID int, name string) (node Node, err err
node.Crumbs, err = session.NodeCrumbs(node.ID)
return
}// }}}
func (session Session) UpdateNode(nodeID int, content string) (err error) {// {{{
func (session Session) UpdateNode(nodeID int, content string, cryptoKeyID int) (err error) {// {{{
_, err = db.Exec(`
UPDATE node SET content = $1 WHERE user_id = $2 AND id = $3
UPDATE node
SET
content = $1,
crypto_key_id = CASE $2::int
WHEN 0 THEN NULL
ELSE $2
END
WHERE
id = $3 AND
user_id = $4
`,
content,
session.UserID,
cryptoKeyID,
nodeID,
session.UserID,
)
return
}// }}}