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 {