Skip to content

Commit

Permalink
Merge branch 'master' of github.com:leanovate/mite-go
Browse files Browse the repository at this point in the history
  • Loading branch information
phiros committed Apr 3, 2019
2 parents ac9fe75 + bce2269 commit 8d6620f
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 64 deletions.
7 changes: 4 additions & 3 deletions mite/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ const userAgent = "mite-go/0.1 (+github.com/leanovate/mite-go)"
const layout = "2006-01-02"

type MiteApi interface {
Projects() ([]Project, error)
Services() ([]Service, error)
TimeEntries(params *TimeEntryParameters) ([]TimeEntry, error)
TimeEntries(params *TimeEntryParameters) ([]*TimeEntry, error)
TimeEntry(id string) (*TimeEntry, error)
Projects() ([]*Project, error)
Services() ([]*Service, error)
}

type miteApi struct {
Expand Down
36 changes: 18 additions & 18 deletions mite/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,33 @@ type Project struct {
Note string
}

func (a *miteApi) Projects() ([]Project, error) {
prs := []ProjectResponse{}
err := a.get("projects.json", &prs)
if err != nil {
return nil, err
}

projects := []Project{}
for _, pr := range prs {
projects = append(projects, pr.ToProject())
}

return projects, nil
}

type ProjectResponse struct {
type projectResponse struct {
Project struct {
Id int `json:"id"`
Name string `json:"name"`
Note string `json:"note"`
} `json:"project"`
}

func (r ProjectResponse) ToProject() Project {
return Project{
func (r *projectResponse) ToProject() *Project {
return &Project{
Id: fmt.Sprintf("%d", r.Project.Id),
Name: r.Project.Name,
Note: r.Project.Note,
}
}

func (a *miteApi) Projects() ([]*Project, error) {
prs := []projectResponse{}
err := a.get("projects.json", &prs)
if err != nil {
return nil, err
}

projects := []*Project{}
for _, pr := range prs {
projects = append(projects, pr.ToProject())
}

return projects, nil
}
36 changes: 18 additions & 18 deletions mite/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,33 @@ type Service struct {
Note string
}

func (a *miteApi) Services() ([]Service, error) {
srs := []ServiceResponse{}
err := a.get("services.json", &srs)
if err != nil {
return nil, err
}

services := []Service{}
for _, sr := range srs {
services = append(services, sr.ToService())
}

return services, nil
}

type ServiceResponse struct {
type serviceResponse struct {
Service struct {
Id int `json:"id"`
Name string `json:"name"`
Note string `json:"note"`
} `json:"service"`
}

func (r ServiceResponse) ToService() Service {
return Service{
func (r *serviceResponse) ToService() *Service {
return &Service{
Id: fmt.Sprintf("%d", r.Service.Id),
Name: r.Service.Name,
Note: r.Service.Note,
}
}

func (a *miteApi) Services() ([]*Service, error) {
srs := []serviceResponse{}
err := a.get("services.json", &srs)
if err != nil {
return nil, err
}

services := []*Service{}
for _, sr := range srs {
services = append(services, sr.ToService())
}

return services, nil
}
60 changes: 35 additions & 25 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,43 +62,26 @@ 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())
}

return timeEntries, 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)
func (a *miteApi) TimeEntry(id string) (*TimeEntry, error) {
ter := timeEntryResponse{}
err := a.get(fmt.Sprintf("/time_entries/%s.json", id), &ter)
if err != nil {
panic(err)
return nil, 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 8d6620f

Please sign in to comment.