diff --git a/cmd/edit.go b/cmd/edit.go index 7dac8a9..2c2e695 100644 --- a/cmd/edit.go +++ b/cmd/edit.go @@ -45,7 +45,8 @@ var editCmd = &cobra.Command{ } if content, err := os.ReadFile(tmp.Name()); err == nil { - if err := d.UpdateNode(args[0], db.UpdateParams{Content: string(content)}); err != nil { + c := string(content) + if err := d.UpdateNode(args[0], db.UpdateParams{Content: &c}); err != nil { fmt.Fprintln(os.Stderr, "failed to update:", err) return } diff --git a/cmd/update.go b/cmd/update.go index e784699..3fd604b 100644 --- a/cmd/update.go +++ b/cmd/update.go @@ -61,8 +61,17 @@ var updateCmd = &cobra.Command{ uRmTags = append(uRmTags, "_prio::low", "_prio::medium", "_prio::high") } - uParams := db.UpdateParams{Title: uTitle, Content: uContent, DueDate: uDue, ClearDue: uClearDue, + uParams := db.UpdateParams{ClearDue: uClearDue, AddTags: uAddTags, RemoveTags: uRmTags, AddRels: addRels, RemoveRels: rmRels} + if cmd.Flags().Changed("title") { + uParams.Title = &uTitle + } + if cmd.Flags().Changed("content") { + uParams.Content = &uContent + } + if cmd.Flags().Changed("due") { + uParams.DueDate = &uDue + } if err := d.UpdateNode(args[0], uParams); err != nil { fmt.Fprintln(os.Stderr, "failed to update:", err) return diff --git a/db/node.go b/db/node.go index 5e1fba0..570e09b 100644 --- a/db/node.go +++ b/db/node.go @@ -41,7 +41,7 @@ type CreateParams struct { Rels map[models.RelType][]string } type UpdateParams struct { - Title, Content, DueDate string + Title, Content, DueDate *string ClearDue bool AddTags, RemoveTags []string AddRels, RemoveRels map[models.RelType][]string @@ -97,19 +97,18 @@ func (db *DB) UpdateNode(id string, p UpdateParams) error { _, err := tx.Exec("UPDATE nodes SET "+col+" = ? WHERE id = ?", val, id) return err } - //TODO: does it make sense to check for emtpy sting? should it be possible to set the content to ""? - if p.Title != "" { - if err := upd("title", p.Title); err != nil { + if p.Title != nil { + if err := upd("title", *p.Title); err != nil { return err } } - if p.Content != "" { - if err := upd("content", p.Content); err != nil { + if p.Content != nil { + if err := upd("content", *p.Content); err != nil { return err } } - if p.DueDate != "" { - if err := upd("due_date", p.DueDate); err != nil { + if p.DueDate != nil { + if err := upd("due_date", *p.DueDate); err != nil { return err } } @@ -143,7 +142,6 @@ func (db *DB) UpdateNode(id string, p UpdateParams) error { } func (db *DB) DeleteNode(id string) error { - //TODO: check if this delete propagates? _, err := db.Exec("DELETE FROM nodes WHERE id = ?", id) return err }