add delete command
This commit is contained in:
parent
e782c4edd0
commit
336c0f3ca9
11
main.go
11
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() {
|
||||
|
|
18
store.go
18
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 {
|
||||
|
|
Loading…
Reference in New Issue