wip: rewrite to webservice library
This commit is contained in:
parent
abbd320b93
commit
52fba2289e
23 changed files with 201 additions and 680 deletions
71
node.go
71
node.go
|
|
@ -25,9 +25,9 @@ type Node struct {
|
|||
ContentEncrypted string `db:"content_encrypted" json:"-"`
|
||||
}
|
||||
|
||||
func (session Session) NodeTree(startNodeID int) (nodes []Node, err error) {// {{{
|
||||
func NodeTree(userID, startNodeID int) (nodes []Node, err error) {// {{{
|
||||
var rows *sqlx.Rows
|
||||
rows, err = db.Queryx(`
|
||||
rows, err = service.Db.Conn.Queryx(`
|
||||
WITH RECURSIVE nodetree AS (
|
||||
SELECT
|
||||
*,
|
||||
|
|
@ -62,7 +62,7 @@ func (session Session) NodeTree(startNodeID int) (nodes []Node, err error) {// {
|
|||
ORDER BY
|
||||
path ASC
|
||||
`,
|
||||
session.UserID,
|
||||
userID,
|
||||
startNodeID,
|
||||
)
|
||||
if err != nil {
|
||||
|
|
@ -87,9 +87,9 @@ func (session Session) NodeTree(startNodeID int) (nodes []Node, err error) {// {
|
|||
|
||||
return
|
||||
}// }}}
|
||||
func (session Session) RootNode() (node Node, err error) {// {{{
|
||||
func RootNode(userID int) (node Node, err error) {// {{{
|
||||
var rows *sqlx.Rows
|
||||
rows, err = db.Queryx(`
|
||||
rows, err = service.Db.Conn.Queryx(`
|
||||
SELECT
|
||||
id,
|
||||
user_id,
|
||||
|
|
@ -100,7 +100,7 @@ func (session Session) RootNode() (node Node, err error) {// {{{
|
|||
user_id = $1 AND
|
||||
parent_id IS NULL
|
||||
`,
|
||||
session.UserID,
|
||||
userID,
|
||||
)
|
||||
if err != nil {
|
||||
return
|
||||
|
|
@ -108,7 +108,7 @@ func (session Session) RootNode() (node Node, err error) {// {{{
|
|||
defer rows.Close()
|
||||
|
||||
node.Name = "Start"
|
||||
node.UserID = session.UserID
|
||||
node.UserID = userID
|
||||
node.Complete = true
|
||||
node.Children = []Node{}
|
||||
node.Crumbs = []Node{}
|
||||
|
|
@ -129,13 +129,13 @@ func (session Session) RootNode() (node Node, err error) {// {{{
|
|||
|
||||
return
|
||||
}// }}}
|
||||
func (session Session) Node(nodeID int) (node Node, err error) {// {{{
|
||||
func RetrieveNode(userID, nodeID int) (node Node, err error) {// {{{
|
||||
if nodeID == 0 {
|
||||
return session.RootNode()
|
||||
return RootNode(userID)
|
||||
}
|
||||
|
||||
var rows *sqlx.Rows
|
||||
rows, err = db.Queryx(`
|
||||
rows, err = service.Db.Conn.Queryx(`
|
||||
WITH RECURSIVE recurse AS (
|
||||
SELECT
|
||||
id,
|
||||
|
|
@ -170,7 +170,7 @@ func (session Session) Node(nodeID int) (node Node, err error) {// {{{
|
|||
|
||||
SELECT * FROM recurse ORDER BY level ASC
|
||||
`,
|
||||
session.UserID,
|
||||
userID,
|
||||
nodeID,
|
||||
)
|
||||
if err != nil {
|
||||
|
|
@ -217,14 +217,14 @@ func (session Session) Node(nodeID int) (node Node, err error) {// {{{
|
|||
}
|
||||
}
|
||||
|
||||
node.Crumbs, err = session.NodeCrumbs(node.ID)
|
||||
node.Files, err = session.Files(node.ID, 0)
|
||||
node.Crumbs, err = NodeCrumbs(node.ID)
|
||||
node.Files, err = Files(userID, node.ID, 0)
|
||||
|
||||
return
|
||||
}// }}}
|
||||
func (session Session) NodeCrumbs(nodeID int) (nodes []Node, err error) {// {{{
|
||||
func NodeCrumbs(nodeID int) (nodes []Node, err error) {// {{{
|
||||
var rows *sqlx.Rows
|
||||
rows, err = db.Queryx(`
|
||||
rows, err = service.Db.Conn.Queryx(`
|
||||
WITH RECURSIVE nodes AS (
|
||||
SELECT
|
||||
id,
|
||||
|
|
@ -260,10 +260,10 @@ func (session Session) NodeCrumbs(nodeID int) (nodes []Node, err error) {// {{{
|
|||
}
|
||||
return
|
||||
}// }}}
|
||||
func (session Session) CreateNode(parentID int, name string) (node Node, err error) {// {{{
|
||||
func CreateNode(userID, parentID int, name string) (node Node, err error) {// {{{
|
||||
var rows *sqlx.Rows
|
||||
|
||||
rows, err = db.Queryx(`
|
||||
rows, err = service.Db.Conn.Queryx(`
|
||||
INSERT INTO node(user_id, parent_id, name)
|
||||
VALUES($1, NULLIF($2, 0)::integer, $3)
|
||||
RETURNING
|
||||
|
|
@ -273,7 +273,7 @@ func (session Session) CreateNode(parentID int, name string) (node Node, err err
|
|||
name,
|
||||
content
|
||||
`,
|
||||
session.UserID,
|
||||
userID,
|
||||
parentID,
|
||||
name,
|
||||
)
|
||||
|
|
@ -292,12 +292,12 @@ func (session Session) CreateNode(parentID int, name string) (node Node, err err
|
|||
node.Complete = true
|
||||
}
|
||||
|
||||
node.Crumbs, err = session.NodeCrumbs(node.ID)
|
||||
node.Crumbs, err = NodeCrumbs(node.ID)
|
||||
return
|
||||
}// }}}
|
||||
func (session Session) UpdateNode(nodeID int, content string, cryptoKeyID int) (err error) {// {{{
|
||||
func UpdateNode(userID, nodeID int, content string, cryptoKeyID int) (err error) {// {{{
|
||||
if cryptoKeyID > 0 {
|
||||
_, err = db.Exec(`
|
||||
_, err = service.Db.Conn.Exec(`
|
||||
UPDATE node
|
||||
SET
|
||||
content = '',
|
||||
|
|
@ -313,10 +313,10 @@ func (session Session) UpdateNode(nodeID int, content string, cryptoKeyID int) (
|
|||
content,
|
||||
cryptoKeyID,
|
||||
nodeID,
|
||||
session.UserID,
|
||||
userID,
|
||||
)
|
||||
} else {
|
||||
_, err = db.Exec(`
|
||||
_, err = service.Db.Conn.Exec(`
|
||||
UPDATE node
|
||||
SET
|
||||
content = $1,
|
||||
|
|
@ -332,24 +332,24 @@ func (session Session) UpdateNode(nodeID int, content string, cryptoKeyID int) (
|
|||
content,
|
||||
cryptoKeyID,
|
||||
nodeID,
|
||||
session.UserID,
|
||||
userID,
|
||||
)
|
||||
}
|
||||
|
||||
return
|
||||
}// }}}
|
||||
func (session Session) RenameNode(nodeID int, name string) (err error) {// {{{
|
||||
_, err = db.Exec(`
|
||||
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,
|
||||
session.UserID,
|
||||
userID,
|
||||
nodeID,
|
||||
)
|
||||
return
|
||||
}// }}}
|
||||
func (session Session) DeleteNode(nodeID int) (err error) {// {{{
|
||||
_, err = db.Exec(`
|
||||
func DeleteNode(userID, nodeID int) (err error) {// {{{
|
||||
_, err = service.Db.Conn.Exec(`
|
||||
WITH RECURSIVE nodetree AS (
|
||||
SELECT
|
||||
id, parent_id
|
||||
|
|
@ -368,15 +368,15 @@ func (session Session) DeleteNode(nodeID int) (err error) {// {{{
|
|||
DELETE FROM node WHERE id IN (
|
||||
SELECT id FROM nodetree
|
||||
)`,
|
||||
session.UserID,
|
||||
userID,
|
||||
nodeID,
|
||||
)
|
||||
return
|
||||
}// }}}
|
||||
func (session Session) SearchNodes(search string) (nodes []Node, err error) {// {{{
|
||||
func SearchNodes(userID int, search string) (nodes []Node, err error) {// {{{
|
||||
nodes = []Node{}
|
||||
var rows *sqlx.Rows
|
||||
rows, err = db.Queryx(`
|
||||
rows, err = service.Db.Conn.Queryx(`
|
||||
SELECT
|
||||
id,
|
||||
user_id,
|
||||
|
|
@ -385,14 +385,15 @@ func (session Session) SearchNodes(search string) (nodes []Node, err error) {//
|
|||
updated
|
||||
FROM node
|
||||
WHERE
|
||||
user_id = $1
|
||||
crypto_key_id IS NULL AND
|
||||
(
|
||||
content ~* $1 OR
|
||||
name ~* $1
|
||||
content ~* $2 OR
|
||||
name ~* $2
|
||||
)
|
||||
ORDER BY
|
||||
updated DESC
|
||||
`, search)
|
||||
`, userID, search)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue