diff --git a/cmd/entries.go b/cmd/entries.go index d4b18ee..c9d2011 100644 --- a/cmd/entries.go +++ b/cmd/entries.go @@ -49,7 +49,7 @@ var entriesListCommand = &cobra.Command{ return } - entries, err := deps.miteApi.TimeEntries(&mite.TimeEntryParameters{ + entries, err := deps.miteApi.TimeEntries(&mite.TimeEntryQuery{ To: &to, From: &from, Direction: direction, diff --git a/mite/api.go b/mite/api.go index e82f307..7a7236a 100644 --- a/mite/api.go +++ b/mite/api.go @@ -12,7 +12,7 @@ const userAgent = "mite-go/0.1 (+github.com/leanovate/mite-go)" const layout = "2006-01-02" type MiteApi interface { - TimeEntries(params *TimeEntryParameters) ([]*TimeEntry, error) + TimeEntries(query *TimeEntryQuery) ([]*TimeEntry, error) TimeEntry(id string) (*TimeEntry, error) CreateTimeEntry(command *TimeEntryCommand) (*TimeEntry, error) Projects() ([]*Project, error) diff --git a/mite/time_entry.go b/mite/time_entry.go index 14c5cda..9641d8c 100644 --- a/mite/time_entry.go +++ b/mite/time_entry.go @@ -45,13 +45,29 @@ func (c *TimeEntryCommand) toRequest() *timeEntryRequest { return r } -// TODO: rename to TimeEntryQuery -type TimeEntryParameters struct { +type TimeEntryQuery struct { From *time.Time To *time.Time Direction string } +func (q *TimeEntryQuery) toValues() url.Values { + v := url.Values{} + if q != nil { + if q.From != nil { + v.Add("from", q.From.Format(layout)) + } + if q.To != nil { + v.Add("to", q.To.Format(layout)) + } + if q.Direction != "" { + v.Add("direction", q.Direction) + } + } + + return v +} + type timeEntryRequest struct { TimeEntry struct { Date string `json:"date_at"` @@ -89,22 +105,9 @@ func (r *timeEntryResponse) ToTimeEntry() *TimeEntry { } } -func (a *miteApi) TimeEntries(params *TimeEntryParameters) ([]*TimeEntry, error) { - values := url.Values{} - if params != nil { - if params.From != nil { - values.Add("from", params.From.Format(layout)) - } - if params.To != nil { - values.Add("to", params.To.Format(layout)) - } - if params.Direction != "" { - values.Add("direction", params.Direction) - } - } - +func (a *miteApi) TimeEntries(query *TimeEntryQuery) ([]*TimeEntry, error) { ter := []timeEntryResponse{} - err := a.getParametrized("time_entries.json", values, &ter) + err := a.getParametrized("time_entries.json", query.toValues(), &ter) if err != nil { return nil, err }