planner/plan/command/update.go

125 lines
2.5 KiB
Go
Raw Normal View History

2024-10-06 11:28:05 +02:00
package command
import (
"fmt"
2024-10-29 07:22:04 +01:00
"strconv"
"strings"
2024-12-28 08:43:59 +01:00
"time"
"go-mod.ewintr.nl/planner/item"
2024-10-06 11:28:05 +02:00
)
2024-12-28 08:43:59 +01:00
type UpdateArgs struct {
LocalID int
Title string
Date item.Date
Time item.Time
Duration time.Duration
Recurrer item.Recurrer
2024-10-06 11:28:05 +02:00
}
2024-12-28 08:43:59 +01:00
func (ua UpdateArgs) Parse(main []string, fields map[string]string) (Command, error) {
2024-10-29 07:22:04 +01:00
if len(main) < 2 || main[0] != "update" {
2024-12-28 08:43:59 +01:00
return nil, ErrWrongCommand
2024-10-29 07:22:04 +01:00
}
localID, err := strconv.Atoi(main[1])
if err != nil {
2024-12-28 08:43:59 +01:00
return nil, fmt.Errorf("not a local id: %v", main[1])
}
args := UpdateArgs{
LocalID: localID,
Title: strings.Join(main[2:], " "),
2024-10-29 07:22:04 +01:00
}
2024-12-28 08:43:59 +01:00
if val, ok := fields[FieldDate]; ok {
d := item.NewDateFromString(val)
if d.IsZero() {
return nil, fmt.Errorf("%w: could not parse date", ErrInvalidArg)
}
args.Date = d
}
if val, ok := fields[FieldTime]; ok {
t := item.NewTimeFromString(val)
if t.IsZero() {
return nil, fmt.Errorf("%w: could not parse time", ErrInvalidArg)
2024-10-29 07:22:04 +01:00
}
2024-12-28 08:43:59 +01:00
args.Time = t
}
if val, ok := fields[FieldDuration]; ok {
d, err := time.ParseDuration(val)
if err != nil {
return nil, fmt.Errorf("%w: could not parse duration", ErrInvalidArg)
}
args.Duration = d
}
if val, ok := fields[FieldRecurrer]; ok {
rec := item.NewRecurrer(val)
if rec == nil {
return nil, fmt.Errorf("%w: could not parse recurrer", ErrInvalidArg)
2024-10-29 07:22:04 +01:00
}
2024-12-28 08:43:59 +01:00
args.Recurrer = rec
2024-10-06 11:28:05 +02:00
}
2024-10-29 07:22:04 +01:00
2024-12-28 08:43:59 +01:00
return &Update{args}, nil
}
type Update struct {
args UpdateArgs
2024-10-06 11:28:05 +02:00
}
2024-12-28 08:43:59 +01:00
func (u *Update) Do(deps Dependencies) error {
2024-10-06 11:28:05 +02:00
var id string
2024-12-28 08:43:59 +01:00
idMap, err := deps.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-12-28 08:43:59 +01:00
if u.args.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-28 08:43:59 +01:00
tsk, err := deps.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-12-28 08:43:59 +01:00
if u.args.Title != "" {
tsk.Title = u.args.Title
2024-10-06 11:28:05 +02:00
}
2024-12-28 08:43:59 +01:00
if !u.args.Date.IsZero() {
tsk.Date = u.args.Date
2024-12-19 12:06:03 +01:00
}
2024-12-28 08:43:59 +01:00
if !u.args.Time.IsZero() {
tsk.Time = u.args.Time
2024-10-06 11:28:05 +02:00
}
2024-12-28 08:43:59 +01:00
if u.args.Duration != 0 {
tsk.Duration = u.args.Duration
2024-10-06 11:28:05 +02:00
}
2024-12-28 08:43:59 +01:00
if u.args.Recurrer != nil {
tsk.Recurrer = u.args.Recurrer
tsk.RecurNext = tsk.Recurrer.First()
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-28 08:43:59 +01:00
if err := deps.TaskRepo.Store(tsk); err != nil {
2024-12-24 08:00:23 +01:00
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-12-28 08:43:59 +01:00
if err := deps.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
}