2024-09-30 07:34:40 +02:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-10-29 07:22:04 +01:00
|
|
|
"strings"
|
2024-09-30 07:34:40 +02:00
|
|
|
|
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-10-29 07:22:04 +01:00
|
|
|
type Add struct {
|
|
|
|
localIDRepo storage.LocalID
|
2024-12-24 08:00:23 +01:00
|
|
|
taskRepo storage.Task
|
2024-10-29 07:22:04 +01:00
|
|
|
syncRepo storage.Sync
|
|
|
|
argSet *ArgSet
|
2024-09-30 07:34:40 +02:00
|
|
|
}
|
|
|
|
|
2024-12-24 08:00:23 +01:00
|
|
|
func NewAdd(localRepo storage.LocalID, taskRepo storage.Task, syncRepo storage.Sync) Command {
|
2024-10-29 07:22:04 +01:00
|
|
|
return &Add{
|
|
|
|
localIDRepo: localRepo,
|
2024-12-24 08:00:23 +01:00
|
|
|
taskRepo: taskRepo,
|
2024-10-29 07:22:04 +01:00
|
|
|
syncRepo: syncRepo,
|
|
|
|
argSet: &ArgSet{
|
|
|
|
Flags: map[string]Flag{
|
2024-12-19 12:06:03 +01:00
|
|
|
FlagOn: &FlagDate{},
|
|
|
|
FlagAt: &FlagTime{},
|
|
|
|
FlagFor: &FlagDuration{},
|
|
|
|
FlagRec: &FlagRecurrer{},
|
2024-10-29 07:22:04 +01:00
|
|
|
},
|
|
|
|
},
|
2024-10-01 07:36:31 +02:00
|
|
|
}
|
2024-09-30 07:34:40 +02:00
|
|
|
}
|
|
|
|
|
2024-10-29 07:22:04 +01:00
|
|
|
func (add *Add) Execute(main []string, flags map[string]string) error {
|
|
|
|
if len(main) == 0 || main[0] != "add" {
|
|
|
|
return ErrWrongCommand
|
|
|
|
}
|
|
|
|
as := add.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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if as.Main == "" {
|
|
|
|
return fmt.Errorf("%w: title is required", ErrInvalidArg)
|
2024-10-01 07:36:31 +02:00
|
|
|
}
|
2024-10-29 07:22:04 +01:00
|
|
|
if !as.IsSet(FlagOn) {
|
2024-10-01 07:36:31 +02:00
|
|
|
return fmt.Errorf("%w: date is required", ErrInvalidArg)
|
|
|
|
}
|
2024-10-29 07:22:04 +01:00
|
|
|
if !as.IsSet(FlagAt) && as.IsSet(FlagFor) {
|
2024-10-01 07:36:31 +02:00
|
|
|
return fmt.Errorf("%w: can not have duration without start time", ErrInvalidArg)
|
|
|
|
}
|
2024-10-29 07:22:04 +01:00
|
|
|
if as.IsSet(FlagAt) && !as.IsSet(FlagFor) {
|
|
|
|
if err := as.Flags[FlagFor].Set("1h"); err != nil {
|
|
|
|
return fmt.Errorf("could not set duration to one hour")
|
|
|
|
}
|
2024-10-01 07:36:31 +02:00
|
|
|
}
|
2024-10-29 07:22:04 +01:00
|
|
|
if !as.IsSet(FlagAt) && !as.IsSet(FlagFor) {
|
|
|
|
if err := as.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-10-29 07:22:04 +01:00
|
|
|
|
|
|
|
return add.do()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (add *Add) do() error {
|
|
|
|
as := add.argSet
|
2024-12-19 12:06:03 +01:00
|
|
|
rec := as.GetRecurrer(FlagRec)
|
2024-12-24 08:00:23 +01:00
|
|
|
tsk := item.Task{
|
2024-12-19 12:06:03 +01:00
|
|
|
ID: uuid.New().String(),
|
|
|
|
Date: as.GetDate(FlagOn),
|
|
|
|
Recurrer: rec,
|
2024-12-24 08:00:23 +01:00
|
|
|
TaskBody: item.TaskBody{
|
2024-12-19 12:06:03 +01:00
|
|
|
Title: as.Main,
|
|
|
|
Time: as.GetTime(FlagAt),
|
|
|
|
Duration: as.GetDuration(FlagFor),
|
2024-10-01 07:36:31 +02:00
|
|
|
},
|
|
|
|
}
|
2024-12-19 12:06:03 +01:00
|
|
|
if rec != nil {
|
2024-12-24 08:00:23 +01:00
|
|
|
tsk.RecurNext = rec.First()
|
2024-12-01 10:22:47 +01:00
|
|
|
}
|
|
|
|
|
2024-12-24 08:00:23 +01:00
|
|
|
if err := add.taskRepo.Store(tsk); err != nil {
|
2024-10-01 07:36:31 +02:00
|
|
|
return fmt.Errorf("could not store event: %v", err)
|
|
|
|
}
|
|
|
|
|
2024-10-29 07:22:04 +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-12-24 08:00:23 +01:00
|
|
|
if err := add.localIDRepo.Store(tsk.ID, localID); err != nil {
|
2024-10-03 07:32:48 +02:00
|
|
|
return fmt.Errorf("could not store local id: %v", err)
|
|
|
|
}
|
|
|
|
|
2024-12-24 08:00:23 +01:00
|
|
|
it, err := tsk.Item()
|
2024-10-07 11:11:18 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not convert event to sync item: %v", err)
|
|
|
|
}
|
2024-10-29 07:22:04 +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
|
|
|
}
|