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 (
"fmt"
"strings"
"time"
"github.com/google/uuid"
"go-mod.ewintr.nl/planner/item"
"go-mod.ewintr.nl/planner/plan/storage"
)
type Add struct {
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 {
func NewAdd(main []string, fields map[string]string) (Command, error) {
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{
ID: uuid.New().String(),
Date: as.GetDate(FlagOn),
Recurrer: rec,
ID: uuid.New().String(),
TaskBody: item.TaskBody{
Title: as.Main,
Time: as.GetTime(FlagAt),
Duration: as.GetDuration(FlagFor),
Title: strings.Join(main, ","),
},
}
if rec != nil {
tsk.RecurNext = rec.First()
if val, ok := fields[FieldDate]; ok {
d := item.NewDateFromString(val)
if d.IsZero() {
return nil, fmt.Errorf("%w: could not parse date", ErrInvalidArg)
}
tsk.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)
}
tsk.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)
}
tsk.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)
}
tsk.Recurrer = rec
tsk.RecurNext = tsk.Recurrer.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
return &Store{
task: tsk,
}, nil
}

View File

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

View File

@ -1,21 +1,28 @@
package command
import (
"errors"
"fmt"
"strings"
"go-mod.ewintr.nl/planner/plan/storage"
)
const (
FlagTitle = "title"
FlagOn = "on"
FlagAt = "at"
FlagFor = "for"
FlagRec = "rec"
FieldTitle = "title"
FieldDate = "date"
FieldTime = "time"
FieldDuration = "duration"
FieldRecurrer = "recurrer"
)
type Dependencies struct {
LocalIDRepo storage.LocalID
TaskRepo storage.Task
SyncRepo storage.Sync
}
type Command interface {
Execute([]string, map[string]string) error
Do(deps Dependencies) error
}
type CLI struct {
@ -23,21 +30,21 @@ type CLI struct {
}
func (cli *CLI) Run(args []string) error {
main, flags, err := ParseFlags(args)
if err != nil {
return err
}
for _, c := range cli.Commands {
err := c.Execute(main, flags)
switch {
case errors.Is(err, ErrWrongCommand):
continue
case err != nil:
return err
default:
return nil
}
}
// main, flags, err := ParseFlags(args)
// if err != nil {
// return err
// }
// for _, c := range cli.Commands {
// err := c.Execute(main, flags)
// switch {
// case errors.Is(err, ErrWrongCommand):
// continue
// case err != nil:
// return err
// default:
// return nil
// }
// }
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"
"strconv"
"strings"
"go-mod.ewintr.nl/planner/plan/storage"
)
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 {
func NewUpdate(main []string, fields map[string]string) (Command, error) {
return &Update{
localIDRepo: localIDRepo,
taskRepo: taskRepo,
syncRepo: syncRepo,
argSet: &ArgSet{
Flags: map[string]Flag{
FlagTitle: &FlagString{},
FlagOn: &FlagDate{},
FlagAt: &FlagTime{},
FlagFor: &FlagDuration{},
FlagRec: &FlagRecurrer{},
FieldTitle: &FlagString{},
FieldDate: &FlagDate{},
FieldTime: &FlagTime{},
FieldDuration: &FlagDuration{},
FieldRecurrer: &FlagRecurrer{},
},
},
}
@ -84,17 +74,17 @@ func (update *Update) do() error {
if as.Main != "" {
tsk.Title = as.Main
}
if as.IsSet(FlagOn) {
tsk.Date = as.GetDate(FlagOn)
if as.IsSet(FieldDate) {
tsk.Date = as.GetDate(FieldDate)
}
if as.IsSet(FlagAt) {
tsk.Time = as.GetTime(FlagAt)
if as.IsSet(FieldTime) {
tsk.Time = as.GetTime(FieldTime)
}
if as.IsSet(FlagFor) {
tsk.Duration = as.GetDuration(FlagFor)
if as.IsSet(FieldDuration) {
tsk.Duration = as.GetDuration(FieldDuration)
}
if as.IsSet(FlagRec) {
tsk.Recurrer = as.GetRecurrer(FlagRec)
if as.IsSet(FieldRecurrer) {
tsk.Recurrer = as.GetRecurrer(FieldRecurrer)
}
if !tsk.Valid() {

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

@ -0,0 +1 @@
package service