Skip to content

Commit

Permalink
Introduce TimeEntryId
Browse files Browse the repository at this point in the history
  • Loading branch information
Ulrich Lissé committed Apr 8, 2019
1 parent ece452b commit faa5ecd
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 45 deletions.
38 changes: 24 additions & 14 deletions cmd/entries.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,13 +173,18 @@ var entriesEditCommand = &cobra.Command{
Use: "edit",
Short: "edits a time entry",
RunE: func(cmd *cobra.Command, args []string) error {
entry, err := deps.miteApi.TimeEntry(editTimeEntryId)
entryId, err := domain.ParseTimeEntryId(editTimeEntryId)
if err != nil {
return err
}

entry, err := deps.miteApi.TimeEntry(entryId)
if err != nil {
return err
}

// use retrieved values as defaults
timeEntry := domain.TimeEntryCommand{
command := domain.TimeEntryCommand{
Date: &entry.Date,
Minutes: &entry.Minutes,
Note: entry.Note,
Expand All @@ -193,41 +198,41 @@ var entriesEditCommand = &cobra.Command{
if err != nil {
return err
}
timeEntry.Date = &eDate
command.Date = &eDate
}

if editMinutes != "" {
eMinutes, err := domain.ParseMinutes(editMinutes)
if err != nil {
return err
}
timeEntry.Minutes = &eMinutes
command.Minutes = &eMinutes
}

if editNote != "" {
timeEntry.Note = editNote
command.Note = editNote
}

if editActivity != "" {
activity := deps.conf.GetActivity(editActivity)
timeEntry.ProjectId = activity.ProjectId
timeEntry.ServiceId = activity.ServiceId
command.ProjectId = activity.ProjectId
command.ServiceId = activity.ServiceId
}

if editProjectId != "" && timeEntry.ProjectId == "" {
timeEntry.ProjectId = editProjectId
if editProjectId != "" && command.ProjectId == "" {
command.ProjectId = editProjectId
}

if editServiceId != "" && timeEntry.ProjectId == "" {
timeEntry.ServiceId = editServiceId
if editServiceId != "" && command.ProjectId == "" {
command.ServiceId = editServiceId
}

err = deps.miteApi.EditTimeEntry(editTimeEntryId, &timeEntry)
err = deps.miteApi.EditTimeEntry(entryId, &command)
if err != nil {
return err
}

entry, err = deps.miteApi.TimeEntry(editTimeEntryId)
entry, err = deps.miteApi.TimeEntry(entryId)
if err != nil {
return err
}
Expand All @@ -241,6 +246,11 @@ var entriesDeleteCommand = &cobra.Command{
Use: "delete",
Short: "deletes a time entry",
RunE: func(cmd *cobra.Command, args []string) error {
return deps.miteApi.DeleteTimeEntry(deleteTimeEntryId)
entryId, err := domain.ParseTimeEntryId(editTimeEntryId)
if err != nil {
return err
}

return deps.miteApi.DeleteTimeEntry(entryId)
},
}
32 changes: 19 additions & 13 deletions cmd/tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,17 @@ var trackerStartCommand = &cobra.Command{
Use: "start",
Short: "starts the time tracker for a time entry",
RunE: func(cmd *cobra.Command, args []string) (err error) {
var entryId domain.TimeEntryId
if trackerTimeEntryId == "" {
trackerTimeEntryId, err = fetchLatestTimeEntryForToday()
if err != nil {
return err
}
entryId, err = fetchLatestTimeEntryForToday()
} else {
entryId, err = domain.ParseTimeEntryId(trackerTimeEntryId)
}
if err != nil {
return err
}

tracking, stopped, err := deps.miteApi.StartTracker(trackerTimeEntryId)
tracking, stopped, err := deps.miteApi.StartTracker(entryId)
if err != nil {
return err
}
Expand All @@ -79,14 +82,17 @@ var trackerStopCommand = &cobra.Command{
Use: "stop",
Short: "stops the time tracker for a time entry",
RunE: func(cmd *cobra.Command, args []string) (err error) {
var entryId domain.TimeEntryId
if trackerTimeEntryId == "" {
trackerTimeEntryId, err = fetchLatestTimeEntryForToday()
if err != nil {
return err
}
entryId, err = fetchLatestTimeEntryForToday()
} else {
entryId, err = domain.ParseTimeEntryId(trackerTimeEntryId)
}
if err != nil {
return err
}

stopped, err := deps.miteApi.StopTracker(trackerTimeEntryId)
stopped, err := deps.miteApi.StopTracker(entryId)
if err != nil {
return err
}
Expand All @@ -100,7 +106,7 @@ var trackerStopCommand = &cobra.Command{
},
}

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

entries, err := deps.miteApi.TimeEntries(&domain.TimeEntryQuery{
Expand All @@ -109,11 +115,11 @@ func fetchLatestTimeEntryForToday() (string, error) {
Direction: "desc",
})
if err != nil {
return "", err
return 0, err
}

if len(entries) == 0 {
return "", errors.New("no time entries for today found")
return 0, errors.New("no time entries for today found")
}

return entries[0].Id, nil
Expand Down
26 changes: 24 additions & 2 deletions domain/time_entry.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,31 @@
package domain

import "time"
import (
"strconv"
"time"
)

type TimeEntryId int

func NewTimeEntryId(i int) TimeEntryId {
return TimeEntryId(i)
}

func ParseTimeEntryId(s string) (TimeEntryId, error) {
i, err := strconv.Atoi(s)
if err != nil {
return 0, err
}

return NewTimeEntryId(i), nil
}

func (i TimeEntryId) String() string {
return strconv.Itoa(int(i))
}

type TimeEntry struct {
Id string
Id TimeEntryId
Minutes Minutes
Date LocalDate
Note string
Expand Down
4 changes: 2 additions & 2 deletions domain/tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package domain
import "time"

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

type StoppedTimeEntry struct {
Id string
Id TimeEntryId
Minutes Minutes
}
10 changes: 5 additions & 5 deletions mite/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ type AccountApi interface{}

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

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

type CustomerApi interface{}
Expand Down
8 changes: 4 additions & 4 deletions mite/time_entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (r *timeEntryResponse) toTimeEntry() *domain.TimeEntry {
}

return &domain.TimeEntry{
Id: strconv.Itoa(r.TimeEntry.Id),
Id: domain.NewTimeEntryId(r.TimeEntry.Id),
Minutes: domain.NewMinutes(r.TimeEntry.Minutes),
Date: d,
Note: r.TimeEntry.Note,
Expand Down Expand Up @@ -120,7 +120,7 @@ func (a *api) TimeEntries(query *domain.TimeEntryQuery) ([]*domain.TimeEntry, er
return timeEntries, nil
}

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

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

func (a *api) DeleteTimeEntry(id string) error {
func (a *api) DeleteTimeEntry(id domain.TimeEntryId) error {
return a.delete(fmt.Sprintf("/time_entries/%s.json", id), nil)
}
9 changes: 4 additions & 5 deletions mite/tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package mite
import (
"fmt"
"github.com/leanovate/mite-go/domain"
"strconv"
"time"
)

Expand All @@ -27,7 +26,7 @@ func (r *trackerResponse) toTrackingTimeEntry() *domain.TrackingTimeEntry {
}

return &domain.TrackingTimeEntry{
Id: strconv.Itoa(r.Tracker.TrackingTimeEntry.Id),
Id: domain.NewTimeEntryId(r.Tracker.TrackingTimeEntry.Id),
Minutes: domain.NewMinutes(r.Tracker.TrackingTimeEntry.Minutes),
Since: r.Tracker.TrackingTimeEntry.Since,
}
Expand All @@ -39,7 +38,7 @@ func (r *trackerResponse) toStoppedTimeEntry() *domain.StoppedTimeEntry {
}

return &domain.StoppedTimeEntry{
Id: strconv.Itoa(r.Tracker.StoppedTimeEntry.Id),
Id: domain.NewTimeEntryId(r.Tracker.StoppedTimeEntry.Id),
Minutes: domain.NewMinutes(r.Tracker.StoppedTimeEntry.Minutes),
}
}
Expand All @@ -54,7 +53,7 @@ func (a *api) Tracker() (*domain.TrackingTimeEntry, error) {
return tr.toTrackingTimeEntry(), nil
}

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

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

0 comments on commit faa5ecd

Please sign in to comment.