From bce2269b078d7b60a9d3ffcc75e92bb246d002cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ulrich=20Liss=C3=A9?= Date: Wed, 3 Apr 2019 11:11:03 +0200 Subject: [PATCH] Change TimeEntries() to return pointers --- mite/api.go | 4 +-- mite/time_entry.go | 66 ++++++++++++++++++++++------------------------ 2 files changed, 34 insertions(+), 36 deletions(-) diff --git a/mite/api.go b/mite/api.go index adb1579..8f2a1a2 100644 --- a/mite/api.go +++ b/mite/api.go @@ -11,10 +11,10 @@ const userAgent = "mite-go/0.1 (+github.com/leanovate/mite-go)" const layout = "2006-01-02" type MiteApi interface { + TimeEntries(params *TimeEntryParameters) ([]*TimeEntry, error) + TimeEntry(id string) (*TimeEntry, error) Projects() ([]*Project, error) Services() ([]*Service, error) - TimeEntries(params *TimeEntryParameters) ([]TimeEntry, error) - TimeEntry(id string) (*TimeEntry, error) } type miteApi struct { diff --git a/mite/time_entry.go b/mite/time_entry.go index afb2962..834b74b 100644 --- a/mite/time_entry.go +++ b/mite/time_entry.go @@ -21,7 +21,34 @@ type TimeEntryParameters struct { Direction string } -func (a *miteApi) TimeEntries(params *TimeEntryParameters) ([]TimeEntry, error) { +type timeEntryResponse struct { + TimeEntry struct { + Id int `json:"id"` + Note string `json:"note"` + Minutes int `json:"minutes"` + Date string `json:"date_at"` + ProjectName string `json:"project_name"` + ServiceName string `json:"service_name"` + } `json:"time_entry"` +} + +func (r *timeEntryResponse) ToTimeEntry() *TimeEntry { + date, err := time.Parse(layout, r.TimeEntry.Date) + if err != nil { + panic(err) + } + + return &TimeEntry{ + Id: fmt.Sprintf("%d", r.TimeEntry.Id), + Note: r.TimeEntry.Note, + Duration: time.Duration(r.TimeEntry.Minutes) * time.Minute, + Date: date, + ProjectName: r.TimeEntry.ProjectName, + ServiceName: r.TimeEntry.ServiceName, + } +} + +func (a *miteApi) TimeEntries(params *TimeEntryParameters) ([]*TimeEntry, error) { values := url.Values{} if params != nil { if params.From != nil { @@ -35,13 +62,13 @@ func (a *miteApi) TimeEntries(params *TimeEntryParameters) ([]TimeEntry, error) } } - ter := []TimeEntryResponse{} + ter := []timeEntryResponse{} err := a.getParametrized("time_entries.json", values, &ter) if err != nil { return nil, err } - timeEntries := []TimeEntry{} + timeEntries := []*TimeEntry{} for _, te := range ter { timeEntries = append(timeEntries, te.ToTimeEntry()) } @@ -50,40 +77,11 @@ func (a *miteApi) TimeEntries(params *TimeEntryParameters) ([]TimeEntry, error) } func (a *miteApi) TimeEntry(id string) (*TimeEntry, error) { - ter := TimeEntryResponse{} + ter := timeEntryResponse{} err := a.get(fmt.Sprintf("/time_entries/%s.json", id), &ter) if err != nil { return nil, err } - te := ter.ToTimeEntry() - - return &te, nil -} - -type TimeEntryResponse struct { - TimeEntry struct { - Id int `json:"id"` - Note string `json:"note"` - Minutes int `json:"minutes"` - Date string `json:"date_at"` - ProjectName string `json:"project_name"` - ServiceName string `json:"service_name"` - } `json:"time_entry"` -} - -func (r TimeEntryResponse) ToTimeEntry() TimeEntry { - date, err := time.Parse(layout, r.TimeEntry.Date) - if err != nil { - panic(err) - } - - return TimeEntry{ - Id: fmt.Sprintf("%d", r.TimeEntry.Id), - Note: r.TimeEntry.Note, - Duration: time.Duration(r.TimeEntry.Minutes) * time.Minute, - Date: date, - ProjectName: r.TimeEntry.ProjectName, - ServiceName: r.TimeEntry.ServiceName, - } + return ter.ToTimeEntry(), nil }