This commit is contained in:
Erik Winter 2024-12-27 11:27:59 +01:00
parent a02a8217d7
commit ea3f62cd45
4 changed files with 28 additions and 65 deletions

View File

@ -9,7 +9,7 @@ import (
"go-mod.ewintr.nl/planner/item"
)
func NewAdd(deps Dependencies, main []string, fields map[string]string) (Command, error) {
func NewAdd(main []string, fields map[string]string) (Command, error) {
if len(main) == 0 || main[0] != "add" {
return nil, ErrWrongCommand
}
@ -18,53 +18,44 @@ func NewAdd(deps Dependencies, main []string, fields map[string]string) (Command
return nil, fmt.Errorf("%w: title is required for add", ErrInvalidArg)
}
title := strings.Join(main, ",")
tsk := item.Task{
ID: uuid.New().String(),
TaskBody: item.TaskBody{
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
tsk.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
tsk.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
tsk.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.Recurrer = rec
tsk.RecurNext = tsk.Recurrer.First()
}
return &Store{
deps: deps,
params: params,
task: tsk,
}, nil
}

View File

@ -22,7 +22,7 @@ type Dependencies struct {
}
type Command interface {
Do() error
Do(deps Dependencies) error
}
type CLI struct {

View File

@ -4,7 +4,6 @@ import (
"fmt"
"time"
"github.com/google/uuid"
"go-mod.ewintr.nl/planner/item"
)
@ -17,50 +16,33 @@ type StoreParams struct {
}
type Store struct {
deps Dependencies
params StoreParams
task item.Task
}
func NewStore(deps Dependencies, params StoreParams) *Store {
func NewStore(task item.Task) *Store {
return &Store{
deps: deps,
params: params,
task: task,
}
}
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 {
func (store *Store) Do(deps Dependencies) error {
if err := deps.TaskRepo.Store(store.task); err != nil {
return fmt.Errorf("could not store event: %v", err)
}
localID, err := store.deps.LocalIDRepo.Next()
localID, err := 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 {
if err := deps.LocalIDRepo.Store(store.task.ID, localID); err != nil {
return fmt.Errorf("could not store local id: %v", err)
}
it, err := tsk.Item()
it, err := store.task.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 {
if err := deps.SyncRepo.Store(it); err != nil {
return fmt.Errorf("could not store sync item: %v", err)
}

View File

@ -4,30 +4,20 @@ import (
"fmt"
"strconv"
"strings"
"go-mod.ewintr.nl/planner/plan/storage"
)
type Update struct {
localIDRepo storage.LocalID
taskRepo storage.Task
syncRepo storage.Sync
argSet *ArgSet
localID int
}
func NewUpdate(localIDRepo storage.LocalID, taskRepo storage.Task, syncRepo storage.Sync) Command {
func NewUpdate(main []string, fields map[string]string) (Command, error) {
return &Update{
localIDRepo: localIDRepo,
taskRepo: taskRepo,
syncRepo: syncRepo,
argSet: &ArgSet{
Flags: map[string]Flag{
FieldTitle: &FlagString{},
FieldDate: &FlagDate{},
FieldTime: &FlagTime{},
FieldDuration: &FlagDuration{},
FieldRecurrer: &FlagRecurrer{},
FieldTitle: &FlagString{},
FieldDate: &FlagDate{},
FieldTime: &FlagTime{},
FieldDuration: &FlagDuration{},
FieldRecurrer: &FlagRecurrer{},
},
},
}