2024-10-06 11:28:05 +02:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-10-29 07:22:04 +01:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2024-10-06 11:28:05 +02:00
|
|
|
|
|
|
|
"go-mod.ewintr.nl/planner/plan/storage"
|
|
|
|
)
|
|
|
|
|
2024-10-29 07:22:04 +01:00
|
|
|
type Update 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
|
|
|
|
localID int
|
|
|
|
}
|
|
|
|
|
2024-12-24 08:00:23 +01:00
|
|
|
func NewUpdate(localIDRepo storage.LocalID, taskRepo storage.Task, syncRepo storage.Sync) Command {
|
2024-10-29 07:22:04 +01:00
|
|
|
return &Update{
|
|
|
|
localIDRepo: localIDRepo,
|
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
|
|
|
FlagTitle: &FlagString{},
|
|
|
|
FlagOn: &FlagDate{},
|
|
|
|
FlagAt: &FlagTime{},
|
|
|
|
FlagFor: &FlagDuration{},
|
|
|
|
FlagRec: &FlagRecurrer{},
|
2024-10-29 07:22:04 +01:00
|
|
|
},
|
2024-10-06 11:28:05 +02:00
|
|
|
},
|
2024-10-29 07:22:04 +01:00
|
|
|
}
|
2024-10-06 11:28:05 +02:00
|
|
|
}
|
|
|
|
|
2024-10-29 07:22:04 +01:00
|
|
|
func (update *Update) Execute(main []string, flags map[string]string) error {
|
|
|
|
if len(main) < 2 || main[0] != "update" {
|
|
|
|
return ErrWrongCommand
|
|
|
|
}
|
|
|
|
localID, err := strconv.Atoi(main[1])
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("not a local id: %v", main[1])
|
|
|
|
}
|
|
|
|
update.localID = localID
|
|
|
|
main = main[2:]
|
|
|
|
|
|
|
|
as := update.argSet
|
|
|
|
as.Main = strings.Join(main, " ")
|
|
|
|
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-10-06 11:28:05 +02:00
|
|
|
}
|
2024-10-29 07:22:04 +01:00
|
|
|
update.argSet = as
|
|
|
|
|
|
|
|
return update.do()
|
2024-10-06 11:28:05 +02:00
|
|
|
}
|
|
|
|
|
2024-10-29 07:22:04 +01:00
|
|
|
func (update *Update) do() error {
|
|
|
|
as := update.argSet
|
2024-10-06 11:28:05 +02:00
|
|
|
var id string
|
2024-10-29 07:22:04 +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)
|
|
|
|
}
|
2024-12-24 08:00:23 +01:00
|
|
|
for tid, lid := range idMap {
|
2024-10-29 07:22:04 +01:00
|
|
|
if update.localID == lid {
|
2024-12-24 08:00:23 +01:00
|
|
|
id = tid
|
2024-10-06 11:28:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if id == "" {
|
|
|
|
return fmt.Errorf("could not find local id")
|
|
|
|
}
|
|
|
|
|
2024-12-24 08:00:23 +01:00
|
|
|
tsk, err := update.taskRepo.Find(id)
|
2024-10-06 11:28:05 +02:00
|
|
|
if err != nil {
|
2024-12-24 08:00:23 +01:00
|
|
|
return fmt.Errorf("could not find task")
|
2024-10-06 11:28:05 +02:00
|
|
|
}
|
|
|
|
|
2024-10-29 07:22:04 +01:00
|
|
|
if as.Main != "" {
|
2024-12-24 08:00:23 +01:00
|
|
|
tsk.Title = as.Main
|
2024-10-06 11:28:05 +02:00
|
|
|
}
|
2024-12-19 12:06:03 +01:00
|
|
|
if as.IsSet(FlagOn) {
|
2024-12-24 08:00:23 +01:00
|
|
|
tsk.Date = as.GetDate(FlagOn)
|
2024-12-19 12:06:03 +01:00
|
|
|
}
|
|
|
|
if as.IsSet(FlagAt) {
|
2024-12-24 08:00:23 +01:00
|
|
|
tsk.Time = as.GetTime(FlagAt)
|
2024-10-06 11:28:05 +02:00
|
|
|
}
|
2024-10-29 07:22:04 +01:00
|
|
|
if as.IsSet(FlagFor) {
|
2024-12-24 08:00:23 +01:00
|
|
|
tsk.Duration = as.GetDuration(FlagFor)
|
2024-10-06 11:28:05 +02:00
|
|
|
}
|
2024-12-19 12:06:03 +01:00
|
|
|
if as.IsSet(FlagRec) {
|
2024-12-24 08:00:23 +01:00
|
|
|
tsk.Recurrer = as.GetRecurrer(FlagRec)
|
2024-12-01 10:22:47 +01:00
|
|
|
}
|
2024-10-29 07:22:04 +01:00
|
|
|
|
2024-12-24 08:00:23 +01:00
|
|
|
if !tsk.Valid() {
|
|
|
|
return fmt.Errorf("task is unvalid")
|
2024-10-29 07:22:04 +01:00
|
|
|
}
|
|
|
|
|
2024-12-24 08:00:23 +01:00
|
|
|
if err := update.taskRepo.Store(tsk); err != nil {
|
|
|
|
return fmt.Errorf("could not store task: %v", err)
|
2024-10-06 11:28:05 +02:00
|
|
|
}
|
|
|
|
|
2024-12-24 08:00:23 +01:00
|
|
|
it, err := tsk.Item()
|
2024-10-07 11:11:18 +02:00
|
|
|
if err != nil {
|
2024-12-24 08:00:23 +01:00
|
|
|
return fmt.Errorf("could not convert task to sync item: %v", err)
|
2024-10-07 11:11:18 +02:00
|
|
|
}
|
2024-10-29 07:22:04 +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
|
|
|
|
}
|