Skip to content

Commit

Permalink
Change TimeEntries() to return pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
Ulrich Lissé committed Apr 3, 2019
1 parent 503e07e commit bce2269
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 36 deletions.
4 changes: 2 additions & 2 deletions mite/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
66 changes: 32 additions & 34 deletions mite/time_entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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())
}
Expand All @@ -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
}

0 comments on commit bce2269

Please sign in to comment.