Compare commits
No commits in common. "2e67160fc77da1e94fa1c705f2336288d16b93b1" and "e49562b7f649a5fdd542128bb5149f975b501907" have entirely different histories.
2e67160fc7
...
e49562b7f6
|
@ -2,15 +2,11 @@ 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 {
|
||||||
|
@ -18,49 +14,14 @@ 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) > 1 {
|
if len(main) > 0 && main[0] != "list" {
|
||||||
return nil, ErrWrongCommand
|
return nil, ErrWrongCommand
|
||||||
}
|
}
|
||||||
|
|
||||||
now := time.Now()
|
return &List{}, nil
|
||||||
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) {
|
||||||
|
@ -68,18 +29,17 @@ 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(list.args.params)
|
all, err := deps.TaskRepo.FindMany(storage.TaskListParams{})
|
||||||
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)
|
||||||
}
|
}
|
||||||
data = append(data, []string{fmt.Sprintf("%d", lid), e.Date.String(), e.Duration.String(), e.Title})
|
fmt.Printf("%s\t%d\t%s\t%s\t%s\n", e.ID, lid, e.Title, e.Date.String(), e.Duration.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
return data, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,7 +24,6 @@ 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)
|
||||||
|
@ -54,16 +53,4 @@ 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)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -64,34 +64,13 @@ WHERE id = ?`, id).Scan(&tsk.ID, &tsk.Title, &dateStr, &timeStr, &durStr, &recur
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *SqliteTask) FindMany(params storage.TaskListParams) ([]item.Task, error) {
|
func (t *SqliteTask) FindMany(params storage.TaskListParams) ([]item.Task, error) {
|
||||||
query := `SELECT id, title, date, time, duration, recurrer FROM tasks`
|
rows, err := t.db.Query(`
|
||||||
args := []interface{}{}
|
SELECT id, title, date, time, duration, recurrer
|
||||||
where := []string{}
|
FROM tasks`)
|
||||||
|
|
||||||
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 {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("%w: %v", ErrSqliteFailure, err)
|
return nil, fmt.Errorf("%w: %v", ErrSqliteFailure, err)
|
||||||
}
|
}
|
||||||
tasks := make([]item.Task, 0)
|
result := make([]item.Task, 0)
|
||||||
defer rows.Close()
|
defer rows.Close()
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
var tsk item.Task
|
var tsk item.Task
|
||||||
|
@ -108,10 +87,10 @@ func (t *SqliteTask) FindMany(params storage.TaskListParams) ([]item.Task, error
|
||||||
tsk.Duration = dur
|
tsk.Duration = dur
|
||||||
tsk.Recurrer = item.NewRecurrer(recurStr)
|
tsk.Recurrer = item.NewRecurrer(recurStr)
|
||||||
|
|
||||||
tasks = append(tasks, tsk)
|
result = append(result, tsk)
|
||||||
}
|
}
|
||||||
|
|
||||||
return tasks, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SqliteTask) Delete(id string) error {
|
func (s *SqliteTask) Delete(id string) error {
|
||||||
|
|
|
@ -31,7 +31,6 @@ 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 {
|
||||||
|
@ -41,18 +40,13 @@ type Task interface {
|
||||||
Delete(id string) error
|
Delete(id string) error
|
||||||
}
|
}
|
||||||
|
|
||||||
func Match(tsk item.Task, params TaskListParams) bool {
|
func Match(tsk item.Task, req TaskListParams) bool {
|
||||||
if params.Recurrer && tsk.Recurrer == nil {
|
if req.Recurrer && tsk.Recurrer == nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
if !params.Date.IsZero() {
|
if !req.Date.IsZero() && !req.Date.Equal(tsk.Date) {
|
||||||
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
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue