list options

This commit is contained in:
Erik Winter 2024-12-29 11:31:33 +01:00
parent c8e5058445
commit e49562b7f6
13 changed files with 91 additions and 18 deletions

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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
}

View File

@ -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")
}

View File

@ -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)
}

View File

@ -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")
}

View File

@ -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)
}

View File

@ -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

View File

@ -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)
}

View File

@ -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`)

View File

@ -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

View File

@ -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