Schedule and notification
This commit is contained in:
parent
fd01e751e2
commit
e9ce21133a
7 changed files with 262 additions and 31 deletions
72
node.go
72
node.go
|
|
@ -6,6 +6,7 @@ import (
|
|||
|
||||
// Standard
|
||||
"time"
|
||||
"database/sql"
|
||||
)
|
||||
|
||||
type ChecklistItem struct {
|
||||
|
|
@ -323,8 +324,69 @@ func CreateNode(userID, parentID int, name string) (node Node, err error) { // {
|
|||
return
|
||||
} // }}}
|
||||
func UpdateNode(userID, nodeID int, content string, cryptoKeyID int, markdown bool) (err error) { // {{{
|
||||
var scannedSchedules, dbSchedules, add, remove []Schedule
|
||||
scannedSchedules = ScanForSchedules(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 = service.Db.Conn.Exec(`
|
||||
_, err = tsx.Exec(`
|
||||
UPDATE node
|
||||
SET
|
||||
content = '',
|
||||
|
|
@ -345,7 +407,7 @@ func UpdateNode(userID, nodeID int, content string, cryptoKeyID int, markdown bo
|
|||
markdown,
|
||||
)
|
||||
} else {
|
||||
_, err = service.Db.Conn.Exec(`
|
||||
_, err = tsx.Exec(`
|
||||
UPDATE node
|
||||
SET
|
||||
content = $1,
|
||||
|
|
@ -366,6 +428,12 @@ func UpdateNode(userID, nodeID int, content string, cryptoKeyID int, markdown bo
|
|||
markdown,
|
||||
)
|
||||
}
|
||||
if err != nil {
|
||||
tsx.Rollback()
|
||||
return
|
||||
}
|
||||
|
||||
err = tsx.Commit()
|
||||
|
||||
return
|
||||
} // }}}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue