Skip to content

Commit

Permalink
Refactor time entry query
Browse files Browse the repository at this point in the history
  • Loading branch information
Ulrich Lissé committed Apr 3, 2019
1 parent b2c7fb4 commit 1f655d4
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 19 deletions.
2 changes: 1 addition & 1 deletion cmd/entries.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion mite/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
37 changes: 20 additions & 17 deletions mite/time_entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 1f655d4

Please sign in to comment.