2024-09-30 07:34:40 +02:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-11-10 15:04:14 +01:00
|
|
|
"strings"
|
2024-09-30 07:34:40 +02:00
|
|
|
"time"
|
|
|
|
|
2024-10-01 07:36:31 +02:00
|
|
|
"github.com/google/uuid"
|
2024-09-30 07:34:40 +02:00
|
|
|
"go-mod.ewintr.nl/planner/item"
|
|
|
|
"go-mod.ewintr.nl/planner/plan/storage"
|
|
|
|
)
|
|
|
|
|
2024-11-06 07:29:29 +01:00
|
|
|
const (
|
|
|
|
FlagOn = "on"
|
|
|
|
FlagAt = "at"
|
|
|
|
FlagFor = "for"
|
|
|
|
)
|
|
|
|
|
2024-11-04 09:49:41 +01:00
|
|
|
type AddCmd struct {
|
|
|
|
localIDRepo storage.LocalID
|
|
|
|
eventRepo storage.Event
|
|
|
|
syncRepo storage.Sync
|
2024-11-10 15:04:14 +01:00
|
|
|
title string
|
|
|
|
flags map[string]Flag
|
2024-09-30 07:34:40 +02:00
|
|
|
}
|
|
|
|
|
2024-11-04 09:49:41 +01:00
|
|
|
func NewAddCmd(localRepo storage.LocalID, eventRepo storage.Event, syncRepo storage.Sync) Command {
|
|
|
|
return &AddCmd{
|
|
|
|
localIDRepo: localRepo,
|
|
|
|
eventRepo: eventRepo,
|
|
|
|
syncRepo: syncRepo,
|
2024-11-10 15:04:14 +01:00
|
|
|
flags: map[string]Flag{
|
|
|
|
FlagOn: &FlagDate{},
|
|
|
|
FlagAt: &FlagTime{},
|
|
|
|
FlagFor: &FlagDuration{},
|
|
|
|
},
|
2024-10-01 07:36:31 +02:00
|
|
|
}
|
2024-11-04 09:49:41 +01:00
|
|
|
}
|
|
|
|
|
2024-11-10 15:04:14 +01:00
|
|
|
func (add *AddCmd) Parse(as *ArgSet) error {
|
|
|
|
if len(as.Main) == 0 || as.Main[0] != "add " {
|
|
|
|
return ErrWrongCommand
|
2024-11-05 07:21:43 +01:00
|
|
|
}
|
2024-11-10 15:04:14 +01:00
|
|
|
add.title = strings.Join(as.Main[1:], " ")
|
|
|
|
for k := range add.flags {
|
|
|
|
if err := add.flags[k].Set(as.Flags[k]); err != nil {
|
|
|
|
return fmt.Errorf("could not set %s: %v", k, err)
|
|
|
|
}
|
2024-11-05 07:21:43 +01:00
|
|
|
}
|
2024-11-10 15:04:14 +01:00
|
|
|
if add.title == "" {
|
|
|
|
return fmt.Errorf("%w: title is required", ErrInvalidArg)
|
2024-10-01 07:36:31 +02:00
|
|
|
}
|
2024-11-10 15:04:14 +01:00
|
|
|
if !add.flags[FlagOn].IsSet() {
|
|
|
|
return fmt.Errorf("%w: date is required", ErrInvalidArg)
|
2024-10-01 07:36:31 +02:00
|
|
|
}
|
2024-11-10 15:04:14 +01:00
|
|
|
if !add.flags[FlagAt].IsSet() && add.flags[FlagFor].IsSet() {
|
|
|
|
return fmt.Errorf("%w: can not have duration without start time", ErrInvalidArg)
|
2024-10-01 07:36:31 +02:00
|
|
|
}
|
2024-11-10 15:04:14 +01:00
|
|
|
if !add.flags[FlagAt].IsSet() && !add.flags[FlagFor].IsSet() {
|
|
|
|
if err := add.flags[FlagFor].Set("24h"); err != nil {
|
|
|
|
return fmt.Errorf("could not set duration to 24 hours")
|
|
|
|
}
|
2024-10-01 07:36:31 +02:00
|
|
|
}
|
2024-11-05 07:21:43 +01:00
|
|
|
|
2024-11-10 15:04:14 +01:00
|
|
|
return nil
|
2024-11-05 07:21:43 +01:00
|
|
|
}
|
|
|
|
|
2024-11-10 15:04:14 +01:00
|
|
|
func (add *AddCmd) Do() error {
|
2024-10-01 07:36:31 +02:00
|
|
|
startFormat := "2006-01-02"
|
2024-11-07 07:25:02 +01:00
|
|
|
startStr := as.Flag(FlagOn)
|
|
|
|
if as.HasFlag(FlagAt) {
|
2024-10-01 07:36:31 +02:00
|
|
|
startFormat = fmt.Sprintf("%s 15:04", startFormat)
|
2024-11-07 07:25:02 +01:00
|
|
|
startStr = fmt.Sprintf("%s %s", startStr, as.Flag(FlagAt))
|
2024-10-01 07:36:31 +02:00
|
|
|
}
|
|
|
|
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{
|
2024-11-07 07:25:02 +01:00
|
|
|
Title: as.Main,
|
2024-10-01 07:36:31 +02:00
|
|
|
Start: start,
|
|
|
|
},
|
|
|
|
}
|
2024-09-30 07:34:40 +02:00
|
|
|
|
2024-11-07 07:25:02 +01:00
|
|
|
if as.HasFlag(FlagFor) {
|
|
|
|
fr, err := time.ParseDuration(as.Flag(FlagFor))
|
2024-10-01 07:36:31 +02:00
|
|
|
if err != nil {
|
2024-10-06 11:28:05 +02:00
|
|
|
return fmt.Errorf("%w: could not parse duration: %s", ErrInvalidArg, err)
|
2024-10-01 07:36:31 +02:00
|
|
|
}
|
|
|
|
e.Duration = fr
|
2024-09-30 07:34:40 +02:00
|
|
|
}
|
2024-11-06 07:29:29 +01:00
|
|
|
if err := add.eventRepo.Store(e); err != nil {
|
2024-10-01 07:36:31 +02:00
|
|
|
return fmt.Errorf("could not store event: %v", err)
|
|
|
|
}
|
|
|
|
|
2024-11-06 07:29:29 +01:00
|
|
|
localID, err := add.localIDRepo.Next()
|
2024-10-03 07:32:48 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not create next local id: %v", err)
|
|
|
|
}
|
2024-11-06 07:29:29 +01:00
|
|
|
if err := add.localIDRepo.Store(e.ID, localID); err != nil {
|
2024-10-03 07:32:48 +02:00
|
|
|
return fmt.Errorf("could not store local id: %v", err)
|
|
|
|
}
|
|
|
|
|
2024-10-07 11:11:18 +02:00
|
|
|
it, err := e.Item()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not convert event to sync item: %v", err)
|
|
|
|
}
|
2024-11-06 07:29:29 +01:00
|
|
|
if err := add.syncRepo.Store(it); err != nil {
|
2024-10-07 11:11:18 +02:00
|
|
|
return fmt.Errorf("could not store sync item: %v", err)
|
|
|
|
}
|
|
|
|
|
2024-10-01 07:36:31 +02:00
|
|
|
return nil
|
2024-09-30 07:34:40 +02:00
|
|
|
}
|