Skip to content

Commit 0616c0f

Browse files
author
Ulrich Lissé
committed
Extract TimeEntry model
1 parent eb9fda3 commit 0616c0f

File tree

5 files changed

+63
-62
lines changed

5 files changed

+63
-62
lines changed

cmd/entries.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"fmt"
66
"github.com/cheynewallace/tabby"
77
"github.com/leanovate/mite-go/domain"
8-
"github.com/leanovate/mite-go/mite"
98
"github.com/spf13/cobra"
109
"strings"
1110
)
@@ -84,7 +83,7 @@ var entriesListCommand = &cobra.Command{
8483
return err
8584
}
8685

87-
entries, err := deps.miteApi.TimeEntries(&mite.TimeEntryQuery{
86+
entries, err := deps.miteApi.TimeEntries(&domain.TimeEntryQuery{
8887
To: &to,
8988
From: &from,
9089
Direction: direction,
@@ -98,7 +97,7 @@ var entriesListCommand = &cobra.Command{
9897
},
9998
}
10099

101-
func printEntries(entries []*mite.TimeEntry) {
100+
func printEntries(entries []*domain.TimeEntry) {
102101
t := tabby.New()
103102
t.AddHeader("id", "notes", "date", "time", "project", "service")
104103
for _, entry := range entries {
@@ -130,7 +129,7 @@ var entriesCreateCommand = &cobra.Command{
130129
return err
131130
}
132131

133-
timeEntry := mite.TimeEntryCommand{
132+
timeEntry := domain.TimeEntryCommand{
134133
Date: &cDate,
135134
Minutes: &cMinutes,
136135
Note: createNote,
@@ -143,7 +142,7 @@ var entriesCreateCommand = &cobra.Command{
143142
return err
144143
}
145144

146-
printEntries([]*mite.TimeEntry{entry})
145+
printEntries([]*domain.TimeEntry{entry})
147146
return nil
148147
},
149148
}
@@ -180,7 +179,7 @@ var entriesEditCommand = &cobra.Command{
180179
}
181180

182181
// use retrieved values as defaults
183-
timeEntry := mite.TimeEntryCommand{
182+
timeEntry := domain.TimeEntryCommand{
184183
Date: &entry.Date,
185184
Minutes: &entry.Minutes,
186185
Note: entry.Note,
@@ -233,7 +232,7 @@ var entriesEditCommand = &cobra.Command{
233232
return err
234233
}
235234

236-
printEntries([]*mite.TimeEntry{entry})
235+
printEntries([]*domain.TimeEntry{entry})
237236
return nil
238237
},
239238
}

cmd/tracker.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"errors"
55
"github.com/cheynewallace/tabby"
66
"github.com/leanovate/mite-go/domain"
7-
"github.com/leanovate/mite-go/mite"
87
"github.com/spf13/cobra"
98
)
109

@@ -104,7 +103,7 @@ var trackerStopCommand = &cobra.Command{
104103
func fetchLatestTimeEntryForToday() (string, error) {
105104
today := domain.Today()
106105

107-
entries, err := deps.miteApi.TimeEntries(&mite.TimeEntryQuery{
106+
entries, err := deps.miteApi.TimeEntries(&domain.TimeEntryQuery{
108107
To: &today,
109108
From: &today,
110109
Direction: "desc",

domain/time_entry.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package domain
2+
3+
import "time"
4+
5+
type TimeEntry struct {
6+
Id string
7+
Minutes Minutes
8+
Date LocalDate
9+
Note string
10+
Billable bool
11+
Locked bool
12+
Revenue float64
13+
HourlyRate int
14+
UserId string
15+
UserName string
16+
ProjectId string
17+
ProjectName string
18+
CustomerId string
19+
CustomerName string
20+
ServiceId string
21+
ServiceName string
22+
CreatedAt time.Time
23+
UpdatedAt time.Time
24+
}
25+
26+
type TimeEntryCommand struct {
27+
Date *LocalDate
28+
Minutes *Minutes
29+
Note string
30+
UserId string
31+
ProjectId string
32+
ServiceId string
33+
Locked bool
34+
}
35+
36+
type TimeEntryQuery struct {
37+
From *LocalDate
38+
To *LocalDate
39+
Direction string
40+
}

mite/api.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ const userAgent = "mite-go/0.1 (+github.com/leanovate/mite-go)"
1515
type AccountApi interface{}
1616

1717
type TimeEntryApi interface {
18-
TimeEntries(query *TimeEntryQuery) ([]*TimeEntry, error)
19-
TimeEntry(id string) (*TimeEntry, error)
20-
CreateTimeEntry(command *TimeEntryCommand) (*TimeEntry, error)
21-
EditTimeEntry(id string, command *TimeEntryCommand) error
18+
TimeEntries(query *domain.TimeEntryQuery) ([]*domain.TimeEntry, error)
19+
TimeEntry(id string) (*domain.TimeEntry, error)
20+
CreateTimeEntry(command *domain.TimeEntryCommand) (*domain.TimeEntry, error)
21+
EditTimeEntry(id string, command *domain.TimeEntryCommand) error
2222
DeleteTimeEntry(id string) error
2323
}
2424

mite/time_entry.go

Lines changed: 12 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -8,38 +8,7 @@ import (
88
"time"
99
)
1010

11-
type TimeEntry struct {
12-
Id string
13-
Minutes domain.Minutes
14-
Date domain.LocalDate
15-
Note string
16-
Billable bool
17-
Locked bool
18-
Revenue float64
19-
HourlyRate int
20-
UserId string
21-
UserName string
22-
ProjectId string
23-
ProjectName string
24-
CustomerId string
25-
CustomerName string
26-
ServiceId string
27-
ServiceName string
28-
CreatedAt time.Time
29-
UpdatedAt time.Time
30-
}
31-
32-
type TimeEntryCommand struct {
33-
Date *domain.LocalDate
34-
Minutes *domain.Minutes
35-
Note string
36-
UserId string
37-
ProjectId string
38-
ServiceId string
39-
Locked bool
40-
}
41-
42-
func (c *TimeEntryCommand) toRequest() *timeEntryRequest {
11+
func fromCommand(c *domain.TimeEntryCommand) *timeEntryRequest {
4312
r := &timeEntryRequest{}
4413
if c.Date != nil {
4514
r.TimeEntry.Date = c.Date.String()
@@ -56,13 +25,7 @@ func (c *TimeEntryCommand) toRequest() *timeEntryRequest {
5625
return r
5726
}
5827

59-
type TimeEntryQuery struct {
60-
From *domain.LocalDate
61-
To *domain.LocalDate
62-
Direction string
63-
}
64-
65-
func (q *TimeEntryQuery) toValues() url.Values {
28+
func fromQuery(q *domain.TimeEntryQuery) url.Values {
6629
v := url.Values{}
6730
if q != nil {
6831
if q.From != nil {
@@ -114,13 +77,13 @@ type timeEntryResponse struct {
11477
} `json:"time_entry"`
11578
}
11679

117-
func (r *timeEntryResponse) toTimeEntry() *TimeEntry {
80+
func (r *timeEntryResponse) toTimeEntry() *domain.TimeEntry {
11881
d, err := domain.ParseLocalDate(r.TimeEntry.Date)
11982
if err != nil {
12083
panic(err)
12184
}
12285

123-
return &TimeEntry{
86+
return &domain.TimeEntry{
12487
Id: strconv.Itoa(r.TimeEntry.Id),
12588
Minutes: domain.NewMinutes(r.TimeEntry.Minutes),
12689
Date: d,
@@ -142,22 +105,22 @@ func (r *timeEntryResponse) toTimeEntry() *TimeEntry {
142105
}
143106
}
144107

145-
func (a *api) TimeEntries(query *TimeEntryQuery) ([]*TimeEntry, error) {
108+
func (a *api) TimeEntries(query *domain.TimeEntryQuery) ([]*domain.TimeEntry, error) {
146109
var ter []timeEntryResponse
147-
err := a.getParametrized("time_entries.json", query.toValues(), &ter)
110+
err := a.getParametrized("time_entries.json", fromQuery(query), &ter)
148111
if err != nil {
149112
return nil, err
150113
}
151114

152-
var timeEntries []*TimeEntry
115+
var timeEntries []*domain.TimeEntry
153116
for _, te := range ter {
154117
timeEntries = append(timeEntries, te.toTimeEntry())
155118
}
156119

157120
return timeEntries, nil
158121
}
159122

160-
func (a *api) TimeEntry(id string) (*TimeEntry, error) {
123+
func (a *api) TimeEntry(id string) (*domain.TimeEntry, error) {
161124
ter := timeEntryResponse{}
162125
err := a.get(fmt.Sprintf("/time_entries/%s.json", id), &ter)
163126
if err != nil {
@@ -167,18 +130,18 @@ func (a *api) TimeEntry(id string) (*TimeEntry, error) {
167130
return ter.toTimeEntry(), nil
168131
}
169132

170-
func (a *api) CreateTimeEntry(command *TimeEntryCommand) (*TimeEntry, error) {
133+
func (a *api) CreateTimeEntry(command *domain.TimeEntryCommand) (*domain.TimeEntry, error) {
171134
ter := timeEntryResponse{}
172-
err := a.post("/time_entries.json", command.toRequest(), &ter)
135+
err := a.post("/time_entries.json", fromCommand(command), &ter)
173136
if err != nil {
174137
return nil, err
175138
}
176139

177140
return ter.toTimeEntry(), nil
178141
}
179142

180-
func (a *api) EditTimeEntry(id string, command *TimeEntryCommand) error {
181-
return a.patch(fmt.Sprintf("/time_entries/%s.json", id), command.toRequest(), nil)
143+
func (a *api) EditTimeEntry(id string, command *domain.TimeEntryCommand) error {
144+
return a.patch(fmt.Sprintf("/time_entries/%s.json", id), fromCommand(command), nil)
182145
}
183146

184147
func (a *api) DeleteTimeEntry(id string) error {

0 commit comments

Comments
 (0)