2024-09-30 07:34:40 +02:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
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"
|
|
|
|
)
|
|
|
|
|
2024-12-27 11:27:59 +01:00
|
|
|
func NewAdd(main []string, fields map[string]string) (Command, error) {
|
2024-10-29 07:22:04 +01:00
|
|
|
if len(main) == 0 || main[0] != "add" {
|
2024-12-27 11:20:32 +01:00
|
|
|
return nil, ErrWrongCommand
|
2024-10-29 07:22:04 +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
|
|
|
|
2024-12-27 11:27:59 +01:00
|
|
|
tsk := item.Task{
|
|
|
|
ID: uuid.New().String(),
|
|
|
|
TaskBody: item.TaskBody{
|
|
|
|
Title: strings.Join(main, ","),
|
|
|
|
},
|
|
|
|
}
|
2024-12-27 11:20:32 +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)
|
2024-10-29 07:22:04 +01:00
|
|
|
}
|
2024-12-27 11:27:59 +01:00
|
|
|
tsk.Date = d
|
2024-10-29 07:22:04 +01:00
|
|
|
}
|
2024-12-27 11:20:32 +01:00
|
|
|
if val, ok := fields[FieldTime]; ok {
|
|
|
|
t := item.NewTimeFromString(val)
|
|
|
|
if t.IsZero() {
|
|
|
|
return nil, fmt.Errorf("%w: could not parse time", ErrInvalidArg)
|
|
|
|
}
|
2024-12-27 11:27:59 +01:00
|
|
|
tsk.Time = t
|
2024-10-01 07:36:31 +02:00
|
|
|
}
|
2024-12-27 11:20:32 +01:00
|
|
|
if val, ok := fields[FieldDuration]; 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:27:59 +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[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-27 11:27:59 +01:00
|
|
|
tsk.Recurrer = rec
|
2024-12-27 11:20:32 +01:00
|
|
|
tsk.RecurNext = tsk.Recurrer.First()
|
2024-10-07 11:11:18 +02:00
|
|
|
}
|
|
|
|
|
2024-12-27 11:20:32 +01:00
|
|
|
return &Store{
|
2024-12-27 11:27:59 +01:00
|
|
|
task: tsk,
|
2024-12-27 11:20:32 +01:00
|
|
|
}, nil
|
2024-09-30 07:34:40 +02:00
|
|
|
}
|