Compare commits

..

No commits in common. "ea3f62cd45759055181335b687b003d2e8a8d77e" and "2a62e6c335e1a6431b280fff7b45fe9fe89ff505" have entirely different histories.

6 changed files with 152 additions and 148 deletions

View File

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

View File

@ -1,28 +1,21 @@
package command package command
import ( import (
"errors"
"fmt" "fmt"
"strings" "strings"
"go-mod.ewintr.nl/planner/plan/storage"
) )
const ( const (
FieldTitle = "title" FlagTitle = "title"
FieldDate = "date" FlagOn = "on"
FieldTime = "time" FlagAt = "at"
FieldDuration = "duration" FlagFor = "for"
FieldRecurrer = "recurrer" FlagRec = "rec"
) )
type Dependencies struct {
LocalIDRepo storage.LocalID
TaskRepo storage.Task
SyncRepo storage.Sync
}
type Command interface { type Command interface {
Do(deps Dependencies) error Execute([]string, map[string]string) error
} }
type CLI struct { type CLI struct {
@ -30,21 +23,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")
} }

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,20 +4,30 @@ import (
"fmt" "fmt"
"strconv" "strconv"
"strings" "strings"
"go-mod.ewintr.nl/planner/plan/storage"
) )
func NewUpdate(main []string, fields map[string]string) (Command, error) { 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 {
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{
FieldTitle: &FlagString{}, FlagTitle: &FlagString{},
FieldDate: &FlagDate{}, FlagOn: &FlagDate{},
FieldTime: &FlagTime{}, FlagAt: &FlagTime{},
FieldDuration: &FlagDuration{}, FlagFor: &FlagDuration{},
FieldRecurrer: &FlagRecurrer{}, FlagRec: &FlagRecurrer{},
}, },
}, },
} }
@ -74,17 +84,17 @@ func (update *Update) do() error {
if as.Main != "" { if as.Main != "" {
tsk.Title = as.Main tsk.Title = as.Main
} }
if as.IsSet(FieldDate) { if as.IsSet(FlagOn) {
tsk.Date = as.GetDate(FieldDate) tsk.Date = as.GetDate(FlagOn)
} }
if as.IsSet(FieldTime) { if as.IsSet(FlagAt) {
tsk.Time = as.GetTime(FieldTime) tsk.Time = as.GetTime(FlagAt)
} }
if as.IsSet(FieldDuration) { if as.IsSet(FlagFor) {
tsk.Duration = as.GetDuration(FieldDuration) tsk.Duration = as.GetDuration(FlagFor)
} }
if as.IsSet(FieldRecurrer) { if as.IsSet(FlagRec) {
tsk.Recurrer = as.GetRecurrer(FieldRecurrer) tsk.Recurrer = as.GetRecurrer(FlagRec)
} }
if !tsk.Valid() { if !tsk.Valid() {

View File

@ -1 +0,0 @@
package service