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 8, 2019
2 parents 65123a2 + 5e22017 commit 284a0cb
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 139 deletions.
18 changes: 9 additions & 9 deletions cmd/entries.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"errors"
"fmt"
"github.com/cheynewallace/tabby"
"github.com/leanovate/mite-go/datetime"
"github.com/leanovate/mite-go/domain"
"github.com/leanovate/mite-go/mite"
"github.com/spf13/cobra"
"strings"
Expand All @@ -31,9 +31,9 @@ var (
)

func init() {
today := datetime.Today()
today := domain.Today()
defaultFrom := today.Add(0, 0, -7)
defaultMinutes := datetime.NewMinutes(0).String()
defaultMinutes := domain.NewMinutes(0).String()

// list
entriesListCommand.Flags().StringVarP(&listTo, "to", "t", today.String(), "list only entries until date (in YYYY-MM-DD format)")
Expand Down Expand Up @@ -75,11 +75,11 @@ var entriesListCommand = &cobra.Command{
RunE: func(cmd *cobra.Command, args []string) error {
direction := listOrder

to, err := datetime.ParseLocalDate(listTo)
to, err := domain.ParseLocalDate(listTo)
if err != nil {
return err
}
from, err := datetime.ParseLocalDate(listFrom)
from, err := domain.ParseLocalDate(listFrom)
if err != nil {
return err
}
Expand Down Expand Up @@ -121,11 +121,11 @@ var entriesCreateCommand = &cobra.Command{
return errors.New("please set both the project AND service id (either via arguments or config)")
}

cDate, err := datetime.ParseLocalDate(createDate)
cDate, err := domain.ParseLocalDate(createDate)
if err != nil {
return err
}
cMinutes, err := datetime.ParseMinutes(createMinutes)
cMinutes, err := domain.ParseMinutes(createMinutes)
if err != nil {
return err
}
Expand Down Expand Up @@ -190,15 +190,15 @@ var entriesEditCommand = &cobra.Command{

// override only fields affected by set parameters of edit
if editDate != "" {
eDate, err := datetime.ParseLocalDate(editDate)
eDate, err := domain.ParseLocalDate(editDate)
if err != nil {
return err
}
timeEntry.Date = &eDate
}

if editMinutes != "" {
eMinutes, err := datetime.ParseMinutes(editMinutes)
eMinutes, err := domain.ParseMinutes(editMinutes)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package cmd
import (
"errors"
"github.com/cheynewallace/tabby"
"github.com/leanovate/mite-go/datetime"
"github.com/leanovate/mite-go/domain"
"github.com/leanovate/mite-go/mite"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -102,7 +102,7 @@ var trackerStopCommand = &cobra.Command{
}

func fetchLatestTimeEntryForToday() (string, error) {
today := datetime.Today()
today := domain.Today()

entries, err := deps.miteApi.TimeEntries(&mite.TimeEntryQuery{
To: &today,
Expand Down
43 changes: 0 additions & 43 deletions datetime/local_date_test.go

This file was deleted.

32 changes: 0 additions & 32 deletions datetime/minutes.go

This file was deleted.

37 changes: 0 additions & 37 deletions datetime/minutes_test.go

This file was deleted.

33 changes: 31 additions & 2 deletions datetime/local_date.go → domain/datetime.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package datetime
package domain

import "time"
import (
"math"
"strings"
"time"
)

const ISO8601 = "2006-01-02"

Expand Down Expand Up @@ -32,3 +36,28 @@ func (d LocalDate) Add(years int, months int, days int) LocalDate {
func (d LocalDate) String() string {
return d.time.Format(ISO8601)
}

type Minutes struct {
duration time.Duration
}

func NewMinutes(i int) Minutes {
return Minutes{duration: time.Duration(i) * time.Minute}
}

func ParseMinutes(s string) (Minutes, error) {
d, err := time.ParseDuration(s)
if err != nil {
return Minutes{}, err
}

return Minutes{duration: d.Round(time.Minute)}, nil
}

func (m Minutes) Value() int {
return int(math.Min(m.duration.Minutes(), math.MaxInt32))
}

func (m Minutes) String() string {
return strings.TrimSuffix(m.duration.String(), "0s")
}
73 changes: 73 additions & 0 deletions domain/datetime_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package domain_test

import (
"github.com/leanovate/mite-go/domain"
"github.com/stretchr/testify/assert"
"testing"
"time"
)

func TestToday(t *testing.T) {
expected := time.Now().Local().Format("2006-01-02")
actual := domain.Today().String()

assert.Equal(t, expected, actual)
}

func TestParseLocalDate(t *testing.T) {
expected := domain.NewLocalDate(time.Date(1970, time.January, 1, 0, 0, 0, 0, time.Local))
actual, err := domain.ParseLocalDate("1970-01-01")

assert.Nil(t, err)
assert.Equal(t, expected, actual)

_, err = domain.ParseLocalDate("1970-01-01T00:00:00Z")

assert.IsType(t, &time.ParseError{}, err)
}

func TestLocalDate_Add(t *testing.T) {
expected := domain.NewLocalDate(time.Date(1971, time.February, 2, 0, 0, 0, 0, time.Local))
actual := domain.
NewLocalDate(time.Date(1970, time.January, 1, 0, 0, 0, 0, time.Local)).
Add(1, 1, 1)

assert.Equal(t, expected, actual)
}

func TestLocalDate_String(t *testing.T) {
expected := "1970-01-01"
actual := domain.NewLocalDate(time.Date(1970, time.January, 1, 0, 0, 0, 0, time.Local)).String()

assert.Equal(t, expected, actual)
}

func Test_ParseMinutes(t *testing.T) {
expected := domain.NewMinutes(23)
actual, err := domain.ParseMinutes("23m")

assert.Nil(t, err)
assert.Equal(t, expected, actual)

actual, err = domain.ParseMinutes("22m33s")

assert.Nil(t, err)
assert.Equal(t, expected, actual)

_, err = domain.ParseMinutes("1970-01-01")
assert.NotNil(t, err)
}

func TestMinutes_Value(t *testing.T) {
expected := 23
actual := domain.NewMinutes(23).Value()

assert.Equal(t, expected, actual)
}

func TestMinutes_String(t *testing.T) {
expected := "23m"
actual := domain.NewMinutes(23).String()

assert.Equal(t, expected, actual)
}
18 changes: 9 additions & 9 deletions mite/time_entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ package mite

import (
"fmt"
"github.com/leanovate/mite-go/datetime"
"github.com/leanovate/mite-go/domain"
"net/url"
"strconv"
"time"
)

type TimeEntry struct {
Id string
Minutes datetime.Minutes
Date datetime.LocalDate
Minutes domain.Minutes
Date domain.LocalDate
Note string
Billable bool
Locked bool
Expand All @@ -30,8 +30,8 @@ type TimeEntry struct {
}

type TimeEntryCommand struct {
Date *datetime.LocalDate
Minutes *datetime.Minutes
Date *domain.LocalDate
Minutes *domain.Minutes
Note string
UserId string
ProjectId string
Expand All @@ -57,8 +57,8 @@ func (c *TimeEntryCommand) toRequest() *timeEntryRequest {
}

type TimeEntryQuery struct {
From *datetime.LocalDate
To *datetime.LocalDate
From *domain.LocalDate
To *domain.LocalDate
Direction string
}

Expand Down Expand Up @@ -115,14 +115,14 @@ type timeEntryResponse struct {
}

func (r *timeEntryResponse) toTimeEntry() *TimeEntry {
d, err := datetime.ParseLocalDate(r.TimeEntry.Date)
d, err := domain.ParseLocalDate(r.TimeEntry.Date)
if err != nil {
panic(err)
}

return &TimeEntry{
Id: strconv.Itoa(r.TimeEntry.Id),
Minutes: datetime.NewMinutes(r.TimeEntry.Minutes),
Minutes: domain.NewMinutes(r.TimeEntry.Minutes),
Date: d,
Note: r.TimeEntry.Note,
Billable: r.TimeEntry.Billable,
Expand Down
Loading

0 comments on commit 284a0cb

Please sign in to comment.