2024-09-30 07:34:40 +02:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2025-01-05 12:20:35 +01:00
|
|
|
"slices"
|
2024-10-29 07:22:04 +01:00
|
|
|
"strings"
|
2024-12-27 11:20:32 +01:00
|
|
|
"time"
|
2024-09-30 07:34:40 +02:00
|
|
|
|
2024-10-01 07:36:31 +02:00
|
|
|
"github.com/google/uuid"
|
2024-09-30 07:34:40 +02:00
|
|
|
"go-mod.ewintr.nl/planner/item"
|
2025-01-13 09:13:48 +01:00
|
|
|
"go-mod.ewintr.nl/planner/sync/client"
|
2024-09-30 07:34:40 +02:00
|
|
|
)
|
|
|
|
|
2024-12-27 11:20:32 +01:00
|
|
|
type AddArgs struct {
|
|
|
|
fieldTPL map[string][]string
|
2024-12-29 09:32:49 +01:00
|
|
|
Task item.Task
|
2024-09-30 07:34:40 +02:00
|
|
|
}
|
|
|
|
|
2024-12-27 11:20:32 +01:00
|
|
|
func NewAddArgs() AddArgs {
|
|
|
|
return AddArgs{
|
|
|
|
fieldTPL: map[string][]string{
|
2024-12-29 09:32:49 +01:00
|
|
|
"date": {"d", "date", "on"},
|
|
|
|
"time": {"t", "time", "at"},
|
2024-12-31 08:37:00 +01:00
|
|
|
"project": {"p", "project"},
|
2024-12-29 09:32:49 +01:00
|
|
|
"duration": {"dur", "duration", "for"},
|
|
|
|
"recurrer": {"rec", "recurrer"},
|
2024-10-29 07:22:04 +01:00
|
|
|
},
|
2024-10-01 07:36:31 +02:00
|
|
|
}
|
2024-09-30 07:34:40 +02:00
|
|
|
}
|
|
|
|
|
2024-12-27 11:20:32 +01:00
|
|
|
func (aa AddArgs) Parse(main []string, fields map[string]string) (Command, error) {
|
2025-01-05 12:20:35 +01:00
|
|
|
if len(main) == 0 || !slices.Contains([]string{"add", "a", "new", "n"}, main[0]) {
|
2024-12-27 11:20:32 +01:00
|
|
|
return nil, ErrWrongCommand
|
2024-10-29 07:22:04 +01:00
|
|
|
}
|
2025-01-05 12:20:35 +01:00
|
|
|
|
2024-12-27 11:20:32 +01:00
|
|
|
main = main[1:]
|
|
|
|
if len(main) == 0 {
|
|
|
|
return nil, fmt.Errorf("%w: title is required for add", ErrInvalidArg)
|
2024-10-29 07:22:04 +01:00
|
|
|
}
|
2024-12-27 11:20:32 +01:00
|
|
|
fields, err := ResolveFields(fields, aa.fieldTPL)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2024-10-29 07:22:04 +01:00
|
|
|
}
|
2024-12-27 11:20:32 +01:00
|
|
|
|
|
|
|
tsk := item.Task{
|
|
|
|
ID: uuid.New().String(),
|
|
|
|
TaskBody: item.TaskBody{
|
2024-12-29 09:32:49 +01:00
|
|
|
Title: strings.Join(main, " "),
|
2024-12-27 11:20:32 +01:00
|
|
|
},
|
2024-10-01 07:36:31 +02:00
|
|
|
}
|
2024-12-27 11:20:32 +01:00
|
|
|
|
2024-12-31 08:37:00 +01:00
|
|
|
if val, ok := fields["project"]; ok {
|
|
|
|
tsk.Project = val
|
|
|
|
}
|
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)
|
|
|
|
}
|
|
|
|
tsk.Date = d
|
2024-10-01 07:36:31 +02:00
|
|
|
}
|
2024-12-27 11:20:32 +01:00
|
|
|
if val, ok := fields["time"]; ok {
|
|
|
|
t := item.NewTimeFromString(val)
|
|
|
|
if t.IsZero() {
|
|
|
|
return nil, fmt.Errorf("%w: could not parse time", ErrInvalidArg)
|
|
|
|
}
|
|
|
|
tsk.Time = t
|
2024-10-01 07:36:31 +02:00
|
|
|
}
|
2024-12-27 11:20:32 +01:00
|
|
|
if val, ok := fields["duration"]; ok {
|
|
|
|
d, err := time.ParseDuration(val)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("%w: could not parse duration", ErrInvalidArg)
|
2024-10-29 07:22:04 +01:00
|
|
|
}
|
2024-12-27 11:20:32 +01:00
|
|
|
tsk.Duration = d
|
2024-10-01 07:36:31 +02:00
|
|
|
}
|
2024-12-27 11:20:32 +01:00
|
|
|
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
|
|
|
tsk.Recurrer = rec
|
|
|
|
tsk.RecurNext = tsk.Recurrer.First()
|
2024-10-01 07:36:31 +02:00
|
|
|
}
|
2024-10-29 07:22:04 +01:00
|
|
|
|
2024-12-27 11:20:32 +01:00
|
|
|
return &Add{
|
2024-12-29 09:32:49 +01:00
|
|
|
Args: AddArgs{
|
|
|
|
Task: tsk,
|
2024-12-27 11:20:32 +01:00
|
|
|
},
|
|
|
|
}, nil
|
2024-10-29 07:22:04 +01:00
|
|
|
}
|
|
|
|
|
2024-12-27 11:20:32 +01:00
|
|
|
type Add struct {
|
2024-12-29 09:32:49 +01:00
|
|
|
Args AddArgs
|
2024-12-27 11:20:32 +01:00
|
|
|
}
|
2024-12-01 10:22:47 +01:00
|
|
|
|
2025-01-13 09:13:48 +01:00
|
|
|
func (a Add) 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()
|
|
|
|
|
|
|
|
if err := repos.Task(tx).Store(a.Args.Task); err != nil {
|
2024-12-29 09:32:49 +01:00
|
|
|
return nil, fmt.Errorf("could not store event: %v", err)
|
2024-10-01 07:36:31 +02:00
|
|
|
}
|
|
|
|
|
2025-01-13 09:13:48 +01:00
|
|
|
localID, err := repos.LocalID(tx).Next()
|
2024-10-03 07:32:48 +02:00
|
|
|
if err != nil {
|
2024-12-29 09:32:49 +01:00
|
|
|
return nil, fmt.Errorf("could not create next local id: %v", err)
|
2024-10-03 07:32:48 +02:00
|
|
|
}
|
2025-01-13 09:13:48 +01:00
|
|
|
if err := repos.LocalID(tx).Store(a.Args.Task.ID, localID); err != nil {
|
2024-12-29 09:32:49 +01:00
|
|
|
return nil, fmt.Errorf("could not store local id: %v", err)
|
2024-10-03 07:32:48 +02:00
|
|
|
}
|
|
|
|
|
2024-12-29 09:32:49 +01:00
|
|
|
it, err := a.Args.Task.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 event 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 add task: %v", err)
|
|
|
|
}
|
|
|
|
|
2025-01-04 12:38:52 +01:00
|
|
|
return AddRender{}, nil
|
2024-12-29 09:32:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type AddRender struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ar AddRender) Render() string {
|
|
|
|
return "stored task"
|
2024-09-30 07:34:40 +02:00
|
|
|
}
|