planner/plan/command/update.go

188 lines
4.3 KiB
Go
Raw Normal View History

2024-10-06 11:28:05 +02:00
package command
import (
2024-12-29 09:32:49 +01:00
"errors"
2024-10-06 11:28:05 +02:00
"fmt"
"slices"
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 09:32:49 +01:00
"go-mod.ewintr.nl/planner/plan/storage"
2025-01-13 09:13:48 +01:00
"go-mod.ewintr.nl/planner/sync/client"
2024-10-06 11:28:05 +02:00
)
2024-12-27 11:20:32 +01:00
type UpdateArgs struct {
fieldTPL map[string][]string
NeedUpdate []string
LocalID int
Title string
Project 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{
2024-12-31 08:37:00 +01:00
"project": {"p", "project"},
2024-12-29 09:32:49 +01:00
"date": {"d", "date", "on"},
"time": {"t", "time", "at"},
"duration": {"dur", "duration", "for"},
"recurrer": {"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) {
2025-01-05 09:53:56 +01:00
if len(main) < 2 {
2024-12-27 11:20:32 +01:00
return nil, ErrWrongCommand
2024-10-29 07:22:04 +01:00
}
2025-01-05 12:23:05 +01:00
aliases := []string{"u", "update", "m", "mod"}
2025-01-05 09:53:56 +01:00
var localIDStr string
switch {
case slices.Contains(aliases, main[0]):
localIDStr = main[1]
case slices.Contains(aliases, main[1]):
localIDStr = main[0]
default:
return nil, ErrWrongCommand
}
localID, err := strconv.Atoi(localIDStr)
2024-10-29 07:22:04 +01:00
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{
NeedUpdate: make([]string, 0),
LocalID: localID,
Title: strings.Join(main[2:], " "),
2024-10-29 07:22:04 +01:00
}
2024-12-31 08:37:00 +01:00
if val, ok := fields["project"]; ok {
args.NeedUpdate = append(args.NeedUpdate, "project")
2024-12-31 08:37:00 +01:00
args.Project = val
}
2024-12-27 11:20:32 +01:00
if val, ok := fields["date"]; ok {
args.NeedUpdate = append(args.NeedUpdate, "date")
if val != "" {
d := item.NewDateFromString(val)
if d.IsZero() {
return nil, fmt.Errorf("%w: could not parse date", ErrInvalidArg)
}
args.Date = d
2024-12-27 11:20:32 +01:00
}
}
if val, ok := fields["time"]; ok {
args.NeedUpdate = append(args.NeedUpdate, "time")
if val != "" {
t := item.NewTimeFromString(val)
if t.IsZero() {
return nil, fmt.Errorf("%w: could not parse time", ErrInvalidArg)
}
args.Time = t
2024-10-29 07:22:04 +01:00
}
2024-12-27 11:20:32 +01:00
}
if val, ok := fields["duration"]; ok {
args.NeedUpdate = append(args.NeedUpdate, "duration")
if val != "" {
d, err := time.ParseDuration(val)
if err != nil {
return nil, fmt.Errorf("%w: could not parse duration", ErrInvalidArg)
}
args.Duration = d
2024-12-27 11:20:32 +01:00
}
}
if val, ok := fields["recurrer"]; ok {
args.NeedUpdate = append(args.NeedUpdate, "recurrer")
if val != "" {
rec := item.NewRecurrer(val)
if rec == nil {
return nil, fmt.Errorf("%w: could not parse recurrer", ErrInvalidArg)
}
args.Recurrer = rec
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-12-27 11:20:32 +01:00
return &Update{args}, nil
}
type Update struct {
args UpdateArgs
2024-10-06 11:28:05 +02:00
}
2025-01-13 09:13:48 +01:00
func (u Update) Do(repos Repositories, _ client.Client) (CommandResult, error) {
tx, err := repos.Begin()
if err != nil {
return nil, fmt.Errorf("could not start transaction: %v", err)
}
defer tx.Rollback()
id, err := repos.LocalID(tx).FindOne(u.args.LocalID)
2024-12-29 09:32:49 +01:00
switch {
case errors.Is(err, storage.ErrNotFound):
return nil, fmt.Errorf("could not find local id")
case err != nil:
return nil, err
2024-10-06 11:28:05 +02:00
}
2025-01-13 09:13:48 +01:00
tsk, err := repos.Task(tx).FindOne(id)
2024-10-06 11:28:05 +02:00
if err != nil {
2024-12-29 09:32:49 +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
}
if slices.Contains(u.args.NeedUpdate, "project") {
2024-12-31 08:37:00 +01:00
tsk.Project = u.args.Project
}
if slices.Contains(u.args.NeedUpdate, "date") {
2024-12-27 11:20:32 +01:00
tsk.Date = u.args.Date
2024-12-19 12:06:03 +01:00
}
if slices.Contains(u.args.NeedUpdate, "time") {
2024-12-27 11:20:32 +01:00
tsk.Time = u.args.Time
2024-10-06 11:28:05 +02:00
}
if slices.Contains(u.args.NeedUpdate, "duration") {
2024-12-27 11:20:32 +01:00
tsk.Duration = u.args.Duration
2024-10-06 11:28:05 +02:00
}
if slices.Contains(u.args.NeedUpdate, "recurrer") {
2024-12-27 11:20:32 +01:00
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 09:32:49 +01:00
return nil, fmt.Errorf("task is unvalid")
2024-10-29 07:22:04 +01:00
}
2025-01-13 09:13:48 +01:00
if err := repos.Task(tx).Store(tsk); err != nil {
2024-12-29 09:32:49 +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 09:32:49 +01:00
return nil, fmt.Errorf("could not convert task to sync item: %v", err)
2024-10-07 11:11:18 +02:00
}
2025-01-13 09:13:48 +01:00
if err := repos.Sync(tx).Store(it); err != nil {
2024-12-29 09:32:49 +01:00
return nil, fmt.Errorf("could not store sync item: %v", err)
2024-10-07 11:11:18 +02:00
}
2025-01-13 09:13:48 +01:00
if err := tx.Commit(); err != nil {
return nil, fmt.Errorf("could not update task: %v", err)
}
2024-12-29 09:32:49 +01:00
return UpdateResult{}, nil
}
type UpdateResult struct{}
func (ur UpdateResult) Render() string {
return "task updated"
2024-10-06 11:28:05 +02:00
}