Skip to content

Commit

Permalink
Extract Tracker model
Browse files Browse the repository at this point in the history
  • Loading branch information
Ulrich Lissé committed Apr 8, 2019
1 parent 0616c0f commit 48eb049
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 21 deletions.
14 changes: 14 additions & 0 deletions domain/tracker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package domain

import "time"

type TrackingTimeEntry struct {
Id string
Minutes Minutes
Since time.Time
}

type StoppedTimeEntry struct {
Id string
Minutes Minutes
}
6 changes: 3 additions & 3 deletions mite/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ type TimeEntryApi interface {
}

type TrackerApi interface {
Tracker() (*TrackingTimeEntry, error)
StartTracker(id string) (*TrackingTimeEntry, *StoppedTimeEntry, error)
StopTracker(id string) (*StoppedTimeEntry, error)
Tracker() (*domain.TrackingTimeEntry, error)
StartTracker(id string) (*domain.TrackingTimeEntry, *domain.StoppedTimeEntry, error)
StopTracker(id string) (*domain.StoppedTimeEntry, error)
}

type CustomerApi interface{}
Expand Down
25 changes: 7 additions & 18 deletions mite/tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,6 @@ import (
"time"
)

type TrackingTimeEntry struct {
Id string
Minutes domain.Minutes
Since time.Time
}

type StoppedTimeEntry struct {
Id string
Minutes domain.Minutes
}

type trackerResponse struct {
Tracker struct {
TrackingTimeEntry *struct {
Expand All @@ -32,30 +21,30 @@ type trackerResponse struct {
} `json:"tracker"`
}

func (r *trackerResponse) toTrackingTimeEntry() *TrackingTimeEntry {
func (r *trackerResponse) toTrackingTimeEntry() *domain.TrackingTimeEntry {
if r.Tracker.TrackingTimeEntry == nil {
return nil
}

return &TrackingTimeEntry{
return &domain.TrackingTimeEntry{
Id: strconv.Itoa(r.Tracker.TrackingTimeEntry.Id),
Minutes: domain.NewMinutes(r.Tracker.TrackingTimeEntry.Minutes),
Since: r.Tracker.TrackingTimeEntry.Since,
}
}

func (r *trackerResponse) toStoppedTimeEntry() *StoppedTimeEntry {
func (r *trackerResponse) toStoppedTimeEntry() *domain.StoppedTimeEntry {
if r.Tracker.StoppedTimeEntry == nil {
return nil
}

return &StoppedTimeEntry{
return &domain.StoppedTimeEntry{
Id: strconv.Itoa(r.Tracker.StoppedTimeEntry.Id),
Minutes: domain.NewMinutes(r.Tracker.StoppedTimeEntry.Minutes),
}
}

func (a *api) Tracker() (*TrackingTimeEntry, error) {
func (a *api) Tracker() (*domain.TrackingTimeEntry, error) {
tr := trackerResponse{}
err := a.get("/tracker.json", &tr)
if err != nil {
Expand All @@ -65,7 +54,7 @@ func (a *api) Tracker() (*TrackingTimeEntry, error) {
return tr.toTrackingTimeEntry(), nil
}

func (a *api) StartTracker(id string) (*TrackingTimeEntry, *StoppedTimeEntry, error) {
func (a *api) StartTracker(id string) (*domain.TrackingTimeEntry, *domain.StoppedTimeEntry, error) {
tr := &trackerResponse{}
err := a.patch(fmt.Sprintf("/tracker/%s.json", id), nil, tr)
if err != nil {
Expand All @@ -75,7 +64,7 @@ func (a *api) StartTracker(id string) (*TrackingTimeEntry, *StoppedTimeEntry, er
return tr.toTrackingTimeEntry(), tr.toStoppedTimeEntry(), nil
}

func (a *api) StopTracker(id string) (*StoppedTimeEntry, error) {
func (a *api) StopTracker(id string) (*domain.StoppedTimeEntry, error) {
tr := &trackerResponse{}
err := a.delete(fmt.Sprintf("/tracker/%s.json", id), tr)
if err != nil {
Expand Down

0 comments on commit 48eb049

Please sign in to comment.