Compare commits

..

2 Commits

Author SHA1 Message Date
Erik Winter ea3f62cd45 wip 2024-12-27 11:27:59 +01:00
Erik Winter a02a8217d7 wip 2024-12-27 11:20:32 +01:00
6 changed files with 141 additions and 145 deletions

View File

@ -3,111 +3,59 @@ package command
import ( import (
"fmt" "fmt"
"strings" "strings"
"time"
"github.com/google/uuid" "github.com/google/uuid"
"go-mod.ewintr.nl/planner/item" "go-mod.ewintr.nl/planner/item"
"go-mod.ewintr.nl/planner/plan/storage"
) )
type Add struct { func NewAdd(main []string, fields map[string]string) (Command, error) {
localIDRepo storage.LocalID
taskRepo storage.Task
syncRepo storage.Sync
argSet *ArgSet
}
func NewAdd(localRepo storage.LocalID, taskRepo storage.Task, syncRepo storage.Sync) Command {
return &Add{
localIDRepo: localRepo,
taskRepo: taskRepo,
syncRepo: syncRepo,
argSet: &ArgSet{
Flags: map[string]Flag{
FlagOn: &FlagDate{},
FlagAt: &FlagTime{},
FlagFor: &FlagDuration{},
FlagRec: &FlagRecurrer{},
},
},
}
}
func (add *Add) Execute(main []string, flags map[string]string) error {
if len(main) == 0 || main[0] != "add" { if len(main) == 0 || main[0] != "add" {
return ErrWrongCommand return nil, ErrWrongCommand
}
as := add.argSet
if len(main) > 1 {
as.Main = strings.Join(main[1:], " ")
}
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)
}
}
if as.Main == "" {
return fmt.Errorf("%w: title is required", ErrInvalidArg)
}
if !as.IsSet(FlagOn) {
return fmt.Errorf("%w: date is required", ErrInvalidArg)
}
if !as.IsSet(FlagAt) && as.IsSet(FlagFor) {
return fmt.Errorf("%w: can not have duration without start time", ErrInvalidArg)
}
if as.IsSet(FlagAt) && !as.IsSet(FlagFor) {
if err := as.Flags[FlagFor].Set("1h"); err != nil {
return fmt.Errorf("could not set duration to one hour")
}
}
if !as.IsSet(FlagAt) && !as.IsSet(FlagFor) {
if err := as.Flags[FlagFor].Set("24h"); err != nil {
return fmt.Errorf("could not set duration to 24 hours")
} }
main = main[1:]
if len(main) == 0 {
return nil, fmt.Errorf("%w: title is required for add", ErrInvalidArg)
} }
return add.do()
}
func (add *Add) do() error {
as := add.argSet
rec := as.GetRecurrer(FlagRec)
tsk := item.Task{ tsk := item.Task{
ID: uuid.New().String(), ID: uuid.New().String(),
Date: as.GetDate(FlagOn),
Recurrer: rec,
TaskBody: item.TaskBody{ TaskBody: item.TaskBody{
Title: as.Main, Title: strings.Join(main, ","),
Time: as.GetTime(FlagAt),
Duration: as.GetDuration(FlagFor),
}, },
} }
if rec != nil {
tsk.RecurNext = rec.First()
}
if err := add.taskRepo.Store(tsk); err != nil { if val, ok := fields[FieldDate]; ok {
return fmt.Errorf("could not store event: %v", err) d := item.NewDateFromString(val)
if d.IsZero() {
return nil, fmt.Errorf("%w: could not parse date", ErrInvalidArg)
} }
tsk.Date = d
localID, err := add.localIDRepo.Next() }
if val, ok := fields[FieldTime]; ok {
t := item.NewTimeFromString(val)
if t.IsZero() {
return nil, fmt.Errorf("%w: could not parse time", ErrInvalidArg)
}
tsk.Time = t
}
if val, ok := fields[FieldDuration]; ok {
d, err := time.ParseDuration(val)
if err != nil { if err != nil {
return fmt.Errorf("could not create next local id: %v", err) return nil, fmt.Errorf("%w: could not parse duration", ErrInvalidArg)
} }
if err := add.localIDRepo.Store(tsk.ID, localID); err != nil { tsk.Duration = d
return fmt.Errorf("could not store local id: %v", err) }
if val, ok := fields[FieldRecurrer]; ok {
rec := item.NewRecurrer(val)
if rec == nil {
return nil, fmt.Errorf("%w: could not parse recurrer", ErrInvalidArg)
}
tsk.Recurrer = rec
tsk.RecurNext = tsk.Recurrer.First()
} }
it, err := tsk.Item() return &Store{
if err != nil { task: tsk,
return fmt.Errorf("could not convert event to sync item: %v", err) }, nil
}
if err := add.syncRepo.Store(it); err != nil {
return fmt.Errorf("could not store sync item: %v", err)
}
return nil
} }

View File

@ -33,7 +33,7 @@ func TestAdd(t *testing.T) {
name: "title missing", name: "title missing",
main: []string{"add"}, main: []string{"add"},
flags: map[string]string{ flags: map[string]string{
command.FlagOn: aDate.String(), command.FieldDate: aDate.String(),
}, },
expErr: true, expErr: true,
}, },
@ -46,7 +46,7 @@ func TestAdd(t *testing.T) {
name: "only date", name: "only date",
main: []string{"add", "title"}, main: []string{"add", "title"},
flags: map[string]string{ flags: map[string]string{
command.FlagOn: aDate.String(), command.FieldDate: aDate.String(),
}, },
expTask: item.Task{ expTask: item.Task{
ID: "title", ID: "title",
@ -61,9 +61,9 @@ func TestAdd(t *testing.T) {
name: "date, time and duration", name: "date, time and duration",
main: []string{"add", "title"}, main: []string{"add", "title"},
flags: map[string]string{ flags: map[string]string{
command.FlagOn: aDate.String(), command.FieldDate: aDate.String(),
command.FlagAt: aTime.String(), command.FieldTime: aTime.String(),
command.FlagFor: anHourStr, command.FieldDuration: anHourStr,
}, },
expTask: item.Task{ expTask: item.Task{
ID: "title", ID: "title",
@ -79,8 +79,8 @@ func TestAdd(t *testing.T) {
name: "date and duration", name: "date and duration",
main: []string{"add", "title"}, main: []string{"add", "title"},
flags: map[string]string{ flags: map[string]string{
command.FlagOn: aDate.String(), command.FieldDate: aDate.String(),
command.FlagFor: anHourStr, command.FieldDuration: anHourStr,
}, },
expErr: true, expErr: true,
}, },

View File

@ -1,21 +1,28 @@
package command package command
import ( import (
"errors"
"fmt" "fmt"
"strings" "strings"
"go-mod.ewintr.nl/planner/plan/storage"
) )
const ( const (
FlagTitle = "title" FieldTitle = "title"
FlagOn = "on" FieldDate = "date"
FlagAt = "at" FieldTime = "time"
FlagFor = "for" FieldDuration = "duration"
FlagRec = "rec" FieldRecurrer = "recurrer"
) )
type Dependencies struct {
LocalIDRepo storage.LocalID
TaskRepo storage.Task
SyncRepo storage.Sync
}
type Command interface { type Command interface {
Execute([]string, map[string]string) error Do(deps Dependencies) error
} }
type CLI struct { type CLI struct {
@ -23,21 +30,21 @@ type CLI struct {
} }
func (cli *CLI) Run(args []string) error { func (cli *CLI) Run(args []string) error {
main, flags, err := ParseFlags(args) // main, flags, err := ParseFlags(args)
if err != nil { // if err != nil {
return err // return err
} // }
for _, c := range cli.Commands { // for _, c := range cli.Commands {
err := c.Execute(main, flags) // err := c.Execute(main, flags)
switch { // switch {
case errors.Is(err, ErrWrongCommand): // case errors.Is(err, ErrWrongCommand):
continue // continue
case err != nil: // case err != nil:
return err // return err
default: // default:
return nil // return nil
} // }
} // }
return fmt.Errorf("could not find matching command") return fmt.Errorf("could not find matching command")
} }

50
plan/command/store.go Normal file
View File

@ -0,0 +1,50 @@
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,30 +4,20 @@ 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,
syncRepo: syncRepo, syncRepo: syncRepo,
argSet: &ArgSet{ argSet: &ArgSet{
Flags: map[string]Flag{ Flags: map[string]Flag{
FlagTitle: &FlagString{}, FieldTitle: &FlagString{},
FlagOn: &FlagDate{}, FieldDate: &FlagDate{},
FlagAt: &FlagTime{}, FieldTime: &FlagTime{},
FlagFor: &FlagDuration{}, FieldDuration: &FlagDuration{},
FlagRec: &FlagRecurrer{}, FieldRecurrer: &FlagRecurrer{},
}, },
}, },
} }
@ -84,17 +74,17 @@ func (update *Update) do() error {
if as.Main != "" { if as.Main != "" {
tsk.Title = as.Main tsk.Title = as.Main
} }
if as.IsSet(FlagOn) { if as.IsSet(FieldDate) {
tsk.Date = as.GetDate(FlagOn) tsk.Date = as.GetDate(FieldDate)
} }
if as.IsSet(FlagAt) { if as.IsSet(FieldTime) {
tsk.Time = as.GetTime(FlagAt) tsk.Time = as.GetTime(FieldTime)
} }
if as.IsSet(FlagFor) { if as.IsSet(FieldDuration) {
tsk.Duration = as.GetDuration(FlagFor) tsk.Duration = as.GetDuration(FieldDuration)
} }
if as.IsSet(FlagRec) { if as.IsSet(FieldRecurrer) {
tsk.Recurrer = as.GetRecurrer(FlagRec) tsk.Recurrer = as.GetRecurrer(FieldRecurrer)
} }
if !tsk.Valid() { if !tsk.Valid() {

1
plan/service/service.go Normal file
View File

@ -0,0 +1 @@
package service