Skip to content

Commit

Permalink
implement item methods
Browse files Browse the repository at this point in the history
  • Loading branch information
kobtea committed Jan 22, 2017
1 parent 35780cb commit e220a8f
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 6 deletions.
17 changes: 12 additions & 5 deletions todoist/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,22 @@ import (
type ID string

func NewID(id string) (ID, error) {
if _, err := strconv.Atoi(id); err == nil {
return ID(id), nil
}
if IsTempID(ID(id)) {
if IsValidID(ID(id)) {
return ID(id), nil
}
return "", fmt.Errorf("Invalid ID: %s", id)
}

func IsValidID(id ID) bool {
if _, err := strconv.Atoi(string(id)); err == nil {
return true
}
if IsTempID(id) {
return true
}
return false
}

func (i ID) MarshalJSON() ([]byte, error) {
s := string(i)
if IsTempID(i) {
Expand All @@ -29,7 +36,7 @@ func (i ID) MarshalJSON() ([]byte, error) {
func (i *ID) UnmarshalJSON(b []byte) (err error) {
s, err := strconv.Unquote(string(b))
if err != nil {
s = string(b) // integer id
s = string(b) // integer id
}
id, err := NewID(s)
if err != nil {
Expand Down
110 changes: 109 additions & 1 deletion todoist/item.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/url"
"net/http"
"errors"
"fmt"
)

type Item struct {
Expand Down Expand Up @@ -45,6 +46,7 @@ func (m *ItemManager) Add(item Item) (*Item, error) {
return nil, errors.New("New item requires a content")
}
item.ID = GenerateTempID()
// append item to sync state only `add` method?
m.SyncState.Items = append(m.SyncState.Items, item)
command := Command{
Type: "item_add",
Expand All @@ -56,7 +58,96 @@ func (m *ItemManager) Add(item Item) (*Item, error) {
return &item, nil
}

func (m *ItemManager) Get(ctx context.Context, id string) (*ItemResponse, error) {
func (m *ItemManager) Update(item Item) (*Item, error) {
if !IsValidID(item.ID) {
return nil, fmt.Errorf("Invalid id: %s", item.ID)
}
command := Command{
Type: "item_update",
Args: item,
UUID: GenerateUUID(),
}
m.queue = append(m.queue, command)
return &item, nil
}

func (m *ItemManager) Delete(ids []ID) (error) {
command := Command{
Type: "item_delete",
UUID: GenerateUUID(),
Args: map[string][]ID{
"ids": ids,
},
}
m.queue = append(m.queue, command)
return nil
}

func (m *ItemManager) Move(projectItems map[ID][]ID, toProject ID) error {
command := Command{
Type: "item_move",
UUID: GenerateUUID(),
Args: map[string]interface{}{
"project_items": projectItems,
"to_project": toProject,
},
}
m.queue = append(m.queue, command)
return nil
}

func (m *ItemManager) Complete(ids []ID, forceHistory bool) error {
var fh int
if forceHistory {
fh = 1
} else {
fh = 0
}
command := Command{
Type: "item_complete",
UUID: GenerateUUID(),
Args: map[string]interface{}{
"ids": ids,
"force_history": fh,
},
}
m.queue = append(m.queue, command)
return nil
}

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

func (m *ItemManager) Close(id ID) error {
command := Command{
Type: "item_close",
UUID: GenerateUUID(),
Args: map[string]ID{
"id": id,
},
}
m.queue = append(m.queue, command)
return nil
}

func (m *ItemManager) Get(ctx context.Context, id ID) (*ItemResponse, error) {
req, err := m.NewRequest(ctx, http.MethodGet, "items/get", url.Values{"item_id": {id}})
if err != nil {
return nil, err
Expand All @@ -73,6 +164,23 @@ func (m *ItemManager) Get(ctx context.Context, id string) (*ItemResponse, error)
return &out, nil
}

func (m *ItemManager) GetCompleted(ctx context.Context, projectID ID) (*[]Item, error) {
req, err := m.NewRequest(ctx, http.MethodGet, "items/get_completed", url.Values{"project_id": {projectID}})
if err != nil {
return nil, err
}
res, err := m.HTTPClient.Do(req)
if err != nil {
return nil, err
}
var out []Item
err = decodeBody(res, &out)
if err != nil {
return nil, err
}
return &out, nil
}

func (m *ItemManager) Resolve(id ID) *Item {
for _, item := range m.SyncState.Items {
if item.ID == id {
Expand Down

0 comments on commit e220a8f

Please sign in to comment.