planner/plan/command/add.go

108 lines
2.5 KiB
Go
Raw Normal View History

2024-09-30 07:34:40 +02:00
package command
import (
2024-10-01 07:36:31 +02:00
"errors"
2024-09-30 07:34:40 +02:00
"fmt"
"time"
2024-10-01 07:36:31 +02:00
"github.com/google/uuid"
2024-09-30 07:34:40 +02:00
"github.com/urfave/cli/v2"
"go-mod.ewintr.nl/planner/item"
"go-mod.ewintr.nl/planner/plan/storage"
)
2024-10-01 07:36:31 +02:00
var (
ErrInvalidArg = errors.New("invalid argument")
)
2024-09-30 07:34:40 +02:00
var AddCmd = &cli.Command{
Name: "add",
Usage: "Add a new event",
Flags: []cli.Flag{
&cli.StringFlag{
2024-10-01 07:36:31 +02:00
Name: "name",
Aliases: []string{"n"},
Usage: "The event that will happen",
Required: true,
2024-09-30 07:34:40 +02:00
},
&cli.StringFlag{
2024-10-01 07:36:31 +02:00
Name: "on",
Aliases: []string{"o"},
Usage: "The date, in YYYY-MM-DD format",
Required: true,
2024-09-30 07:34:40 +02:00
},
&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)",
},
},
}
2024-10-03 07:32:48 +02:00
func NewAddCmd(localRepo storage.LocalID, eventRepo storage.Event) *cli.Command {
2024-10-01 07:36:31 +02:00
AddCmd.Action = func(cCtx *cli.Context) error {
2024-10-03 07:32:48 +02:00
return Add(localRepo, eventRepo, cCtx.String("name"), cCtx.String("on"), cCtx.String("at"), cCtx.String("for"))
2024-10-01 07:36:31 +02:00
}
2024-09-30 07:34:40 +02:00
return AddCmd
}
2024-10-03 07:32:48 +02:00
func Add(localIDRepo storage.LocalID, eventRepo storage.Event, nameStr, onStr, atStr, frStr string) error {
2024-10-01 07:36:31 +02:00
if nameStr == "" {
return fmt.Errorf("%w: name is required", ErrInvalidArg)
}
if onStr == "" {
return fmt.Errorf("%w: date is required", ErrInvalidArg)
}
if atStr == "" && frStr != "" {
return fmt.Errorf("%w: can not have duration without start time", ErrInvalidArg)
}
if atStr == "" && frStr == "" {
frStr = "24h"
}
2024-09-30 07:34:40 +02:00
2024-10-01 07:36:31 +02:00
startFormat := "2006-01-02"
startStr := onStr
if atStr != "" {
startFormat = fmt.Sprintf("%s 15:04", 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: uuid.New().String(),
EventBody: item.EventBody{
Title: nameStr,
Start: start,
},
}
2024-09-30 07:34:40 +02:00
2024-10-01 07:36:31 +02:00
if frStr != "" {
fr, err := time.ParseDuration(frStr)
if err != nil {
return fmt.Errorf("%w: could not parse time: %s", ErrInvalidArg, err)
}
e.Duration = fr
2024-09-30 07:34:40 +02:00
}
2024-10-03 07:32:48 +02:00
if err := eventRepo.Store(e); err != nil {
2024-10-01 07:36:31 +02:00
return fmt.Errorf("could not store event: %v", err)
}
2024-10-03 07:32:48 +02:00
localID, err := localIDRepo.Next()
if err != nil {
return fmt.Errorf("could not create next local id: %v", err)
}
if err := localIDRepo.Store(e.ID, localID); err != nil {
return fmt.Errorf("could not store local id: %v", err)
}
2024-10-01 07:36:31 +02:00
return nil
2024-09-30 07:34:40 +02:00
}