Compare commits

...

5 Commits

5 changed files with 98 additions and 18 deletions

BIN
dist/plan vendored

Binary file not shown.

View File

@ -2,11 +2,15 @@ package command
import (
"fmt"
"slices"
"time"
"go-mod.ewintr.nl/planner/item"
"go-mod.ewintr.nl/planner/plan/storage"
)
type ListArgs struct {
params storage.TaskListParams
}
func NewListArgs() ListArgs {
@ -14,14 +18,49 @@ func NewListArgs() ListArgs {
}
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 &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 {
args ListArgs
}
func (list *List) Do(deps Dependencies) ([][]string, error) {
@ -29,17 +68,18 @@ 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.FindMany(storage.TaskListParams{})
all, err := deps.TaskRepo.FindMany(list.args.params)
if err != nil {
return nil, err
}
data := [][]string{{"id", "date", "dur", "title"}}
for _, e := range all {
lid, ok := localIDs[e.ID]
if !ok {
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

@ -23,7 +23,8 @@ func TestTask(t *testing.T) {
t.Log("store")
tsk1 := item.Task{
ID: "id-1",
ID: "id-1",
Date: item.NewDate(2024, 12, 29),
}
if err := mem.Store(tsk1); err != nil {
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 != "" {
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

@ -64,13 +64,34 @@ WHERE id = ?`, id).Scan(&tsk.ID, &tsk.Title, &dateStr, &timeStr, &durStr, &recur
}
func (t *SqliteTask) FindMany(params storage.TaskListParams) ([]item.Task, error) {
rows, err := t.db.Query(`
SELECT id, title, date, time, duration, recurrer
FROM tasks`)
query := `SELECT id, title, date, time, duration, recurrer FROM tasks`
args := []interface{}{}
where := []string{}
if params.Recurrer {
where = append(where, `recurrer IS NOT NULL AND recurrer != ''`)
}
if !params.Date.IsZero() && !params.IncludeBefore {
where = append(where, `date = ?`)
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 {
query += ` WHERE ` + where[0]
for _, w := range where[1:] {
query += ` AND ` + w
}
}
rows, err := t.db.Query(query, args...)
if err != nil {
return nil, fmt.Errorf("%w: %v", ErrSqliteFailure, err)
}
result := make([]item.Task, 0)
tasks := make([]item.Task, 0)
defer rows.Close()
for rows.Next() {
var tsk item.Task
@ -87,10 +108,10 @@ FROM tasks`)
tsk.Duration = dur
tsk.Recurrer = item.NewRecurrer(recurStr)
result = append(result, tsk)
tasks = append(tasks, tsk)
}
return result, nil
return tasks, nil
}
func (s *SqliteTask) Delete(id string) error {

View File

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