This commit is contained in:
Erik Winter 2024-12-29 12:36:27 +01:00
parent 0413da4c3d
commit 9e05bef411
4 changed files with 76 additions and 13 deletions

View File

@ -2,11 +2,15 @@ package command
import ( import (
"fmt" "fmt"
"slices"
"time"
"go-mod.ewintr.nl/planner/item"
"go-mod.ewintr.nl/planner/plan/storage" "go-mod.ewintr.nl/planner/plan/storage"
) )
type ListArgs struct { type ListArgs struct {
params storage.TaskListParams
} }
func NewListArgs() ListArgs { func NewListArgs() ListArgs {
@ -14,14 +18,49 @@ func NewListArgs() ListArgs {
} }
func (la ListArgs) Parse(main []string, flags map[string]string) (Command, error) { func (la ListArgs) Parse(main []string, flags map[string]string) (Command, error) {
if len(main) > 0 && main[0] != "list" { if len(main) > 1 {
return nil, ErrWrongCommand return nil, ErrWrongCommand
} }
return &List{}, nil 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":
}
case 2:
if main[0] != "list" || main[1] != "recur" {
return nil, ErrWrongCommand
}
recurrer = true
}
return &List{
args: ListArgs{
params: storage.TaskListParams{
Date: date,
IncludeBefore: includeBefore,
Recurrer: recurrer,
},
},
}, nil
} }
type List struct { type List struct {
args ListArgs
} }
func (list *List) Do(deps Dependencies) ([][]string, error) { func (list *List) Do(deps Dependencies) ([][]string, error) {
@ -29,17 +68,18 @@ func (list *List) Do(deps Dependencies) ([][]string, error) {
if err != nil { if err != nil {
return nil, fmt.Errorf("could not get local ids: %v", err) return nil, fmt.Errorf("could not get local ids: %v", err)
} }
all, err := deps.TaskRepo.FindMany(storage.TaskListParams{}) all, err := deps.TaskRepo.FindMany(list.args.params)
if err != nil { if err != nil {
return nil, err return nil, err
} }
data := [][]string{{"id", "date", "dur", "title"}}
for _, e := range all { for _, e := range all {
lid, ok := localIDs[e.ID] lid, ok := localIDs[e.ID]
if !ok { if !ok {
return nil, fmt.Errorf("could not find local id for %s", e.ID) return nil, fmt.Errorf("could not find local id for %s", e.ID)
} }
fmt.Printf("%s\t%d\t%s\t%s\t%s\n", e.ID, lid, e.Title, e.Date.String(), e.Duration.String()) data = append(data, []string{fmt.Sprintf("%d", lid), e.Date.String(), e.Duration.String(), e.Title})
} }
return nil, nil return data, nil
} }

View File

@ -24,6 +24,7 @@ func TestTask(t *testing.T) {
t.Log("store") t.Log("store")
tsk1 := item.Task{ tsk1 := item.Task{
ID: "id-1", ID: "id-1",
Date: item.NewDate(2024, 12, 29),
} }
if err := mem.Store(tsk1); err != nil { if err := mem.Store(tsk1); err != nil {
t.Errorf("exp nil, got %v", err) t.Errorf("exp nil, got %v", err)
@ -53,4 +54,16 @@ func TestTask(t *testing.T) {
if diff := item.TaskDiffs([]item.Task{tsk1, tsk2}, actTasks); diff != "" { if diff := item.TaskDiffs([]item.Task{tsk1, tsk2}, actTasks); diff != "" {
t.Errorf("(exp -, got +)\n%s", diff) t.Errorf("(exp -, got +)\n%s", diff)
} }
t.Log("fond some")
actTasks, actErr = mem.FindMany(storage.TaskListParams{
Date: item.NewDate(2024, 12, 29),
})
if actErr != nil {
t.Errorf("exp nil, got %v", actErr)
}
if diff := item.TaskDiffs([]item.Task{tsk1}, actTasks); diff != "" {
t.Errorf("(exp -, got +)\n%s", diff)
}
} }

View File

@ -71,10 +71,14 @@ func (t *SqliteTask) FindMany(params storage.TaskListParams) ([]item.Task, error
if params.Recurrer { if params.Recurrer {
where = append(where, `recurrer IS NOT NULL AND recurrer != ''`) where = append(where, `recurrer IS NOT NULL AND recurrer != ''`)
} }
if !params.Date.IsZero() { if !params.Date.IsZero() && !params.IncludeBefore {
where = append(where, `date = ?`) where = append(where, `date = ?`)
args = append(args, params.Date.String()) args = append(args, params.Date.String())
} }
if !params.Date.IsZero() && params.IncludeBefore {
where = append(where, `date <= ?`)
args = append(args, params.Date.String())
}
if len(where) > 0 { if len(where) > 0 {
query += ` WHERE ` + where[0] query += ` WHERE ` + where[0]

View File

@ -31,6 +31,7 @@ type Sync interface {
type TaskListParams struct { type TaskListParams struct {
Recurrer bool Recurrer bool
Date item.Date Date item.Date
IncludeBefore bool
} }
type Task interface { type Task interface {
@ -40,13 +41,18 @@ type Task interface {
Delete(id string) error Delete(id string) error
} }
func Match(tsk item.Task, req TaskListParams) bool { func Match(tsk item.Task, params TaskListParams) bool {
if req.Recurrer && tsk.Recurrer == nil { if params.Recurrer && tsk.Recurrer == nil {
return false return false
} }
if !req.Date.IsZero() && !req.Date.Equal(tsk.Date) { if !params.Date.IsZero() {
if !params.IncludeBefore && !params.Date.Equal(tsk.Date) {
return false return false
} }
if params.IncludeBefore && tsk.Date.After(params.Date) {
return false
}
}
return true return true
} }