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"
)
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" {
return nil, ErrWrongCommand
}
@ -55,7 +59,37 @@ func NewAdd(main []string, fields map[string]string) (Command, error) {
tsk.RecurNext = tsk.Recurrer.First()
}
return &Store{
task: tsk,
return &Add{
args: AddArgs{
Task: tsk,
},
}, 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 (
"fmt"
"strconv"
"go-mod.ewintr.nl/planner/plan/storage"
)
type Delete struct {
localIDRepo storage.LocalID
taskRepo storage.Task
syncRepo storage.Sync
localID int
type DeleteArgs struct {
LocalID int
}
func NewDelete(localIDRepo storage.LocalID, taskRepo storage.Task, syncRepo storage.Sync) Command {
return &Delete{
localIDRepo: localIDRepo,
taskRepo: taskRepo,
syncRepo: syncRepo,
}
}
func (del *Delete) Execute(main []string, flags map[string]string) error {
func (da DeleteArgs) Parse(main []string, flags map[string]string) (Command, error) {
if len(main) < 2 || main[0] != "delete" {
return ErrWrongCommand
return nil, ErrWrongCommand
}
localID, err := strconv.Atoi(main[1])
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()
return &Delete{
args: DeleteArgs{
LocalID: localID,
},
}, nil
}
func (del *Delete) do() error {
type Delete struct {
args DeleteArgs
}
func (del *Delete) Do(deps Dependencies) error {
var id string
idMap, err := del.localIDRepo.FindAll()
idMap, err := deps.LocalIDRepo.FindAll()
if err != nil {
return fmt.Errorf("could not get local ids: %v", err)
}
for tskID, lid := range idMap {
if del.localID == lid {
if del.args.LocalID == lid {
id = tskID
}
}
@ -50,7 +44,7 @@ func (del *Delete) do() error {
return fmt.Errorf("could not find local id")
}
tsk, err := del.taskRepo.Find(id)
tsk, err := deps.TaskRepo.Find(id)
if err != nil {
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)
}
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)
}
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)
}
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)
}

View File

@ -2,36 +2,28 @@ package command
import (
"fmt"
"go-mod.ewintr.nl/planner/plan/storage"
)
type List struct {
localIDRepo storage.LocalID
taskRepo storage.Task
type ListArgs struct {
}
func NewList(localIDRepo storage.LocalID, taskRepo storage.Task) Command {
return &List{
localIDRepo: localIDRepo,
taskRepo: taskRepo,
}
}
func (list *List) Execute(main []string, flags map[string]string) error {
func (la ListArgs) Parse(main []string, flags map[string]string) (Command, error) {
if len(main) > 0 && main[0] != "list" {
return ErrWrongCommand
return nil, ErrWrongCommand
}
return list.do()
return &List{}, nil
}
func (list *List) do() error {
localIDs, err := list.localIDRepo.FindAll()
type List struct {
}
func (list *List) Do(deps Dependencies) error {
localIDs, err := deps.LocalIDRepo.FindAll()
if err != nil {
return fmt.Errorf("could not get local ids: %v", err)
}
all, err := list.taskRepo.FindAll()
all, err := deps.TaskRepo.FindAll()
if err != nil {
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"
"strconv"
"strings"
"time"
"go-mod.ewintr.nl/planner/item"
)
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{},
},
},
}
type UpdateArgs struct {
LocalID int
Title string
Date item.Date
Time item.Time
Duration time.Duration
Recurrer item.Recurrer
}
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" {
return ErrWrongCommand
return nil, ErrWrongCommand
}
localID, err := strconv.Atoi(main[1])
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
main = 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)
}
args := UpdateArgs{
LocalID: localID,
Title: strings.Join(main[2:], " "),
}
update.argSet = as
return update.do()
if val, ok := fields[FieldDate]; ok {
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
}
func (update *Update) do() error {
as := update.argSet
type Update struct {
args UpdateArgs
}
func (u *Update) Do(deps Dependencies) error {
var id string
idMap, err := update.localIDRepo.FindAll()
idMap, err := deps.LocalIDRepo.FindAll()
if err != nil {
return fmt.Errorf("could not get local ids: %v", err)
}
for tid, lid := range idMap {
if update.localID == lid {
if u.args.LocalID == lid {
id = tid
}
}
@ -66,32 +82,33 @@ func (update *Update) do() error {
return fmt.Errorf("could not find local id")
}
tsk, err := update.taskRepo.Find(id)
tsk, err := deps.TaskRepo.Find(id)
if err != nil {
return fmt.Errorf("could not find task")
}
if as.Main != "" {
tsk.Title = as.Main
if u.args.Title != "" {
tsk.Title = u.args.Title
}
if as.IsSet(FieldDate) {
tsk.Date = as.GetDate(FieldDate)
if !u.args.Date.IsZero() {
tsk.Date = u.args.Date
}
if as.IsSet(FieldTime) {
tsk.Time = as.GetTime(FieldTime)
if !u.args.Time.IsZero() {
tsk.Time = u.args.Time
}
if as.IsSet(FieldDuration) {
tsk.Duration = as.GetDuration(FieldDuration)
if u.args.Duration != 0 {
tsk.Duration = u.args.Duration
}
if as.IsSet(FieldRecurrer) {
tsk.Recurrer = as.GetRecurrer(FieldRecurrer)
if u.args.Recurrer != nil {
tsk.Recurrer = u.args.Recurrer
tsk.RecurNext = tsk.Recurrer.First()
}
if !tsk.Valid() {
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)
}
@ -99,7 +116,7 @@ func (update *Update) do() error {
if err != nil {
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)
}