Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
phiros committed Apr 5, 2019
2 parents f9b5673 + 7233dba commit 626a7fc
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 58 deletions.
73 changes: 53 additions & 20 deletions cmd/tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package cmd

import (
"errors"
"fmt"
"github.com/cheynewallace/tabby"
"github.com/leanovate/mite-go/date"
"github.com/leanovate/mite-go/mite"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -31,7 +31,19 @@ var trackerStatusCommand = &cobra.Command{
Use: "status",
Short: "shows the status of the time tracker",
RunE: func(cmd *cobra.Command, args []string) (err error) {
panic("implement trackerStatusCommand")
tracking, err := deps.miteApi.Tracker()
if err != nil {
return err
}
if tracking == nil {
return nil
}

t := tabby.New()
t.AddHeader("id", "time", "state", "since")
t.AddLine(tracking.Id, tracking.Duration, "tracking", tracking.Since)
t.Print()

return nil
},
}
Expand All @@ -46,8 +58,45 @@ var trackerStartCommand = &cobra.Command{
return err
}
}
fmt.Printf("passed id: %s\n", trackerTimeEntryId)
panic("implement trackerStartCommand")

tracking, stopped, err := deps.miteApi.StartTracker(trackerTimeEntryId)
if err != nil {
return err
}

t := tabby.New()
t.AddHeader("id", "time", "state", "since")
t.AddLine(tracking.Id, tracking.Duration, "tracking", tracking.Since)
if stopped != nil {
t.AddLine(stopped.Id, stopped.Duration, "stopped")
}
t.Print()

return nil
},
}

var trackerStopCommand = &cobra.Command{
Use: "stop",
Short: "stops the time tracker for a time entry",
RunE: func(cmd *cobra.Command, args []string) (err error) {
if trackerTimeEntryId == "" {
trackerTimeEntryId, err = fetchLatestTimeEntryForToday()
if err != nil {
return err
}
}

stopped, err := deps.miteApi.StopTracker(trackerTimeEntryId)
if err != nil {
return err
}

t := tabby.New()
t.AddHeader("id", "time", "state")
t.AddLine(stopped.Id, stopped.Duration, "stopped")
t.Print()

return nil
},
}
Expand All @@ -70,19 +119,3 @@ func fetchLatestTimeEntryForToday() (string, error) {

return entries[0].Id, nil
}

var trackerStopCommand = &cobra.Command{
Use: "stop",
Short: "stops the time tracker for a time entry",
RunE: func(cmd *cobra.Command, args []string) (err error) {
if trackerTimeEntryId == "" {
trackerTimeEntryId, err = fetchLatestTimeEntryForToday()
if err != nil {
return err
}
}
fmt.Printf("passed id: %s\n", trackerTimeEntryId)
panic("implement trackerStopCommand")
return nil
},
}
25 changes: 22 additions & 3 deletions mite/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ type TimeEntryApi interface {
DeleteTimeEntry(id string) error
}

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

type CustomerApi interface{}

type ProjectApi interface {
Expand All @@ -36,6 +42,7 @@ type UserApi interface{}
type Api interface {
AccountApi
TimeEntryApi
TrackerApi
CustomerApi
ProjectApi
ServiceApi
Expand Down Expand Up @@ -107,10 +114,14 @@ func (a *api) post(resource string, body interface{}, result interface{}) error
return err
}

return json.NewDecoder(res.Body).Decode(result)
if result != nil {
return json.NewDecoder(res.Body).Decode(result)
}

return nil
}

func (a *api) patch(resource string, body interface{}) error {
func (a *api) patch(resource string, body interface{}, result interface{}) error {
b, err := json.Marshal(body)
if err != nil {
return err
Expand All @@ -135,10 +146,14 @@ func (a *api) patch(resource string, body interface{}) error {
return err
}

if result != nil {
return json.NewDecoder(res.Body).Decode(result)
}

return nil
}

func (a *api) delete(resource string) error {
func (a *api) delete(resource string, result interface{}) error {
req, err := http.NewRequest(http.MethodDelete, a.url(resource), nil)
if err != nil {
return err
Expand All @@ -157,6 +172,10 @@ func (a *api) delete(resource string) error {
return err
}

if result != nil {
return json.NewDecoder(res.Body).Decode(result)
}

return nil
}

Expand Down
4 changes: 2 additions & 2 deletions mite/time_entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,9 @@ func (a *api) CreateTimeEntry(command *TimeEntryCommand) (*TimeEntry, error) {
}

func (a *api) EditTimeEntry(id string, command *TimeEntryCommand) error {
return a.patch(fmt.Sprintf("/time_entries/%s.json", id), command.toRequest())
return a.patch(fmt.Sprintf("/time_entries/%s.json", id), command.toRequest(), nil)
}

func (a *api) DeleteTimeEntry(id string) error {
return a.delete(fmt.Sprintf("/time_entries/%s.json", id))
return a.delete(fmt.Sprintf("/time_entries/%s.json", id), nil)
}
86 changes: 53 additions & 33 deletions mite/tracker.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package mite

import (
"math"
"fmt"
"strconv"
"time"
)
Expand All @@ -17,49 +17,69 @@ type StoppedTimeEntry struct {
Duration time.Duration
}

type Tracker struct {
Tracking TrackingTimeEntry
Stopped StoppedTimeEntry
type trackerResponse struct {
Tracker struct {
TrackingTimeEntry *struct {
Id int `json:"id"`
Minutes int `json:"minutes"`
Since time.Time `json:"since"`
} `json:"tracking_time_entry"`
StoppedTimeEntry *struct {
Id int `json:"id"`
Minutes int `json:"minutes"`
} `json:"stopped_time_entry"`
} `json:"tracker"`
}

type TrackerCommand struct {
Id string
Duration time.Duration
Since time.Time
}
func (r *trackerResponse) toTrackingTimeEntry() *TrackingTimeEntry {
if r.Tracker.TrackingTimeEntry == nil {
return nil
}

func (c *TrackerCommand) toRequest() *trackerRequest {
i, err := strconv.Atoi(c.Id)
if err != nil {
panic(err)
return &TrackingTimeEntry{
Id: strconv.Itoa(r.Tracker.TrackingTimeEntry.Id),
Duration: time.Duration(r.Tracker.TrackingTimeEntry.Minutes) * time.Minute,
Since: r.Tracker.TrackingTimeEntry.Since,
}
}

r := &trackerRequest{}
r.Tracker.TrackingTimeEntry.Id = i
r.Tracker.TrackingTimeEntry.Minutes = int(math.Floor(math.Round(c.Duration.Minutes()))) // BOGUS
r.Tracker.TrackingTimeEntry.Since = c.Since
func (r *trackerResponse) toStoppedTimeEntry() *StoppedTimeEntry {
if r.Tracker.StoppedTimeEntry == nil {
return nil
}

return r
return &StoppedTimeEntry{
Id: strconv.Itoa(r.Tracker.StoppedTimeEntry.Id),
Duration: time.Duration(r.Tracker.StoppedTimeEntry.Minutes) * time.Minute,
}
}

type trackerRequest struct {
Tracker struct {
TrackingTimeEntry struct {
Id int `json:"id"`
Minutes int `json:"minutes"`
Since time.Time `json:"since"`
} `json:"tracking_time_entry"`
} `json:"tracker"`
}
func (a *api) Tracker() (*TrackingTimeEntry, error) {
tr := trackerResponse{}
err := a.get("/tracker.json", &tr)
if err != nil {
return nil, err
}

func (a *api) Tracker() (*Tracker, error) {
return &Tracker{}, nil
return tr.toTrackingTimeEntry(), nil
}

func (a *api) StartTracker(command TrackerCommand) (*Tracker, error) {
return &Tracker{}, nil
func (a *api) StartTracker(id string) (*TrackingTimeEntry, *StoppedTimeEntry, error) {
tr := &trackerResponse{}
err := a.patch(fmt.Sprintf("/tracker/%s.json", id), nil, tr)
if err != nil {
return nil, nil, err
}

return tr.toTrackingTimeEntry(), tr.toStoppedTimeEntry(), nil
}

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

return tr.toStoppedTimeEntry(), nil
}

0 comments on commit 626a7fc

Please sign in to comment.