From 9aec6b1b638651ab77fb811589b6a8c263de1a82 Mon Sep 17 00:00:00 2001 From: Erik Winter Date: Sun, 24 Nov 2024 09:57:10 +0100 Subject: [PATCH] item.valid --- item/event.go | 14 ++++++++ item/event_test.go | 69 ++++++++++++++++++++++++++++++++++++++++ plan/command/add_test.go | 2 +- plan/command/update.go | 62 ++++++++++++++---------------------- 4 files changed, 108 insertions(+), 39 deletions(-) diff --git a/item/event.go b/item/event.go index aa11e5d..5a7fbfc 100644 --- a/item/event.go +++ b/item/event.go @@ -86,3 +86,17 @@ func (e Event) Item() (Item, error) { Body: string(body), }, nil } + +func (e Event) Valid() bool { + if e.Title == "" { + return false + } + if e.Start.IsZero() || e.Start.Year() < 2024 { + return false + } + if e.Duration.Seconds() < 1 { + return false + } + + return true +} diff --git a/item/event_test.go b/item/event_test.go index 39c5fb3..2e78cda 100644 --- a/item/event_test.go +++ b/item/event_test.go @@ -132,3 +132,72 @@ func TestEventItem(t *testing.T) { }) } } + +func TestEventValidate(t *testing.T) { + t.Parallel() + + oneHour, err := time.ParseDuration("1h") + if err != nil { + t.Errorf("exp nil, got %v", err) + } + + for _, tc := range []struct { + name string + event item.Event + exp bool + }{ + { + name: "empty", + }, + { + name: "missing title", + event: item.Event{ + ID: "a", + EventBody: item.EventBody{ + Start: time.Date(2024, 9, 20, 8, 0, 0, 0, time.UTC), + Duration: oneHour, + }, + }, + }, + { + name: "no date", + event: item.Event{ + ID: "a", + EventBody: item.EventBody{ + Title: "title", + Start: time.Date(0, 0, 0, 8, 0, 0, 0, time.UTC), + Duration: oneHour, + }, + }, + }, + { + name: "no duration", + event: item.Event{ + ID: "a", + EventBody: item.EventBody{ + Title: "title", + Start: time.Date(2024, 9, 20, 8, 0, 0, 0, time.UTC), + }, + }, + }, + { + name: "valid", + event: item.Event{ + ID: "a", + EventBody: item.EventBody{ + Title: "title", + Start: time.Date(2024, 9, 20, 8, 0, 0, 0, time.UTC), + Duration: oneHour, + }, + }, + exp: true, + }, + } { + t.Run(tc.name, func(t *testing.T) { + if act := tc.event.Valid(); tc.exp != act { + t.Errorf("exp %v, got %v", tc.exp, act) + } + + }) + } +} diff --git a/plan/command/add_test.go b/plan/command/add_test.go index b239a65..aee120e 100644 --- a/plan/command/add_test.go +++ b/plan/command/add_test.go @@ -10,7 +10,7 @@ import ( "go-mod.ewintr.nl/planner/plan/storage/memory" ) -func TestAddParse(t *testing.T) { +func TestAdd(t *testing.T) { t.Parallel() aDateStr := "2024-11-02" diff --git a/plan/command/update.go b/plan/command/update.go index 87a377e..5c7c2e9 100644 --- a/plan/command/update.go +++ b/plan/command/update.go @@ -2,9 +2,9 @@ package command import ( "fmt" + "strings" "time" - "github.com/urfave/cli/v2" "go-mod.ewintr.nl/planner/plan/storage" ) @@ -17,7 +17,7 @@ type Update struct { func NewUpdate(localIDRepo storage.LocalID, eventRepo storage.Event, syncRepo storage.Sync) Command { return &Update{ - localIDRepo: localRepo, + localIDRepo: localIDRepo, eventRepo: eventRepo, syncRepo: syncRepo, argSet: &ArgSet{ @@ -31,44 +31,30 @@ func NewUpdate(localIDRepo storage.LocalID, eventRepo storage.Event, syncRepo st } } -var UpdateCmd = &cli.Command{ - Name: "update", - Usage: "Update an event", - Flags: []cli.Flag{ - &cli.IntFlag{ - Name: "localID", - Aliases: []string{"l"}, - Usage: "The local id of the event", - Required: true, - }, - &cli.StringFlag{ - Name: "name", - Aliases: []string{"n"}, - Usage: "The event that will happen", - }, - &cli.StringFlag{ - Name: "on", - Aliases: []string{"o"}, - Usage: "The date, in YYYY-MM-DD format", - }, - &cli.StringFlag{ - Name: "at", - Aliases: []string{"a"}, - Usage: "The time, in HH:MM format. If omitted, the event will last the whole day", - }, - &cli.StringFlag{ - Name: "for", - Aliases: []string{"f"}, - Usage: "The duration, in show format (e.g. 1h30m)", - }, - }, +func (update *Update) Parse(main []string, flags map[string]string) error { + if len(main) == 0 || main[0] != "update" { + return ErrWrongCommand + } + as := update.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) + } + } + + return nil } -func NewUpdateCmd(localRepo storage.LocalID, eventRepo storage.Event, syncRepo storage.Sync) *cli.Command { - UpdateCmd.Action = func(cCtx *cli.Context) error { - return Update(localRepo, eventRepo, syncRepo, cCtx.Int("localID"), cCtx.String("name"), cCtx.String("on"), cCtx.String("at"), cCtx.String("for")) - } - return UpdateCmd +func (update *Update) Do() error { + + return nil } func UpdateOld(localRepo storage.LocalID, eventRepo storage.Event, syncRepo storage.Sync, localID int, nameStr, onStr, atStr, frStr string) error {