This commit is contained in:
Erik Winter 2024-12-28 08:43:59 +01:00
parent ea3f62cd45
commit f7501eec1c
5 changed files with 135 additions and 148 deletions

View File

@ -9,7 +9,11 @@ import (
"go-mod.ewintr.nl/planner/item" "go-mod.ewintr.nl/planner/item"
) )
func NewAdd(main []string, fields map[string]string) (Command, error) { type AddArgs struct {
Task item.Task
}
func (aa AddArgs) Parse(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
} }
@ -55,7 +59,37 @@ func NewAdd(main []string, fields map[string]string) (Command, error) {
tsk.RecurNext = tsk.Recurrer.First() tsk.RecurNext = tsk.Recurrer.First()
} }
return &Store{ return &Add{
task: tsk, args: AddArgs{
Task: tsk,
},
}, nil }, nil
} }
type Add struct {
args AddArgs
}
func (a *Add) Do(deps Dependencies) error {
if err := deps.TaskRepo.Store(a.args.Task); err != nil {
return fmt.Errorf("could not store event: %v", err)
}
localID, err := deps.LocalIDRepo.Next()
if err != nil {
return fmt.Errorf("could not create next local id: %v", err)
}
if err := deps.LocalIDRepo.Store(a.args.Task.ID, localID); err != nil {
return fmt.Errorf("could not store local id: %v", err)
}
it, err := a.args.Task.Item()
if err != nil {
return fmt.Errorf("could not convert event to sync item: %v", err)
}
if err := deps.SyncRepo.Store(it); err != nil {
return fmt.Errorf("could not store sync item: %v", err)
}
return nil
}

View File

@ -3,46 +3,40 @@ package command
import ( import (
"fmt" "fmt"
"strconv" "strconv"
"go-mod.ewintr.nl/planner/plan/storage"
) )
type Delete struct { type DeleteArgs struct {
localIDRepo storage.LocalID LocalID int
taskRepo storage.Task
syncRepo storage.Sync
localID int
} }
func NewDelete(localIDRepo storage.LocalID, taskRepo storage.Task, syncRepo storage.Sync) Command { func (da DeleteArgs) Parse(main []string, flags map[string]string) (Command, error) {
return &Delete{
localIDRepo: localIDRepo,
taskRepo: taskRepo,
syncRepo: syncRepo,
}
}
func (del *Delete) Execute(main []string, flags map[string]string) error {
if len(main) < 2 || main[0] != "delete" { if len(main) < 2 || main[0] != "delete" {
return ErrWrongCommand return nil, ErrWrongCommand
} }
localID, err := strconv.Atoi(main[1]) localID, err := strconv.Atoi(main[1])
if err != nil { if err != nil {
return fmt.Errorf("not a local id: %v", main[1]) return nil, fmt.Errorf("not a local id: %v", main[1])
}
del.localID = localID
return del.do()
} }
func (del *Delete) do() error { return &Delete{
args: DeleteArgs{
LocalID: localID,
},
}, nil
}
type Delete struct {
args DeleteArgs
}
func (del *Delete) Do(deps Dependencies) error {
var id string var id string
idMap, err := del.localIDRepo.FindAll() idMap, err := deps.LocalIDRepo.FindAll()
if err != nil { if err != nil {
return fmt.Errorf("could not get local ids: %v", err) return fmt.Errorf("could not get local ids: %v", err)
} }
for tskID, lid := range idMap { for tskID, lid := range idMap {
if del.localID == lid { if del.args.LocalID == lid {
id = tskID id = tskID
} }
} }
@ -50,7 +44,7 @@ func (del *Delete) do() error {
return fmt.Errorf("could not find local id") return fmt.Errorf("could not find local id")
} }
tsk, err := del.taskRepo.Find(id) tsk, err := deps.TaskRepo.Find(id)
if err != nil { if err != nil {
return fmt.Errorf("could not get task: %v", err) return fmt.Errorf("could not get task: %v", err)
} }
@ -60,15 +54,15 @@ func (del *Delete) do() error {
return fmt.Errorf("could not convert task to sync item: %v", err) return fmt.Errorf("could not convert task to sync item: %v", err)
} }
it.Deleted = true it.Deleted = true
if err := del.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)
} }
if err := del.localIDRepo.Delete(id); err != nil { if err := deps.LocalIDRepo.Delete(id); err != nil {
return fmt.Errorf("could not delete local id: %v", err) return fmt.Errorf("could not delete local id: %v", err)
} }
if err := del.taskRepo.Delete(id); err != nil { if err := deps.TaskRepo.Delete(id); err != nil {
return fmt.Errorf("could not delete task: %v", err) return fmt.Errorf("could not delete task: %v", err)
} }

View File

@ -2,36 +2,28 @@ package command
import ( import (
"fmt" "fmt"
"go-mod.ewintr.nl/planner/plan/storage"
) )
type List struct { type ListArgs struct {
localIDRepo storage.LocalID
taskRepo storage.Task
} }
func NewList(localIDRepo storage.LocalID, taskRepo storage.Task) Command { func (la ListArgs) Parse(main []string, flags map[string]string) (Command, error) {
return &List{
localIDRepo: localIDRepo,
taskRepo: taskRepo,
}
}
func (list *List) Execute(main []string, flags map[string]string) error {
if len(main) > 0 && main[0] != "list" { if len(main) > 0 && main[0] != "list" {
return ErrWrongCommand return nil, ErrWrongCommand
} }
return list.do() return &List{}, nil
} }
func (list *List) do() error { type List struct {
localIDs, err := list.localIDRepo.FindAll() }
func (list *List) Do(deps Dependencies) error {
localIDs, err := deps.LocalIDRepo.FindAll()
if err != nil { if err != nil {
return fmt.Errorf("could not get local ids: %v", err) return fmt.Errorf("could not get local ids: %v", err)
} }
all, err := list.taskRepo.FindAll() all, err := deps.TaskRepo.FindAll()
if err != nil { if err != nil {
return err return err
} }

View File

@ -1,50 +0,0 @@
package command
import (
"fmt"
"time"
"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 {
task item.Task
}
func NewStore(task item.Task) *Store {
return &Store{
task: task,
}
}
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 := deps.LocalIDRepo.Next()
if err != nil {
return fmt.Errorf("could not create next local id: %v", err)
}
if err := deps.LocalIDRepo.Store(store.task.ID, localID); err != nil {
return fmt.Errorf("could not store local id: %v", err)
}
it, err := store.task.Item()
if err != nil {
return fmt.Errorf("could not convert event to sync item: %v", err)
}
if err := deps.SyncRepo.Store(it); err != nil {
return fmt.Errorf("could not store sync item: %v", err)
}
return nil
}

View File

@ -4,61 +4,77 @@ import (
"fmt" "fmt"
"strconv" "strconv"
"strings" "strings"
"time"
"go-mod.ewintr.nl/planner/item"
) )
func NewUpdate(main []string, fields map[string]string) (Command, error) { type UpdateArgs struct {
return &Update{ LocalID int
localIDRepo: localIDRepo, Title string
taskRepo: taskRepo, Date item.Date
syncRepo: syncRepo, Time item.Time
argSet: &ArgSet{ Duration time.Duration
Flags: map[string]Flag{ Recurrer item.Recurrer
FieldTitle: &FlagString{},
FieldDate: &FlagDate{},
FieldTime: &FlagTime{},
FieldDuration: &FlagDuration{},
FieldRecurrer: &FlagRecurrer{},
},
},
}
} }
func (update *Update) Execute(main []string, flags map[string]string) error { func (ua UpdateArgs) Parse(main []string, fields map[string]string) (Command, error) {
if len(main) < 2 || main[0] != "update" { if len(main) < 2 || main[0] != "update" {
return ErrWrongCommand return nil, ErrWrongCommand
} }
localID, err := strconv.Atoi(main[1]) localID, err := strconv.Atoi(main[1])
if err != nil { if err != nil {
return fmt.Errorf("not a local id: %v", main[1]) return nil, fmt.Errorf("not a local id: %v", main[1])
} }
update.localID = localID args := UpdateArgs{
main = main[2:] LocalID: localID,
Title: strings.Join(main[2:], " "),
as := update.argSet
as.Main = strings.Join(main, " ")
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)
}
}
update.argSet = as
return update.do()
} }
func (update *Update) do() error { if val, ok := fields[FieldDate]; ok {
as := update.argSet d := item.NewDateFromString(val)
if d.IsZero() {
return nil, fmt.Errorf("%w: could not parse date", ErrInvalidArg)
}
args.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)
}
args.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)
}
args.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)
}
args.Recurrer = rec
}
return &Update{args}, nil
}
type Update struct {
args UpdateArgs
}
func (u *Update) Do(deps Dependencies) error {
var id string var id string
idMap, err := update.localIDRepo.FindAll() idMap, err := deps.LocalIDRepo.FindAll()
if err != nil { if err != nil {
return fmt.Errorf("could not get local ids: %v", err) return fmt.Errorf("could not get local ids: %v", err)
} }
for tid, lid := range idMap { for tid, lid := range idMap {
if update.localID == lid { if u.args.LocalID == lid {
id = tid id = tid
} }
} }
@ -66,32 +82,33 @@ func (update *Update) do() error {
return fmt.Errorf("could not find local id") return fmt.Errorf("could not find local id")
} }
tsk, err := update.taskRepo.Find(id) tsk, err := deps.TaskRepo.Find(id)
if err != nil { if err != nil {
return fmt.Errorf("could not find task") return fmt.Errorf("could not find task")
} }
if as.Main != "" { if u.args.Title != "" {
tsk.Title = as.Main tsk.Title = u.args.Title
} }
if as.IsSet(FieldDate) { if !u.args.Date.IsZero() {
tsk.Date = as.GetDate(FieldDate) tsk.Date = u.args.Date
} }
if as.IsSet(FieldTime) { if !u.args.Time.IsZero() {
tsk.Time = as.GetTime(FieldTime) tsk.Time = u.args.Time
} }
if as.IsSet(FieldDuration) { if u.args.Duration != 0 {
tsk.Duration = as.GetDuration(FieldDuration) tsk.Duration = u.args.Duration
} }
if as.IsSet(FieldRecurrer) { if u.args.Recurrer != nil {
tsk.Recurrer = as.GetRecurrer(FieldRecurrer) tsk.Recurrer = u.args.Recurrer
tsk.RecurNext = tsk.Recurrer.First()
} }
if !tsk.Valid() { if !tsk.Valid() {
return fmt.Errorf("task is unvalid") return fmt.Errorf("task is unvalid")
} }
if err := update.taskRepo.Store(tsk); err != nil { if err := deps.TaskRepo.Store(tsk); err != nil {
return fmt.Errorf("could not store task: %v", err) return fmt.Errorf("could not store task: %v", err)
} }
@ -99,7 +116,7 @@ func (update *Update) do() error {
if err != nil { if err != nil {
return fmt.Errorf("could not convert task to sync item: %v", err) return fmt.Errorf("could not convert task to sync item: %v", err)
} }
if err := update.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)
} }