Skip to content

Commit

Permalink
Introduce Id value objects
Browse files Browse the repository at this point in the history
  • Loading branch information
Ulrich Lissé committed Apr 8, 2019
1 parent 08b7e2f commit 3315050
Show file tree
Hide file tree
Showing 10 changed files with 163 additions and 36 deletions.
49 changes: 37 additions & 12 deletions cmd/entries.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)")
}

Expand All @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down
21 changes: 21 additions & 0 deletions domain/account.go
Original file line number Diff line number Diff line change
@@ -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{}
21 changes: 21 additions & 0 deletions domain/customer.go
Original file line number Diff line number Diff line change
@@ -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{}
23 changes: 22 additions & 1 deletion domain/project.go
Original file line number Diff line number Diff line change
@@ -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
}
Expand Down
23 changes: 22 additions & 1 deletion domain/service.go
Original file line number Diff line number Diff line change
@@ -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
}
Expand Down
14 changes: 7 additions & 7 deletions domain/time_entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

Expand Down
21 changes: 21 additions & 0 deletions domain/user.go
Original file line number Diff line number Diff line change
@@ -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{}
3 changes: 1 addition & 2 deletions mite/project.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package mite

import (
"fmt"
"github.com/leanovate/mite-go/domain"
)

Expand All @@ -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,
}
Expand Down
3 changes: 1 addition & 2 deletions mite/service.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package mite

import (
"fmt"
"github.com/leanovate/mite-go/domain"
)

Expand All @@ -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,
}
Expand Down
21 changes: 10 additions & 11 deletions mite/time_entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"github.com/leanovate/mite-go/domain"
"net/url"
"strconv"
"time"
)

Expand All @@ -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
Expand Down Expand Up @@ -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"`
}
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 3315050

Please sign in to comment.