proper add
This commit is contained in:
parent
de7f5b8c74
commit
30e6b5fec0
|
@ -1,6 +1,7 @@
|
||||||
package command
|
package command
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -9,6 +10,10 @@ import (
|
||||||
"go-mod.ewintr.nl/planner/plan/storage"
|
"go-mod.ewintr.nl/planner/plan/storage"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ErrInvalidArg = errors.New("invalid argument")
|
||||||
|
)
|
||||||
|
|
||||||
var AddCmd = &cli.Command{
|
var AddCmd = &cli.Command{
|
||||||
Name: "add",
|
Name: "add",
|
||||||
Usage: "Add a new event",
|
Usage: "Add a new event",
|
||||||
|
@ -17,11 +22,13 @@ var AddCmd = &cli.Command{
|
||||||
Name: "name",
|
Name: "name",
|
||||||
Aliases: []string{"n"},
|
Aliases: []string{"n"},
|
||||||
Usage: "The event that will happen",
|
Usage: "The event that will happen",
|
||||||
|
Required: true,
|
||||||
},
|
},
|
||||||
&cli.StringFlag{
|
&cli.StringFlag{
|
||||||
Name: "on",
|
Name: "on",
|
||||||
Aliases: []string{"o"},
|
Aliases: []string{"o"},
|
||||||
Usage: "The date, in YYYY-MM-DD format",
|
Usage: "The date, in YYYY-MM-DD format",
|
||||||
|
Required: true,
|
||||||
},
|
},
|
||||||
&cli.StringFlag{
|
&cli.StringFlag{
|
||||||
Name: "at",
|
Name: "at",
|
||||||
|
@ -37,29 +44,49 @@ var AddCmd = &cli.Command{
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewAddCmd(repo storage.EventRepo) *cli.Command {
|
func NewAddCmd(repo storage.EventRepo) *cli.Command {
|
||||||
AddCmd.Action = NewAddAction(repo)
|
AddCmd.Action = func(cCtx *cli.Context) error {
|
||||||
|
return Add(cCtx.String("name"), cCtx.String("on"), cCtx.String("at"), cCtx.String("for"), repo)
|
||||||
|
}
|
||||||
return AddCmd
|
return AddCmd
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewAddAction(repo storage.EventRepo) func(*cli.Context) error {
|
func Add(nameStr, onStr, atStr, frStr string, repo storage.EventRepo) error {
|
||||||
return func(cCtx *cli.Context) error {
|
if nameStr == "" {
|
||||||
desc := cCtx.String("name")
|
return fmt.Errorf("%w: name is required", ErrInvalidArg)
|
||||||
date, err := time.Parse("2006-01-02", cCtx.String("date"))
|
}
|
||||||
if err != nil {
|
if onStr == "" {
|
||||||
return fmt.Errorf("could not parse date: %v", err)
|
return fmt.Errorf("%w: date is required", ErrInvalidArg)
|
||||||
}
|
}
|
||||||
|
|
||||||
one := item.Event{
|
startFormat := "2006-01-02"
|
||||||
|
startStr := onStr
|
||||||
|
if atStr != "" {
|
||||||
|
startFormat = fmt.Sprintf("%s HH:MM", startFormat)
|
||||||
|
startStr = fmt.Sprintf("%s %s", startStr, atStr)
|
||||||
|
}
|
||||||
|
start, err := time.Parse(startFormat, startStr)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("%w: could not parse start time and date: %v", ErrInvalidArg, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
e := item.Event{
|
||||||
ID: "a",
|
ID: "a",
|
||||||
EventBody: item.EventBody{
|
EventBody: item.EventBody{
|
||||||
Title: desc,
|
Title: nameStr,
|
||||||
Start: date,
|
Start: start,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
if err := repo.Store(one); err != nil {
|
|
||||||
|
if frStr != "" {
|
||||||
|
fr, err := time.ParseDuration(frStr)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("%w: could not parse time: %s", ErrInvalidArg, err)
|
||||||
|
}
|
||||||
|
e.Duration = fr
|
||||||
|
}
|
||||||
|
if err := repo.Store(e); err != nil {
|
||||||
return fmt.Errorf("could not store event: %v", err)
|
return fmt.Errorf("could not store event: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
|
@ -0,0 +1,10 @@
|
||||||
|
package command_test
|
||||||
|
|
||||||
|
func TestAdd(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
for _, tc := range []struct{
|
||||||
|
name string
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue