planner/plan/command/list.go

91 lines
1.9 KiB
Go
Raw Normal View History

2024-09-30 07:34:40 +02:00
package command
import (
"fmt"
2024-12-29 12:36:27 +01:00
"slices"
"time"
2024-12-29 11:31:33 +01:00
2024-12-29 12:36:27 +01:00
"go-mod.ewintr.nl/planner/item"
2024-12-29 11:31:33 +01:00
"go-mod.ewintr.nl/planner/plan/storage"
2024-09-30 07:34:40 +02:00
)
2024-12-27 11:20:32 +01:00
type ListArgs struct {
2024-12-29 12:36:27 +01:00
params storage.TaskListParams
2024-09-30 07:34:40 +02:00
}
2024-12-27 11:20:32 +01:00
func NewListArgs() ListArgs {
return ListArgs{}
2024-09-30 07:34:40 +02:00
}
2024-12-27 11:20:32 +01:00
func (la ListArgs) Parse(main []string, flags map[string]string) (Command, error) {
2024-12-29 14:13:42 +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
}
2024-12-29 12:36:27 +01:00
now := time.Now()
today := item.NewDate(now.Year(), int(now.Month()), now.Day())
tomorrow := item.NewDate(now.Year(), int(now.Month()), now.Day()+1)
var date item.Date
var includeBefore, recurrer bool
switch len(main) {
case 0:
date = today
includeBefore = true
case 1:
switch {
case slices.Contains([]string{"today", "tod"}, main[0]):
date = today
includeBefore = true
case slices.Contains([]string{"tomorrow", "tom"}, main[0]):
date = tomorrow
case main[0] == "list":
2024-12-29 14:13:42 +01:00
default:
return nil, ErrWrongCommand
2024-12-29 12:36:27 +01:00
}
case 2:
2024-12-29 14:13:42 +01:00
if main[0] == "list" && main[1] == "recur" {
recurrer = true
} else {
2024-12-29 12:36:27 +01:00
return nil, ErrWrongCommand
}
2024-12-29 14:13:42 +01:00
default:
return nil, ErrWrongCommand
2024-12-29 12:36:27 +01:00
}
return &List{
args: ListArgs{
params: storage.TaskListParams{
Date: date,
IncludeBefore: includeBefore,
Recurrer: recurrer,
},
},
}, nil
2024-12-27 11:20:32 +01:00
}
type List struct {
2024-12-29 12:36:27 +01:00
args ListArgs
2024-10-29 07:22:04 +01:00
}
2024-12-29 10:16:03 +01:00
func (list *List) Do(deps Dependencies) ([][]string, error) {
2024-12-27 11:20:32 +01:00
localIDs, err := deps.LocalIDRepo.FindAll()
2024-10-03 07:32:48 +02:00
if err != nil {
2024-12-29 10:16:03 +01:00
return nil, fmt.Errorf("could not get local ids: %v", err)
2024-10-03 07:32:48 +02:00
}
2024-12-29 12:36:27 +01:00
all, err := deps.TaskRepo.FindMany(list.args.params)
2024-10-03 07:32:48 +02:00
if err != nil {
2024-12-29 10:16:03 +01:00
return nil, err
2024-10-03 07:32:48 +02:00
}
2024-12-29 12:36:27 +01:00
data := [][]string{{"id", "date", "dur", "title"}}
2024-10-03 07:32:48 +02:00
for _, e := range all {
lid, ok := localIDs[e.ID]
if !ok {
2024-12-29 10:16:03 +01:00
return nil, fmt.Errorf("could not find local id for %s", e.ID)
2024-09-30 07:34:40 +02:00
}
2024-12-29 12:36:27 +01:00
data = append(data, []string{fmt.Sprintf("%d", lid), e.Date.String(), e.Duration.String(), e.Title})
2024-09-30 07:34:40 +02:00
}
2024-12-29 12:36:27 +01:00
return data, nil
2024-09-30 07:34:40 +02:00
}