item.valid

This commit is contained in:
Erik Winter 2024-11-24 09:57:10 +01:00
parent 6fc18bfa6e
commit 9aec6b1b63
4 changed files with 108 additions and 39 deletions

View File

@ -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
}

View File

@ -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)
}
})
}
}

View File

@ -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"

View File

@ -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)
}
}
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 nil
}
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 {