diff --git a/plan/command/add.go b/plan/command/add.go index 53c39b0..4c0d2be 100644 --- a/plan/command/add.go +++ b/plan/command/add.go @@ -3,111 +3,68 @@ package command import ( "fmt" "strings" + "time" "github.com/google/uuid" "go-mod.ewintr.nl/planner/item" - "go-mod.ewintr.nl/planner/plan/storage" ) -type Add struct { - localIDRepo storage.LocalID - taskRepo storage.Task - syncRepo storage.Sync - argSet *ArgSet -} - -func NewAdd(localRepo storage.LocalID, taskRepo storage.Task, syncRepo storage.Sync) Command { - return &Add{ - localIDRepo: localRepo, - taskRepo: taskRepo, - syncRepo: syncRepo, - argSet: &ArgSet{ - Flags: map[string]Flag{ - FlagOn: &FlagDate{}, - FlagAt: &FlagTime{}, - FlagFor: &FlagDuration{}, - FlagRec: &FlagRecurrer{}, - }, - }, - } -} - -func (add *Add) Execute(main []string, flags map[string]string) error { +func NewAdd(deps Dependencies, main []string, fields map[string]string) (Command, error) { if len(main) == 0 || main[0] != "add" { - return ErrWrongCommand + return nil, ErrWrongCommand } - as := add.argSet - if len(main) > 1 { - as.Main = strings.Join(main[1:], " ") - } - for k := range as.Flags { - v, ok := flags[k] - if !ok { - continue - } - if err := as.Set(k, v); err != nil { - return fmt.Errorf("could not set %s: %v", k, err) - } - } - if as.Main == "" { - return fmt.Errorf("%w: title is required", ErrInvalidArg) - } - if !as.IsSet(FlagOn) { - return fmt.Errorf("%w: date is required", ErrInvalidArg) - } - if !as.IsSet(FlagAt) && as.IsSet(FlagFor) { - return fmt.Errorf("%w: can not have duration without start time", ErrInvalidArg) - } - if as.IsSet(FlagAt) && !as.IsSet(FlagFor) { - if err := as.Flags[FlagFor].Set("1h"); err != nil { - return fmt.Errorf("could not set duration to one hour") - } - } - if !as.IsSet(FlagAt) && !as.IsSet(FlagFor) { - if err := as.Flags[FlagFor].Set("24h"); err != nil { - return fmt.Errorf("could not set duration to 24 hours") - } + main = main[1:] + if len(main) == 0 { + return nil, fmt.Errorf("%w: title is required for add", ErrInvalidArg) } - return add.do() -} + title := strings.Join(main, ",") + + if val, ok := fields[FieldDate]; ok { + d := item.NewDateFromString(val) + if d.IsZero() { + return nil, fmt.Errorf("%w: could not parse date", ErrInvalidArg) + } + params.Date = d + } + if val, ok := fields[FieldTime]; ok { + t := item.NewTimeFromString(val) + if t.IsZero() { + return nil, fmt.Errorf("%w: could not parse time", ErrInvalidArg) + } + params.Time = t + } + if val, ok := fields[FieldDuration]; ok { + d, err := time.ParseDuration(val) + if err != nil { + return nil, fmt.Errorf("%w: could not parse duration", ErrInvalidArg) + } + params.Duration = d + } + if val, ok := fields[FieldRecurrer]; ok { + rec := item.NewRecurrer(val) + if rec == nil { + return nil, fmt.Errorf("%w: could not parse recurrer", ErrInvalidArg) + } + params.Recurrer = rec + } -func (add *Add) do() error { - as := add.argSet - rec := as.GetRecurrer(FlagRec) tsk := item.Task{ ID: uuid.New().String(), - Date: as.GetDate(FlagOn), - Recurrer: rec, + Date: store.params.Date, + Recurrer: store.params.Recurrer, TaskBody: item.TaskBody{ - Title: as.Main, - Time: as.GetTime(FlagAt), - Duration: as.GetDuration(FlagFor), + Title: store.params.Title, + Time: store.params.Time, + Duration: store.params.Duration, }, } - if rec != nil { - tsk.RecurNext = rec.First() + if tsk.Recurrer != nil { + tsk.RecurNext = tsk.Recurrer.First() } - if err := add.taskRepo.Store(tsk); err != nil { - return fmt.Errorf("could not store event: %v", err) - } - - localID, err := add.localIDRepo.Next() - if err != nil { - return fmt.Errorf("could not create next local id: %v", err) - } - if err := add.localIDRepo.Store(tsk.ID, localID); err != nil { - return fmt.Errorf("could not store local id: %v", err) - } - - it, err := tsk.Item() - if err != nil { - return fmt.Errorf("could not convert event to sync item: %v", err) - } - if err := add.syncRepo.Store(it); err != nil { - return fmt.Errorf("could not store sync item: %v", err) - } - - return nil + return &Store{ + deps: deps, + params: params, + }, nil } diff --git a/plan/command/add_test.go b/plan/command/add_test.go index 6af038d..cb46462 100644 --- a/plan/command/add_test.go +++ b/plan/command/add_test.go @@ -33,7 +33,7 @@ func TestAdd(t *testing.T) { name: "title missing", main: []string{"add"}, flags: map[string]string{ - command.FlagOn: aDate.String(), + command.FieldDate: aDate.String(), }, expErr: true, }, @@ -46,7 +46,7 @@ func TestAdd(t *testing.T) { name: "only date", main: []string{"add", "title"}, flags: map[string]string{ - command.FlagOn: aDate.String(), + command.FieldDate: aDate.String(), }, expTask: item.Task{ ID: "title", @@ -61,9 +61,9 @@ func TestAdd(t *testing.T) { name: "date, time and duration", main: []string{"add", "title"}, flags: map[string]string{ - command.FlagOn: aDate.String(), - command.FlagAt: aTime.String(), - command.FlagFor: anHourStr, + command.FieldDate: aDate.String(), + command.FieldTime: aTime.String(), + command.FieldDuration: anHourStr, }, expTask: item.Task{ ID: "title", @@ -79,8 +79,8 @@ func TestAdd(t *testing.T) { name: "date and duration", main: []string{"add", "title"}, flags: map[string]string{ - command.FlagOn: aDate.String(), - command.FlagFor: anHourStr, + command.FieldDate: aDate.String(), + command.FieldDuration: anHourStr, }, expErr: true, }, diff --git a/plan/command/command.go b/plan/command/command.go index 6c1daa8..f353abc 100644 --- a/plan/command/command.go +++ b/plan/command/command.go @@ -1,21 +1,28 @@ package command import ( - "errors" "fmt" "strings" + + "go-mod.ewintr.nl/planner/plan/storage" ) const ( - FlagTitle = "title" - FlagOn = "on" - FlagAt = "at" - FlagFor = "for" - FlagRec = "rec" + FieldTitle = "title" + FieldDate = "date" + FieldTime = "time" + FieldDuration = "duration" + FieldRecurrer = "recurrer" ) +type Dependencies struct { + LocalIDRepo storage.LocalID + TaskRepo storage.Task + SyncRepo storage.Sync +} + type Command interface { - Execute([]string, map[string]string) error + Do() error } type CLI struct { @@ -23,21 +30,21 @@ type CLI struct { } func (cli *CLI) Run(args []string) error { - main, flags, err := ParseFlags(args) - if err != nil { - return err - } - for _, c := range cli.Commands { - err := c.Execute(main, flags) - switch { - case errors.Is(err, ErrWrongCommand): - continue - case err != nil: - return err - default: - return nil - } - } + // main, flags, err := ParseFlags(args) + // if err != nil { + // return err + // } + // for _, c := range cli.Commands { + // err := c.Execute(main, flags) + // switch { + // case errors.Is(err, ErrWrongCommand): + // continue + // case err != nil: + // return err + // default: + // return nil + // } + // } return fmt.Errorf("could not find matching command") } diff --git a/plan/command/store.go b/plan/command/store.go new file mode 100644 index 0000000..e76f60d --- /dev/null +++ b/plan/command/store.go @@ -0,0 +1,68 @@ +package command + +import ( + "fmt" + "time" + + "github.com/google/uuid" + "go-mod.ewintr.nl/planner/item" +) + +type StoreParams struct { + Title string + Date item.Date + Time item.Time + Duration time.Duration + Recurrer item.Recurrer +} + +type Store struct { + deps Dependencies + params StoreParams +} + +func NewStore(deps Dependencies, params StoreParams) *Store { + return &Store{ + deps: deps, + params: params, + } +} + +func (store *Store) Do() error { + tsk := item.Task{ + ID: uuid.New().String(), + Date: store.params.Date, + Recurrer: store.params.Recurrer, + TaskBody: item.TaskBody{ + Title: store.params.Title, + Time: store.params.Time, + Duration: store.params.Duration, + }, + } + if tsk.Recurrer != nil { + tsk.RecurNext = tsk.Recurrer.First() + } + // TODO check valid + + if err := store.deps.TaskRepo.Store(tsk); err != nil { + return fmt.Errorf("could not store event: %v", err) + } + + localID, err := store.deps.LocalIDRepo.Next() + if err != nil { + return fmt.Errorf("could not create next local id: %v", err) + } + if err := store.deps.LocalIDRepo.Store(tsk.ID, localID); err != nil { + return fmt.Errorf("could not store local id: %v", err) + } + + it, err := tsk.Item() + if err != nil { + return fmt.Errorf("could not convert event to sync item: %v", err) + } + if err := store.deps.SyncRepo.Store(it); err != nil { + return fmt.Errorf("could not store sync item: %v", err) + } + + return nil +} diff --git a/plan/command/update.go b/plan/command/update.go index 60a50a3..faad644 100644 --- a/plan/command/update.go +++ b/plan/command/update.go @@ -23,11 +23,11 @@ func NewUpdate(localIDRepo storage.LocalID, taskRepo storage.Task, syncRepo stor syncRepo: syncRepo, argSet: &ArgSet{ Flags: map[string]Flag{ - FlagTitle: &FlagString{}, - FlagOn: &FlagDate{}, - FlagAt: &FlagTime{}, - FlagFor: &FlagDuration{}, - FlagRec: &FlagRecurrer{}, + FieldTitle: &FlagString{}, + FieldDate: &FlagDate{}, + FieldTime: &FlagTime{}, + FieldDuration: &FlagDuration{}, + FieldRecurrer: &FlagRecurrer{}, }, }, } @@ -84,17 +84,17 @@ func (update *Update) do() error { if as.Main != "" { tsk.Title = as.Main } - if as.IsSet(FlagOn) { - tsk.Date = as.GetDate(FlagOn) + if as.IsSet(FieldDate) { + tsk.Date = as.GetDate(FieldDate) } - if as.IsSet(FlagAt) { - tsk.Time = as.GetTime(FlagAt) + if as.IsSet(FieldTime) { + tsk.Time = as.GetTime(FieldTime) } - if as.IsSet(FlagFor) { - tsk.Duration = as.GetDuration(FlagFor) + if as.IsSet(FieldDuration) { + tsk.Duration = as.GetDuration(FieldDuration) } - if as.IsSet(FlagRec) { - tsk.Recurrer = as.GetRecurrer(FlagRec) + if as.IsSet(FieldRecurrer) { + tsk.Recurrer = as.GetRecurrer(FieldRecurrer) } if !tsk.Valid() { diff --git a/plan/service/service.go b/plan/service/service.go new file mode 100644 index 0000000..6d43c33 --- /dev/null +++ b/plan/service/service.go @@ -0,0 +1 @@ +package service