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" "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" { if len(main) == 0 || main[0] != "add" {
return nil, ErrWrongCommand 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) 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 { if val, ok := fields[FieldDate]; ok {
d := item.NewDateFromString(val) d := item.NewDateFromString(val)
if d.IsZero() { if d.IsZero() {
return nil, fmt.Errorf("%w: could not parse date", ErrInvalidArg) return nil, fmt.Errorf("%w: could not parse date", ErrInvalidArg)
} }
params.Date = d tsk.Date = d
} }
if val, ok := fields[FieldTime]; ok { if val, ok := fields[FieldTime]; ok {
t := item.NewTimeFromString(val) t := item.NewTimeFromString(val)
if t.IsZero() { if t.IsZero() {
return nil, fmt.Errorf("%w: could not parse time", ErrInvalidArg) return nil, fmt.Errorf("%w: could not parse time", ErrInvalidArg)
} }
params.Time = t tsk.Time = t
} }
if val, ok := fields[FieldDuration]; ok { if val, ok := fields[FieldDuration]; ok {
d, err := time.ParseDuration(val) d, err := time.ParseDuration(val)
if err != nil { if err != nil {
return nil, fmt.Errorf("%w: could not parse duration", ErrInvalidArg) return nil, fmt.Errorf("%w: could not parse duration", ErrInvalidArg)
} }
params.Duration = d tsk.Duration = d
} }
if val, ok := fields[FieldRecurrer]; ok { if val, ok := fields[FieldRecurrer]; ok {
rec := item.NewRecurrer(val) rec := item.NewRecurrer(val)
if rec == nil { if rec == nil {
return nil, fmt.Errorf("%w: could not parse recurrer", ErrInvalidArg) return nil, fmt.Errorf("%w: could not parse recurrer", ErrInvalidArg)
} }
params.Recurrer = rec tsk.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() tsk.RecurNext = tsk.Recurrer.First()
} }
return &Store{ return &Store{
deps: deps, task: tsk,
params: params,
}, nil }, nil
} }

View File

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

View File

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

View File

@ -4,19 +4,9 @@ import (
"fmt" "fmt"
"strconv" "strconv"
"strings" "strings"
"go-mod.ewintr.nl/planner/plan/storage"
) )
type Update struct { func NewUpdate(main []string, fields map[string]string) (Command, error) {
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 {
return &Update{ return &Update{
localIDRepo: localIDRepo, localIDRepo: localIDRepo,
taskRepo: taskRepo, taskRepo: taskRepo,