From e49562b7f649a5fdd542128bb5149f975b501907 Mon Sep 17 00:00:00 2001 From: Erik Winter Date: Sun, 29 Dec 2024 11:31:33 +0100 Subject: [PATCH] list options --- plan/command/add_test.go | 3 +- plan/command/delete.go | 2 +- plan/command/delete_test.go | 2 +- plan/command/list.go | 4 ++- plan/command/show.go | 2 +- plan/command/sync_test.go | 3 +- plan/command/update.go | 2 +- plan/command/update_test.go | 2 +- plan/storage/memory/task.go | 8 +++-- plan/storage/memory/task_test.go | 7 +++-- plan/storage/sqlite/task.go | 4 +-- plan/storage/storage.go | 20 +++++++++++-- plan/storage/storage_test.go | 50 ++++++++++++++++++++++++++++++++ 13 files changed, 91 insertions(+), 18 deletions(-) diff --git a/plan/command/add_test.go b/plan/command/add_test.go index faeb2f8..e9bf125 100644 --- a/plan/command/add_test.go +++ b/plan/command/add_test.go @@ -6,6 +6,7 @@ import ( "go-mod.ewintr.nl/planner/item" "go-mod.ewintr.nl/planner/plan/command" + "go-mod.ewintr.nl/planner/plan/storage" "go-mod.ewintr.nl/planner/plan/storage/memory" ) @@ -74,7 +75,7 @@ func TestAdd(t *testing.T) { t.Errorf("exp nil, got %v", err) } - actTasks, err := taskRepo.FindAll() + actTasks, err := taskRepo.FindMany(storage.TaskListParams{}) if err != nil { t.Errorf("exp nil, got %v", err) } diff --git a/plan/command/delete.go b/plan/command/delete.go index 7017323..e66a921 100644 --- a/plan/command/delete.go +++ b/plan/command/delete.go @@ -49,7 +49,7 @@ func (del *Delete) Do(deps Dependencies) ([][]string, error) { return nil, fmt.Errorf("could not find local id") } - tsk, err := deps.TaskRepo.Find(id) + tsk, err := deps.TaskRepo.FindOne(id) if err != nil { return nil, fmt.Errorf("could not get task: %v", err) } diff --git a/plan/command/delete_test.go b/plan/command/delete_test.go index 8e148e4..b51b64a 100644 --- a/plan/command/delete_test.go +++ b/plan/command/delete_test.go @@ -73,7 +73,7 @@ func TestDelete(t *testing.T) { return } - _, repoErr := taskRepo.Find(e.ID) + _, repoErr := taskRepo.FindOne(e.ID) if !errors.Is(repoErr, storage.ErrNotFound) { t.Errorf("exp %v, got %v", storage.ErrNotFound, repoErr) } diff --git a/plan/command/list.go b/plan/command/list.go index 05ed941..1251ead 100644 --- a/plan/command/list.go +++ b/plan/command/list.go @@ -2,6 +2,8 @@ package command import ( "fmt" + + "go-mod.ewintr.nl/planner/plan/storage" ) type ListArgs struct { @@ -27,7 +29,7 @@ func (list *List) Do(deps Dependencies) ([][]string, error) { if err != nil { return nil, fmt.Errorf("could not get local ids: %v", err) } - all, err := deps.TaskRepo.FindAll() + all, err := deps.TaskRepo.FindMany(storage.TaskListParams{}) if err != nil { return nil, err } diff --git a/plan/command/show.go b/plan/command/show.go index 4368c1f..083628b 100644 --- a/plan/command/show.go +++ b/plan/command/show.go @@ -45,7 +45,7 @@ func (s *Show) Do(deps Dependencies) ([][]string, error) { return nil, err } - tsk, err := deps.TaskRepo.Find(id) + tsk, err := deps.TaskRepo.FindOne(id) if err != nil { return nil, fmt.Errorf("could not find task") } diff --git a/plan/command/sync_test.go b/plan/command/sync_test.go index f76a9b6..9e3b55a 100644 --- a/plan/command/sync_test.go +++ b/plan/command/sync_test.go @@ -7,6 +7,7 @@ import ( "github.com/google/go-cmp/cmp" "go-mod.ewintr.nl/planner/item" "go-mod.ewintr.nl/planner/plan/command" + "go-mod.ewintr.nl/planner/plan/storage" "go-mod.ewintr.nl/planner/plan/storage/memory" "go-mod.ewintr.nl/planner/sync/client" ) @@ -216,7 +217,7 @@ func TestSyncReceive(t *testing.T) { } // check result - actTasks, err := taskRepo.FindAll() + actTasks, err := taskRepo.FindMany(storage.TaskListParams{}) if err != nil { t.Errorf("exp nil, got %v", err) } diff --git a/plan/command/update.go b/plan/command/update.go index 8ff8661..30bf53e 100644 --- a/plan/command/update.go +++ b/plan/command/update.go @@ -94,7 +94,7 @@ func (u *Update) Do(deps Dependencies) ([][]string, error) { return nil, err } - tsk, err := deps.TaskRepo.Find(id) + tsk, err := deps.TaskRepo.FindOne(id) if err != nil { return nil, fmt.Errorf("could not find task") } diff --git a/plan/command/update_test.go b/plan/command/update_test.go index fbc14d9..2cf9325 100644 --- a/plan/command/update_test.go +++ b/plan/command/update_test.go @@ -201,7 +201,7 @@ func TestUpdateExecute(t *testing.T) { return } - actTask, err := taskRepo.Find(tskID) + actTask, err := taskRepo.FindOne(tskID) if err != nil { t.Errorf("exp nil, got %v", err) } diff --git a/plan/storage/memory/task.go b/plan/storage/memory/task.go index bfba997..10290a1 100644 --- a/plan/storage/memory/task.go +++ b/plan/storage/memory/task.go @@ -19,7 +19,7 @@ func NewTask() *Task { } } -func (t *Task) Find(id string) (item.Task, error) { +func (t *Task) FindOne(id string) (item.Task, error) { t.mutex.RLock() defer t.mutex.RUnlock() @@ -30,13 +30,15 @@ func (t *Task) Find(id string) (item.Task, error) { return task, nil } -func (t *Task) FindAll() ([]item.Task, error) { +func (t *Task) FindMany(params storage.TaskListParams) ([]item.Task, error) { t.mutex.RLock() defer t.mutex.RUnlock() tasks := make([]item.Task, 0, len(t.tasks)) for _, tsk := range t.tasks { - tasks = append(tasks, tsk) + if storage.Match(tsk, params) { + tasks = append(tasks, tsk) + } } sort.Slice(tasks, func(i, j int) bool { return tasks[i].ID < tasks[j].ID diff --git a/plan/storage/memory/task_test.go b/plan/storage/memory/task_test.go index 265ab67..03d8f7b 100644 --- a/plan/storage/memory/task_test.go +++ b/plan/storage/memory/task_test.go @@ -4,6 +4,7 @@ import ( "testing" "go-mod.ewintr.nl/planner/item" + "go-mod.ewintr.nl/planner/plan/storage" ) func TestTask(t *testing.T) { @@ -12,7 +13,7 @@ func TestTask(t *testing.T) { mem := NewTask() t.Log("empty") - actTasks, actErr := mem.FindAll() + actTasks, actErr := mem.FindMany(storage.TaskListParams{}) if actErr != nil { t.Errorf("exp nil, got %v", actErr) } @@ -36,7 +37,7 @@ func TestTask(t *testing.T) { } t.Log("find one") - actTask, actErr := mem.Find(tsk1.ID) + actTask, actErr := mem.FindOne(tsk1.ID) if actErr != nil { t.Errorf("exp nil, got %v", actErr) } @@ -45,7 +46,7 @@ func TestTask(t *testing.T) { } t.Log("find all") - actTasks, actErr = mem.FindAll() + actTasks, actErr = mem.FindMany(storage.TaskListParams{}) if actErr != nil { t.Errorf("exp nil, got %v", actErr) } diff --git a/plan/storage/sqlite/task.go b/plan/storage/sqlite/task.go index 9b23b50..67453ca 100644 --- a/plan/storage/sqlite/task.go +++ b/plan/storage/sqlite/task.go @@ -38,7 +38,7 @@ recurrer=? return nil } -func (t *SqliteTask) Find(id string) (item.Task, error) { +func (t *SqliteTask) FindOne(id string) (item.Task, error) { var tsk item.Task var dateStr, timeStr, recurStr, durStr string err := t.db.QueryRow(` @@ -63,7 +63,7 @@ WHERE id = ?`, id).Scan(&tsk.ID, &tsk.Title, &dateStr, &timeStr, &durStr, &recur return tsk, nil } -func (t *SqliteTask) FindAll() ([]item.Task, error) { +func (t *SqliteTask) FindMany(params storage.TaskListParams) ([]item.Task, error) { rows, err := t.db.Query(` SELECT id, title, date, time, duration, recurrer FROM tasks`) diff --git a/plan/storage/storage.go b/plan/storage/storage.go index 7e7073c..a517be6 100644 --- a/plan/storage/storage.go +++ b/plan/storage/storage.go @@ -28,13 +28,29 @@ type Sync interface { LastUpdate() (time.Time, error) } +type TaskListParams struct { + Recurrer bool + Date item.Date +} + type Task interface { Store(task item.Task) error - Find(id string) (item.Task, error) - FindAll() ([]item.Task, error) + FindOne(id string) (item.Task, error) + FindMany(params TaskListParams) ([]item.Task, error) Delete(id string) error } +func Match(tsk item.Task, req TaskListParams) bool { + if req.Recurrer && tsk.Recurrer == nil { + return false + } + if !req.Date.IsZero() && !req.Date.Equal(tsk.Date) { + return false + } + + return true +} + func NextLocalID(used []int) int { if len(used) == 0 { return 1 diff --git a/plan/storage/storage_test.go b/plan/storage/storage_test.go index 528ec1a..937e7b9 100644 --- a/plan/storage/storage_test.go +++ b/plan/storage/storage_test.go @@ -3,10 +3,60 @@ package storage_test import ( "testing" + "go-mod.ewintr.nl/planner/item" "go-mod.ewintr.nl/planner/plan/storage" ) +func TestMatch(t *testing.T) { + t.Parallel() + + tskMatch := item.Task{ + ID: "id", + Date: item.NewDate(2024, 12, 29), + Recurrer: item.NewRecurrer("2024-12-29, daily"), + TaskBody: item.TaskBody{ + Title: "name", + }, + } + tskNotMatch := item.Task{ + ID: "id", + Date: item.NewDate(2024, 12, 28), + TaskBody: item.TaskBody{ + Title: "name", + }, + } + + for _, tc := range []struct { + name string + params storage.TaskListParams + }{ + { + name: "date", + params: storage.TaskListParams{ + Date: item.NewDate(2024, 12, 29), + }, + }, + { + name: "recurrer", + params: storage.TaskListParams{ + Recurrer: true, + }, + }, + } { + t.Run(tc.name, func(t *testing.T) { + if !storage.Match(tskMatch, tc.params) { + t.Errorf("exp tsk to match") + } + if storage.Match(tskNotMatch, tc.params) { + t.Errorf("exp tsk to not match") + } + }) + } +} + func TestNextLocalId(t *testing.T) { + t.Parallel() + for _, tc := range []struct { name string used []int