Skip to content

Commit

Permalink
fix item
Browse files Browse the repository at this point in the history
  • Loading branch information
kobtea committed Sep 12, 2019
1 parent 82b9edb commit cff7de9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 45 deletions.
58 changes: 29 additions & 29 deletions cmd/todoist/cmd/item.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,29 +192,27 @@ var itemDeleteCmd = &cobra.Command{
Short: "delete items",
RunE: func(cmd *cobra.Command, args []string) error {
if err := util.AutoCommit(func(client todoist.Client, ctx context.Context) error {
return util.ProcessIDs(
args,
func(ids []todoist.ID) error {
var items []todoist.Item
for _, id := range ids {
item := client.Item.Resolve(id)
if item == nil {
return fmt.Errorf("invalid id: %s", id)
}
items = append(items, *item)
}
relations := client.Relation.Items(items)
fmt.Println(util.ItemTableString(items, relations, func(i todoist.Item) todoist.Time { return i.Due.Date }))

reader := bufio.NewReader(os.Stdin)
fmt.Print("are you sure to delete above item(s)? (y/[n]): ")
ans, err := reader.ReadString('\n')
if ans != "y\n" || err != nil {
fmt.Println("abort")
return errors.New("abort")
}
return client.Item.Delete(ids)
})
if len(args) != 1 {
return fmt.Errorf("require one item id")
}
id, err := todoist.NewID(args[0])
if err != nil {
return err
}
item := client.Item.Resolve(id)
if item == nil {
return fmt.Errorf("invalid id: %s", id)
}
relations := client.Relation.Items([]todoist.Item{*item})
fmt.Println(util.ItemTableString([]todoist.Item{*item}, relations, func(i todoist.Item) todoist.Time { return i.Due.Date }))
reader := bufio.NewReader(os.Stdin)
fmt.Print("are you sure to delete above item(s)? (y/[n]): ")
ans, err := reader.ReadString('\n')
if ans != "y\n" || err != nil {
fmt.Println("abort")
return errors.New("abort")
}
return client.Item.Delete(id)
}); err != nil {
if err.Error() == "abort" {
return nil
Expand Down Expand Up @@ -300,12 +298,14 @@ var itemUncompleteCmd = &cobra.Command{
Short: "uncomplete items",
RunE: func(cmd *cobra.Command, args []string) error {
if err := util.AutoCommit(func(client todoist.Client, ctx context.Context) error {
return util.ProcessIDs(
args,
func(ids []todoist.ID) error {
restoreState := map[todoist.ID][]string{}
return client.Item.Uncomplete(ids, true, restoreState)
})
if len(args) != 1 {
return fmt.Errorf("require one item id")
}
id, err := todoist.NewID(args[0])
if err != nil {
return err
}
return client.Item.Uncomplete(id)
}); err != nil {
return err
}
Expand Down
23 changes: 7 additions & 16 deletions todoist/item.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,15 @@ type Item struct {
Lang string `json:"lang"`
} `json:"due,omitempty"`
Priority int `json:"priority,omitempty"`
Indent int `json:"indent,omitempty"`
ItemOrder int `json:"item_order,omitempty"`
ParentID ID `json:"parent_id,omitempty"`
ChildOrder int `json:"child_order,omitempty"`
DayOrder int `json:"day_order,omitempty"`
Collapsed int `json:"collapsed,omitempty"`
Labels []ID `json:"labels,omitempty"`
AssignedByUID ID `json:"assigned_by_uid,omitempty"`
ResponsibleUID ID `json:"responsible_uid,omitempty"`
Checked int `json:"checked,omitempty"`
InHistory int `json:"in_history,omitempty"`
IsArchived int `json:"is_archived,omitempty"`
SyncID int `json:"sync_id,omitempty"`
DateAdded Time `json:"date_added,omitempty"`
CompletedDate Time `json:"completed_date"`
Expand Down Expand Up @@ -84,12 +83,12 @@ func (c *ItemClient) Update(item Item) (*Item, error) {
return &item, nil
}

func (c *ItemClient) Delete(ids []ID) error {
func (c *ItemClient) Delete(id ID) error {
command := Command{
Type: "item_delete",
UUID: GenerateUUID(),
Args: map[string][]ID{
"ids": ids,
Args: map[string]ID{
"id": id,
},
}
c.queue = append(c.queue, command)
Expand Down Expand Up @@ -128,20 +127,12 @@ func (c *ItemClient) Complete(ids []ID, forceHistory bool) error {
return nil
}

func (c *ItemClient) Uncomplete(ids []ID, updateItemOrders bool, restoreState map[ID][]string) error {
var uio int
if updateItemOrders {
uio = 1
} else {
uio = 0
}
func (c *ItemClient) Uncomplete(id ID) error {
command := Command{
Type: "item_uncomplete",
UUID: GenerateUUID(),
Args: map[string]interface{}{
"ids": ids,
"update_item_orders": uio,
"restore_state": restoreState,
"id": id,
},
}
c.queue = append(c.queue, command)
Expand Down

0 comments on commit cff7de9

Please sign in to comment.