Skip to content

Commit

Permalink
implement relation
Browse files Browse the repository at this point in the history
  • Loading branch information
kobtea committed Feb 21, 2017
1 parent 13366a3 commit 75e26c5
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 7 deletions.
15 changes: 8 additions & 7 deletions cmd/todoist/cmd/today.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,17 @@ var todayCmd = &cobra.Command{
sort.Slice(items, func(i, j int) bool {
return items[i].DueDateUtc.Before(items[j].DueDateUtc)
})
relations := client.Relation.Items(items)
var rows [][]todoist.ColorStringer
for _, i := range items {
var project todoist.Project
if p := client.Project.Resolve(i.ProjectID); p != nil {
project = *p
project := todoist.Project{}
if v, ok := relations.Projects[i.ProjectID]; ok {
project = v
}
var labels []string
for _, l := range i.Labels {
if j := client.Label.Resolve(l); j != nil {
labels = append(labels, j.String())
labels := []string{}
for _, lid := range i.Labels {
if v, ok := relations.Labels[lid]; ok {
labels = append(labels, v.String())
}
}
rows = append(rows, []todoist.ColorStringer{
Expand Down
2 changes: 2 additions & 0 deletions todoist/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type Client struct {
Item *ItemClient
Label *LabelClient
Project *ProjectClient
Relation *RelationClient
queue []Command
}

Expand Down Expand Up @@ -77,6 +78,7 @@ func NewClient(endpoint, token, sync_token, cache_dir string, logger *log.Logger
c.Item = &ItemClient{c, &itemCache{&c.syncState.Items}}
c.Label = &LabelClient{c, &labelCache{&c.syncState.Labels}}
c.Project = &ProjectClient{c, &projectCache{&c.syncState.Projects}}
c.Relation = &RelationClient{c}
return c, nil
}

Expand Down
32 changes: 32 additions & 0 deletions todoist/relation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package todoist

type RelationClient struct {
*Client
}

type Relations struct {
//Users map[ID]User
Projects map[ID]Project
Labels map[ID]Label
}

func (c RelationClient) Items(items []Item) Relations {
res := Relations{Projects: map[ID]Project{}, Labels: map[ID]Label{}}
for _, item := range items {
if _, ok := res.Projects[item.ProjectID]; !ok {
p := c.Project.Resolve(item.ProjectID)
if p != nil {
res.Projects[item.ProjectID] = *p
}
}
for _, id := range item.Labels {
if _, ok := res.Labels[id]; !ok {
l := c.Label.Resolve(id)
if l != nil {
res.Labels[id] = *l
}
}
}
}
return res
}

0 comments on commit 75e26c5

Please sign in to comment.