planner/plan/command/update.go

137 lines
2.9 KiB
Go
Raw Normal View History

2024-10-06 11:28:05 +02:00
package command
import (
2024-12-29 10:18:11 +01:00
"errors"
2024-10-06 11:28:05 +02:00
"fmt"
2024-10-29 07:22:04 +01:00
"strconv"
"strings"
2024-12-27 11:20:32 +01:00
"time"
2024-10-06 11:28:05 +02:00
2024-12-27 11:20:32 +01:00
"go-mod.ewintr.nl/planner/item"
2024-12-29 10:18:11 +01:00
"go-mod.ewintr.nl/planner/plan/storage"
2024-10-06 11:28:05 +02:00
)
2024-12-27 11:20:32 +01:00
type UpdateArgs struct {
fieldTPL map[string][]string
LocalID int
Title string
Date item.Date
Time item.Time
Duration time.Duration
Recurrer item.Recurrer
2024-10-29 07:22:04 +01:00
}
2024-12-27 11:20:32 +01:00
func NewUpdateArgs() UpdateArgs {
return UpdateArgs{
fieldTPL: map[string][]string{
"date": []string{"d", "date", "on"},
"time": []string{"t", "time", "at"},
"duration": []string{"dur", "duration", "for"},
"recurrer": []string{"rec", "recurrer"},
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-12-27 11:20:32 +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-27 11:20:32 +01:00
return nil, ErrWrongCommand
2024-10-29 07:22:04 +01:00
}
localID, err := strconv.Atoi(main[1])
if err != nil {
2024-12-27 11:20:32 +01:00
return nil, fmt.Errorf("not a local id: %v", main[1])
}
fields, err = ResolveFields(fields, ua.fieldTPL)
if err != nil {
return nil, err
}
args := UpdateArgs{
LocalID: localID,
Title: strings.Join(main[2:], " "),
2024-10-29 07:22:04 +01:00
}
2024-12-27 11:20:32 +01:00
if val, ok := fields["date"]; 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["time"]; 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-27 11:20:32 +01:00
args.Time = t
}
if val, ok := fields["duration"]; 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["recurrer"]; 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-27 11:20:32 +01:00
args.Recurrer = rec
2024-10-06 11:28:05 +02:00
}
2024-10-29 07:22:04 +01:00
2024-12-27 11:20:32 +01:00
return &Update{args}, nil
}
type Update struct {
args UpdateArgs
2024-10-06 11:28:05 +02:00
}
2024-12-29 10:16:03 +01:00
func (u *Update) Do(deps Dependencies) ([][]string, error) {
2024-12-29 10:18:11 +01:00
id, err := deps.LocalIDRepo.FindOne(u.args.LocalID)
switch {
case errors.Is(err, storage.ErrNotFound):
2024-12-29 10:16:03 +01:00
return nil, fmt.Errorf("could not find local id")
2024-12-29 10:18:11 +01:00
case err != nil:
return nil, err
2024-10-06 11:28:05 +02:00
}
2024-12-29 11:31:33 +01:00
tsk, err := deps.TaskRepo.FindOne(id)
2024-10-06 11:28:05 +02:00
if err != nil {
2024-12-29 10:16:03 +01:00
return nil, fmt.Errorf("could not find task")
2024-10-06 11:28:05 +02:00
}
2024-12-27 11:20:32 +01:00
if u.args.Title != "" {
tsk.Title = u.args.Title
2024-10-06 11:28:05 +02:00
}
2024-12-27 11:20:32 +01:00
if !u.args.Date.IsZero() {
tsk.Date = u.args.Date
2024-12-19 12:06:03 +01:00
}
2024-12-27 11:20:32 +01:00
if !u.args.Time.IsZero() {
tsk.Time = u.args.Time
2024-10-06 11:28:05 +02:00
}
2024-12-27 11:20:32 +01:00
if u.args.Duration != 0 {
tsk.Duration = u.args.Duration
2024-10-06 11:28:05 +02:00
}
2024-12-27 11:20:32 +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() {
2024-12-29 10:16:03 +01:00
return nil, fmt.Errorf("task is unvalid")
2024-10-29 07:22:04 +01:00
}
2024-12-27 11:20:32 +01:00
if err := deps.TaskRepo.Store(tsk); err != nil {
2024-12-29 10:16:03 +01:00
return nil, 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-29 10:16:03 +01:00
return nil, fmt.Errorf("could not convert task to sync item: %v", err)
2024-10-07 11:11:18 +02:00
}
2024-12-27 11:20:32 +01:00
if err := deps.SyncRepo.Store(it); err != nil {
2024-12-29 10:16:03 +01:00
return nil, fmt.Errorf("could not store sync item: %v", err)
2024-10-07 11:11:18 +02:00
}
2024-12-29 10:16:03 +01:00
return nil, nil
2024-10-06 11:28:05 +02:00
}