planner/plan/command/update.go

125 lines
2.6 KiB
Go
Raw Normal View History

2024-10-06 11:28:05 +02:00
package command
import (
"fmt"
2024-11-25 09:34:31 +01:00
"strconv"
2024-11-24 09:57:10 +01:00
"strings"
2024-10-06 11:28:05 +02:00
"time"
"go-mod.ewintr.nl/planner/plan/storage"
)
2024-11-22 08:23:13 +01:00
type Update struct {
localIDRepo storage.LocalID
eventRepo storage.Event
syncRepo storage.Sync
argSet *ArgSet
2024-11-25 09:34:31 +01:00
localID int
2024-11-22 08:23:13 +01:00
}
func NewUpdate(localIDRepo storage.LocalID, eventRepo storage.Event, syncRepo storage.Sync) Command {
return &Update{
2024-11-24 09:57:10 +01:00
localIDRepo: localIDRepo,
2024-11-22 08:23:13 +01:00
eventRepo: eventRepo,
syncRepo: syncRepo,
argSet: &ArgSet{
Flags: map[string]Flag{
FlagTitle: &FlagString{},
FlagOn: &FlagDate{},
FlagAt: &FlagTime{},
FlagFor: &FlagDuration{},
},
},
}
}
2024-11-28 07:20:41 +01:00
func (update *Update) Execute(main []string, flags map[string]string) error {
2024-11-25 09:34:31 +01:00
if len(main) < 2 || main[0] != "update" {
2024-11-24 09:57:10 +01:00
return ErrWrongCommand
}
2024-11-25 09:34:31 +01:00
localID, err := strconv.Atoi(main[1])
if err != nil {
return fmt.Errorf("not a local id: %v", main[1])
}
update.localID = localID
2024-11-27 07:30:52 +01:00
main = main[2:]
2024-11-25 09:34:31 +01:00
2024-11-24 09:57:10 +01:00
as := update.argSet
2024-11-27 07:30:52 +01:00
as.Main = strings.Join(main, " ")
2024-11-24 09:57:10 +01:00
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)
}
}
2024-11-27 07:30:52 +01:00
update.argSet = as
2024-11-24 09:57:10 +01:00
2024-11-28 07:20:41 +01:00
return update.do()
2024-10-06 11:28:05 +02:00
}
2024-11-28 07:20:41 +01:00
func (update *Update) do() error {
2024-11-25 09:34:31 +01:00
as := update.argSet
2024-10-06 11:28:05 +02:00
var id string
2024-11-25 09:34:31 +01:00
idMap, err := update.localIDRepo.FindAll()
2024-10-06 11:28:05 +02:00
if err != nil {
return fmt.Errorf("could not get local ids: %v", err)
}
for eid, lid := range idMap {
2024-11-25 09:34:31 +01:00
if update.localID == lid {
2024-10-06 11:28:05 +02:00
id = eid
}
}
if id == "" {
return fmt.Errorf("could not find local id")
}
2024-11-25 09:34:31 +01:00
e, err := update.eventRepo.Find(id)
2024-10-06 11:28:05 +02:00
if err != nil {
return fmt.Errorf("could not find event")
}
2024-11-25 09:34:31 +01:00
if as.Main != "" {
e.Title = as.Main
2024-10-06 11:28:05 +02:00
}
2024-11-26 07:27:15 +01:00
if as.IsSet(FlagOn) || as.IsSet(FlagAt) {
on := time.Date(e.Start.Year(), e.Start.Month(), e.Start.Day(), 0, 0, 0, 0, time.UTC)
atH := time.Duration(e.Start.Hour()) * time.Hour
atM := time.Duration(e.Start.Minute()) * time.Minute
if as.IsSet(FlagOn) {
on = as.GetTime(FlagOn)
2024-10-06 11:28:05 +02:00
}
2024-11-26 07:27:15 +01:00
if as.IsSet(FlagAt) {
at := as.GetTime(FlagAt)
atH = time.Duration(at.Hour()) * time.Hour
atM = time.Duration(at.Minute()) * time.Minute
2024-10-06 11:28:05 +02:00
}
2024-11-26 07:27:15 +01:00
e.Start = on.Add(atH).Add(atM)
2024-10-06 11:28:05 +02:00
}
2024-11-26 07:27:15 +01:00
if as.IsSet(FlagFor) {
e.Duration = as.GetDuration(FlagFor)
2024-10-06 11:28:05 +02:00
}
2024-11-26 07:27:15 +01:00
if !e.Valid() {
return fmt.Errorf("event is unvalid")
}
if err := update.eventRepo.Store(e); err != nil {
2024-10-06 11:28:05 +02:00
return fmt.Errorf("could not store event: %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-26 07:27:15 +01:00
if err := update.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-06 11:28:05 +02:00
return nil
}