add delete command

This commit is contained in:
Kevin Schoon 2018-01-17 15:53:45 +08:00
parent e782c4edd0
commit 336c0f3ca9
2 changed files with 28 additions and 1 deletions

11
main.go
View File

@ -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() {

View File

@ -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 {