Skip to content

Commit

Permalink
Extract TimeEntry model
Browse files Browse the repository at this point in the history
  • Loading branch information
Ulrich Lissé committed Apr 8, 2019
1 parent eb9fda3 commit 0616c0f
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 62 deletions.
13 changes: 6 additions & 7 deletions cmd/entries.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"github.com/cheynewallace/tabby"
"github.com/leanovate/mite-go/domain"
"github.com/leanovate/mite-go/mite"
"github.com/spf13/cobra"
"strings"
)
Expand Down Expand Up @@ -84,7 +83,7 @@ var entriesListCommand = &cobra.Command{
return err
}

entries, err := deps.miteApi.TimeEntries(&mite.TimeEntryQuery{
entries, err := deps.miteApi.TimeEntries(&domain.TimeEntryQuery{
To: &to,
From: &from,
Direction: direction,
Expand All @@ -98,7 +97,7 @@ var entriesListCommand = &cobra.Command{
},
}

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

timeEntry := mite.TimeEntryCommand{
timeEntry := domain.TimeEntryCommand{
Date: &cDate,
Minutes: &cMinutes,
Note: createNote,
Expand All @@ -143,7 +142,7 @@ var entriesCreateCommand = &cobra.Command{
return err
}

printEntries([]*mite.TimeEntry{entry})
printEntries([]*domain.TimeEntry{entry})
return nil
},
}
Expand Down Expand Up @@ -180,7 +179,7 @@ var entriesEditCommand = &cobra.Command{
}

// use retrieved values as defaults
timeEntry := mite.TimeEntryCommand{
timeEntry := domain.TimeEntryCommand{
Date: &entry.Date,
Minutes: &entry.Minutes,
Note: entry.Note,
Expand Down Expand Up @@ -233,7 +232,7 @@ var entriesEditCommand = &cobra.Command{
return err
}

printEntries([]*mite.TimeEntry{entry})
printEntries([]*domain.TimeEntry{entry})
return nil
},
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"errors"
"github.com/cheynewallace/tabby"
"github.com/leanovate/mite-go/domain"
"github.com/leanovate/mite-go/mite"
"github.com/spf13/cobra"
)

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

entries, err := deps.miteApi.TimeEntries(&mite.TimeEntryQuery{
entries, err := deps.miteApi.TimeEntries(&domain.TimeEntryQuery{
To: &today,
From: &today,
Direction: "desc",
Expand Down
40 changes: 40 additions & 0 deletions domain/time_entry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package domain

import "time"

type TimeEntry struct {
Id string
Minutes Minutes
Date LocalDate
Note string
Billable bool
Locked bool
Revenue float64
HourlyRate int
UserId string
UserName string
ProjectId string
ProjectName string
CustomerId string
CustomerName string
ServiceId string
ServiceName string
CreatedAt time.Time
UpdatedAt time.Time
}

type TimeEntryCommand struct {
Date *LocalDate
Minutes *Minutes
Note string
UserId string
ProjectId string
ServiceId string
Locked bool
}

type TimeEntryQuery struct {
From *LocalDate
To *LocalDate
Direction string
}
8 changes: 4 additions & 4 deletions mite/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ const userAgent = "mite-go/0.1 (+github.com/leanovate/mite-go)"
type AccountApi interface{}

type TimeEntryApi interface {
TimeEntries(query *TimeEntryQuery) ([]*TimeEntry, error)
TimeEntry(id string) (*TimeEntry, error)
CreateTimeEntry(command *TimeEntryCommand) (*TimeEntry, error)
EditTimeEntry(id string, command *TimeEntryCommand) error
TimeEntries(query *domain.TimeEntryQuery) ([]*domain.TimeEntry, error)
TimeEntry(id string) (*domain.TimeEntry, error)
CreateTimeEntry(command *domain.TimeEntryCommand) (*domain.TimeEntry, error)
EditTimeEntry(id string, command *domain.TimeEntryCommand) error
DeleteTimeEntry(id string) error
}

Expand Down
61 changes: 12 additions & 49 deletions mite/time_entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,7 @@ import (
"time"
)

type TimeEntry struct {
Id string
Minutes domain.Minutes
Date domain.LocalDate
Note string
Billable bool
Locked bool
Revenue float64
HourlyRate int
UserId string
UserName string
ProjectId string
ProjectName string
CustomerId string
CustomerName string
ServiceId string
ServiceName string
CreatedAt time.Time
UpdatedAt time.Time
}

type TimeEntryCommand struct {
Date *domain.LocalDate
Minutes *domain.Minutes
Note string
UserId string
ProjectId string
ServiceId string
Locked bool
}

func (c *TimeEntryCommand) toRequest() *timeEntryRequest {
func fromCommand(c *domain.TimeEntryCommand) *timeEntryRequest {
r := &timeEntryRequest{}
if c.Date != nil {
r.TimeEntry.Date = c.Date.String()
Expand All @@ -56,13 +25,7 @@ func (c *TimeEntryCommand) toRequest() *timeEntryRequest {
return r
}

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

func (q *TimeEntryQuery) toValues() url.Values {
func fromQuery(q *domain.TimeEntryQuery) url.Values {
v := url.Values{}
if q != nil {
if q.From != nil {
Expand Down Expand Up @@ -114,13 +77,13 @@ type timeEntryResponse struct {
} `json:"time_entry"`
}

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

return &TimeEntry{
return &domain.TimeEntry{
Id: strconv.Itoa(r.TimeEntry.Id),
Minutes: domain.NewMinutes(r.TimeEntry.Minutes),
Date: d,
Expand All @@ -142,22 +105,22 @@ func (r *timeEntryResponse) toTimeEntry() *TimeEntry {
}
}

func (a *api) TimeEntries(query *TimeEntryQuery) ([]*TimeEntry, error) {
func (a *api) TimeEntries(query *domain.TimeEntryQuery) ([]*domain.TimeEntry, error) {
var ter []timeEntryResponse
err := a.getParametrized("time_entries.json", query.toValues(), &ter)
err := a.getParametrized("time_entries.json", fromQuery(query), &ter)
if err != nil {
return nil, err
}

var timeEntries []*TimeEntry
var timeEntries []*domain.TimeEntry
for _, te := range ter {
timeEntries = append(timeEntries, te.toTimeEntry())
}

return timeEntries, nil
}

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

func (a *api) CreateTimeEntry(command *TimeEntryCommand) (*TimeEntry, error) {
func (a *api) CreateTimeEntry(command *domain.TimeEntryCommand) (*domain.TimeEntry, error) {
ter := timeEntryResponse{}
err := a.post("/time_entries.json", command.toRequest(), &ter)
err := a.post("/time_entries.json", fromCommand(command), &ter)
if err != nil {
return nil, err
}

return ter.toTimeEntry(), nil
}

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

func (a *api) DeleteTimeEntry(id string) error {
Expand Down

0 comments on commit 0616c0f

Please sign in to comment.