From 336c0f3ca90867029983d10ea135bb114b6ab172 Mon Sep 17 00:00:00 2001 From: Kevin Schoon Date: Wed, 17 Jan 2018 15:53:45 +0800 Subject: [PATCH] add delete command --- main.go | 11 ++++++++++- store.go | 18 ++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index b5e71d1..b30c964 100644 --- a/main.go +++ b/main.go @@ -86,7 +86,16 @@ func list(path *string) func(*cli.Cmd) { } func _delete(path *string) func(*cli.Cmd) { - return func(cmd *cli.Cmd) {} + return func(cmd *cli.Cmd) { + cmd.Spec = "[OPTIONS] TASK_ID" + var taskID = cmd.IntArg("TASK_ID", -1, "task to delete") + cmd.Action = func() { + db, err := NewStore(*path) + maybe(err) + defer db.Close() + maybe(db.DeleteTask(*taskID)) + } + } } func main() { diff --git a/store.go b/store.go index 9aa63e1..b40fd03 100644 --- a/store.go +++ b/store.go @@ -108,6 +108,24 @@ func (s Store) ReadRecords(taskID int) ([]*Record, error) { return records, nil } +func (s Store) DeleteTask(taskID int) error { + tx, err := s.db.Begin() + if err != nil { + return err + } + _, err = tx.Exec("DELETE FROM task WHERE rowid = $1", &taskID) + if err != nil { + tx.Rollback() + return err + } + _, err = tx.Exec("DELETE FROM record WHERE task_id = $1", &taskID) + if err != nil { + tx.Rollback() + return err + } + return tx.Commit() +} + func (s Store) Close() error { return s.db.Close() } func initDB(db *Store) error {