From 3315050f3738b43069af4dfd3e21649f0f1dbe27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ulrich=20Liss=C3=A9?= Date: Mon, 8 Apr 2019 17:44:00 +0200 Subject: [PATCH] Introduce Id value objects --- cmd/entries.go | 49 +++++++++++++++++++++++++++++++++----------- domain/account.go | 21 +++++++++++++++++++ domain/customer.go | 21 +++++++++++++++++++ domain/project.go | 23 ++++++++++++++++++++- domain/service.go | 23 ++++++++++++++++++++- domain/time_entry.go | 14 ++++++------- domain/user.go | 21 +++++++++++++++++++ mite/project.go | 3 +-- mite/service.go | 3 +-- mite/time_entry.go | 21 +++++++++---------- 10 files changed, 163 insertions(+), 36 deletions(-) diff --git a/cmd/entries.go b/cmd/entries.go index daa9b09..204ccef 100644 --- a/cmd/entries.go +++ b/cmd/entries.go @@ -114,9 +114,8 @@ var entriesCreateCommand = &cobra.Command{ Use: "create", Short: "creates a time entry", RunE: func(cmd *cobra.Command, args []string) error { - projectId, servicesId := servicesAndProjectId() - - if projectId == "" || servicesId == "" { + projectId, serviceId := projectAndServiceId() + if projectId == "" || serviceId == "" { return errors.New("please set both the project AND service id (either via arguments or config)") } @@ -128,13 +127,21 @@ var entriesCreateCommand = &cobra.Command{ if err != nil { return err } + cProjectId, err := domain.ParseProjectId(projectId) + if err != nil { + return err + } + cServiceId, err := domain.ParseServiceId(serviceId) + if err != nil { + return err + } timeEntry := domain.TimeEntryCommand{ Date: &cDate, Minutes: &cMinutes, Note: createNote, - ProjectId: projectId, - ServiceId: servicesId, + ProjectId: cProjectId, + ServiceId: cServiceId, } entry, err := deps.miteApi.CreateTimeEntry(&timeEntry) @@ -147,7 +154,7 @@ var entriesCreateCommand = &cobra.Command{ }, } -func servicesAndProjectId() (projectId, servicesId string) { +func projectAndServiceId() (projectId, servicesId string) { if createProjectId == "" && createActivity != "" { activity := deps.conf.GetActivity(createActivity) createProjectId = activity.ProjectId @@ -215,16 +222,34 @@ var entriesEditCommand = &cobra.Command{ if editActivity != "" { activity := deps.conf.GetActivity(editActivity) - command.ProjectId = activity.ProjectId - command.ServiceId = activity.ServiceId + + projectId, err := domain.ParseProjectId(activity.ProjectId) + if err != nil { + return err + } + command.ProjectId = projectId + + serviceId, err := domain.ParseServiceId(activity.ServiceId) + if err != nil { + return err + } + command.ServiceId = serviceId } - if editProjectId != "" && command.ProjectId == "" { - command.ProjectId = editProjectId + if editProjectId != "" && command.ProjectId == 0 { + projectId, err := domain.ParseProjectId(editProjectId) + if err != nil { + return err + } + command.ProjectId = projectId } - if editServiceId != "" && command.ProjectId == "" { - command.ServiceId = editServiceId + if editServiceId != "" && command.ProjectId == 0 { + serviceId, err := domain.ParseServiceId(editServiceId) + if err != nil { + return err + } + command.ServiceId = serviceId } err = deps.miteApi.EditTimeEntry(entryId, &command) diff --git a/domain/account.go b/domain/account.go index 3ea5c82..34c4832 100644 --- a/domain/account.go +++ b/domain/account.go @@ -1,3 +1,24 @@ package domain +import "strconv" + +type AccountId int + +func NewAccountId(i int) AccountId { + return AccountId(i) +} + +func ParseAccountId(s string) (AccountId, error) { + i, err := strconv.Atoi(s) + if err != nil { + return 0, err + } + + return NewAccountId(i), nil +} + +func (i AccountId) String() string { + return strconv.Itoa(int(i)) +} + type AccountApi interface{} diff --git a/domain/customer.go b/domain/customer.go index 1fb6f64..a87fe98 100644 --- a/domain/customer.go +++ b/domain/customer.go @@ -1,3 +1,24 @@ package domain +import "strconv" + +type CustomerId int + +func NewCustomerId(i int) CustomerId { + return CustomerId(i) +} + +func ParseCustomerId(s string) (CustomerId, error) { + i, err := strconv.Atoi(s) + if err != nil { + return 0, err + } + + return NewCustomerId(i), nil +} + +func (i CustomerId) String() string { + return strconv.Itoa(int(i)) +} + type CustomerApi interface{} diff --git a/domain/project.go b/domain/project.go index 8b865bb..5182be0 100644 --- a/domain/project.go +++ b/domain/project.go @@ -1,7 +1,28 @@ package domain +import "strconv" + +type ProjectId int + +func NewProjectId(i int) ProjectId { + return ProjectId(i) +} + +func ParseProjectId(s string) (ProjectId, error) { + i, err := strconv.Atoi(s) + if err != nil { + return 0, err + } + + return NewProjectId(i), nil +} + +func (i ProjectId) String() string { + return strconv.Itoa(int(i)) +} + type Project struct { - Id string + Id ProjectId Name string Note string } diff --git a/domain/service.go b/domain/service.go index fbe0840..fca1018 100644 --- a/domain/service.go +++ b/domain/service.go @@ -1,7 +1,28 @@ package domain +import "strconv" + +type ServiceId int + +func NewServiceId(i int) ServiceId { + return ServiceId(i) +} + +func ParseServiceId(s string) (ServiceId, error) { + i, err := strconv.Atoi(s) + if err != nil { + return 0, err + } + + return NewServiceId(i), nil +} + +func (i ServiceId) String() string { + return strconv.Itoa(int(i)) +} + type Service struct { - Id string + Id ServiceId Name string Note string } diff --git a/domain/time_entry.go b/domain/time_entry.go index dc0e609..850c156 100644 --- a/domain/time_entry.go +++ b/domain/time_entry.go @@ -33,13 +33,13 @@ type TimeEntry struct { Locked bool Revenue float64 HourlyRate int - UserId string + UserId UserId UserName string - ProjectId string + ProjectId ProjectId ProjectName string - CustomerId string + CustomerId CustomerId CustomerName string - ServiceId string + ServiceId ServiceId ServiceName string CreatedAt time.Time UpdatedAt time.Time @@ -49,9 +49,9 @@ type TimeEntryCommand struct { Date *LocalDate Minutes *Minutes Note string - UserId string - ProjectId string - ServiceId string + UserId UserId + ProjectId ProjectId + ServiceId ServiceId Locked bool } diff --git a/domain/user.go b/domain/user.go index 4298df6..c9cdece 100644 --- a/domain/user.go +++ b/domain/user.go @@ -1,3 +1,24 @@ package domain +import "strconv" + +type UserId int + +func NewUserId(i int) UserId { + return UserId(i) +} + +func ParseUserId(s string) (UserId, error) { + i, err := strconv.Atoi(s) + if err != nil { + return 0, err + } + + return NewUserId(i), nil +} + +func (i UserId) String() string { + return strconv.Itoa(int(i)) +} + type UserApi interface{} diff --git a/mite/project.go b/mite/project.go index cdf01f4..00bf89f 100644 --- a/mite/project.go +++ b/mite/project.go @@ -1,7 +1,6 @@ package mite import ( - "fmt" "github.com/leanovate/mite-go/domain" ) @@ -15,7 +14,7 @@ type projectResponse struct { func (r *projectResponse) toProject() *domain.Project { return &domain.Project{ - Id: fmt.Sprintf("%d", r.Project.Id), + Id: domain.NewProjectId(r.Project.Id), Name: r.Project.Name, Note: r.Project.Note, } diff --git a/mite/service.go b/mite/service.go index 42d841d..43ca9c8 100644 --- a/mite/service.go +++ b/mite/service.go @@ -1,7 +1,6 @@ package mite import ( - "fmt" "github.com/leanovate/mite-go/domain" ) @@ -15,7 +14,7 @@ type serviceResponse struct { func (r *serviceResponse) toService() *domain.Service { return &domain.Service{ - Id: fmt.Sprintf("%d", r.Service.Id), + Id: domain.NewServiceId(r.Service.Id), Name: r.Service.Name, Note: r.Service.Note, } diff --git a/mite/time_entry.go b/mite/time_entry.go index 386fcb2..f3fdfc8 100644 --- a/mite/time_entry.go +++ b/mite/time_entry.go @@ -4,7 +4,6 @@ import ( "fmt" "github.com/leanovate/mite-go/domain" "net/url" - "strconv" "time" ) @@ -17,9 +16,9 @@ func fromCommand(c *domain.TimeEntryCommand) *timeEntryRequest { r.TimeEntry.Minutes = c.Minutes.Value() } r.TimeEntry.Note = c.Note - r.TimeEntry.UserId = c.UserId - r.TimeEntry.ProjectId = c.ProjectId - r.TimeEntry.ServiceId = c.ServiceId + r.TimeEntry.UserId = int(c.UserId) + r.TimeEntry.ProjectId = int(c.ProjectId) + r.TimeEntry.ServiceId = int(c.ServiceId) r.TimeEntry.Locked = c.Locked return r @@ -47,9 +46,9 @@ type timeEntryRequest struct { Date string `json:"date_at,omitempty"` Minutes int `json:"minutes,omitempty"` Note string `json:"note,omitempty"` - UserId string `json:"user_id,omitempty"` - ProjectId string `json:"project_id,omitempty"` - ServiceId string `json:"service_id,omitempty"` + UserId int `json:"user_id,omitempty"` + ProjectId int `json:"project_id,omitempty"` + ServiceId int `json:"service_id,omitempty"` Locked bool `json:"locked,omitempty"` } `json:"time_entry"` } @@ -92,13 +91,13 @@ func (r *timeEntryResponse) toTimeEntry() *domain.TimeEntry { Locked: r.TimeEntry.Locked, Revenue: r.TimeEntry.Revenue, HourlyRate: r.TimeEntry.HourlyRate, - UserId: strconv.Itoa(r.TimeEntry.UserId), + UserId: domain.NewUserId(r.TimeEntry.UserId), UserName: r.TimeEntry.UserName, - ProjectId: strconv.Itoa(r.TimeEntry.ProjectId), + ProjectId: domain.NewProjectId(r.TimeEntry.ProjectId), ProjectName: r.TimeEntry.ProjectName, - CustomerId: strconv.Itoa(r.TimeEntry.CustomerId), + CustomerId: domain.NewCustomerId(r.TimeEntry.CustomerId), CustomerName: r.TimeEntry.CustomerName, - ServiceId: strconv.Itoa(r.TimeEntry.ServiceId), + ServiceId: domain.NewServiceId(r.TimeEntry.ServiceId), ServiceName: r.TimeEntry.ServiceName, CreatedAt: r.TimeEntry.CreatedAt, UpdatedAt: r.TimeEntry.UpdatedAt,