package command import ( "fmt" "strings" "time" "github.com/google/uuid" "go-mod.ewintr.nl/planner/item" ) func NewAdd(deps Dependencies, main []string, fields map[string]string) (Command, error) { if len(main) == 0 || main[0] != "add" { return nil, ErrWrongCommand } main = main[1:] if len(main) == 0 { return nil, fmt.Errorf("%w: title is required for add", ErrInvalidArg) } 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 } 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() } return &Store{ deps: deps, params: params, }, nil }