introduce localtask

This commit is contained in:
Erik Winter 2021-08-20 09:06:35 +02:00
parent 9d36a37246
commit fc4cf55e78
11 changed files with 81 additions and 66 deletions

View File

@ -11,7 +11,7 @@ func FormatError(err error) string {
return fmt.Sprintf("could not perform command.\n\nerror: %s\n", err.Error()) return fmt.Sprintf("could not perform command.\n\nerror: %s\n", err.Error())
} }
func FormatTaskTable(local storage.LocalRepository, tasks []*task.Task) string { func FormatTaskTable(local storage.LocalRepository, tasks []*task.LocalTask) string {
if len(tasks) == 0 { if len(tasks) == 0 {
return "no tasks to display\n" return "no tasks to display\n"
} }
@ -29,7 +29,7 @@ func FormatTaskTable(local storage.LocalRepository, tasks []*task.Task) string {
return output return output
} }
func FormatTask(id int, t *task.Task) string { func FormatTask(id int, t *task.LocalTask) string {
output := fmt.Sprintf(`folder: %s output := fmt.Sprintf(`folder: %s
action: %s action: %s
project: %s project: %s

View File

@ -41,7 +41,7 @@ type List struct {
} }
type ListResult struct { type ListResult struct {
Tasks []*task.Task Tasks []*task.LocalTask
} }
func NewList(local storage.LocalRepository, reqs ListReqs) *List { func NewList(local storage.LocalRepository, reqs ListReqs) *List {
@ -61,7 +61,7 @@ func (l *List) Process() (*ListResult, error) {
folders = []string{l.reqs.Folder} folders = []string{l.reqs.Folder}
} }
var potentialTasks []*task.Task var potentialTasks []*task.LocalTask
for _, folder := range folders { for _, folder := range folders {
folderTasks, err := l.local.FindAllInFolder(folder) folderTasks, err := l.local.FindAllInFolder(folder)
if err != nil { if err != nil {
@ -79,7 +79,7 @@ func (l *List) Process() (*ListResult, error) {
} }
if l.reqs.Project != "" { if l.reqs.Project != "" {
var projectTasks []*task.Task var projectTasks []*task.LocalTask
for _, pt := range potentialTasks { for _, pt := range potentialTasks {
if pt.Project == l.reqs.Project { if pt.Project == l.reqs.Project {
projectTasks = append(projectTasks, pt) projectTasks = append(projectTasks, pt)
@ -95,7 +95,7 @@ func (l *List) Process() (*ListResult, error) {
}, nil }, nil
} }
dueTasks := []*task.Task{} dueTasks := []*task.LocalTask{}
for _, t := range potentialTasks { for _, t := range potentialTasks {
switch { switch {
case t.Due.IsZero(): case t.Due.IsZero():

View File

@ -47,7 +47,9 @@ func TestListProcess(t *testing.T) {
Project: "project2", Project: "project2",
} }
allTasks := []*task.Task{task1, task2, task3, task4} allTasks := []*task.Task{task1, task2, task3, task4}
localTask2 := &task.LocalTask{Task: *task2}
localTask3 := &task.LocalTask{Task: *task3}
localTask4 := &task.LocalTask{Task: *task4}
local := storage.NewMemory() local := storage.NewMemory()
test.OK(t, local.SetTasks(allTasks)) test.OK(t, local.SetTasks(allTasks))
@ -60,14 +62,14 @@ func TestListProcess(t *testing.T) {
for _, tc := range []struct { for _, tc := range []struct {
name string name string
reqs process.ListReqs reqs process.ListReqs
exp []*task.Task exp []*task.LocalTask
}{ }{
{ {
name: "due", name: "due",
reqs: process.ListReqs{ reqs: process.ListReqs{
Due: date2, Due: date2,
}, },
exp: []*task.Task{task3}, exp: []*task.LocalTask{localTask3},
}, },
{ {
name: "due and before", name: "due and before",
@ -75,21 +77,21 @@ func TestListProcess(t *testing.T) {
Due: date2, Due: date2,
IncludeBefore: true, IncludeBefore: true,
}, },
exp: []*task.Task{task2, task3}, exp: []*task.LocalTask{localTask2, localTask3},
}, },
{ {
name: "folder", name: "folder",
reqs: process.ListReqs{ reqs: process.ListReqs{
Folder: task.FOLDER_PLANNED, Folder: task.FOLDER_PLANNED,
}, },
exp: []*task.Task{task2, task3, task4}, exp: []*task.LocalTask{localTask2, localTask3, localTask4},
}, },
{ {
name: "project", name: "project",
reqs: process.ListReqs{ reqs: process.ListReqs{
Project: "project2", Project: "project2",
}, },
exp: []*task.Task{task2, task4}, exp: []*task.LocalTask{localTask2, localTask4},
}, },
} { } {
t.Run(tc.name, func(t *testing.T) { t.Run(tc.name, func(t *testing.T) {

View File

@ -24,7 +24,7 @@ func NewProjects(local storage.LocalRepository) *Projects {
} }
func (p *Projects) Process() ([]string, error) { func (p *Projects) Process() ([]string, error) {
allTasks := []*task.Task{} allTasks := []*task.LocalTask{}
for _, folder := range []string{task.FOLDER_NEW, task.FOLDER_PLANNED, task.FOLDER_UNPLANNED} { for _, folder := range []string{task.FOLDER_NEW, task.FOLDER_PLANNED, task.FOLDER_UNPLANNED} {
folderTasks, err := p.local.FindAllInFolder(folder) folderTasks, err := p.local.FindAllInFolder(folder)
if err != nil { if err != nil {

View File

@ -24,6 +24,9 @@ func TestSyncProcess(t *testing.T) {
Folder: task.FOLDER_UNPLANNED, Folder: task.FOLDER_UNPLANNED,
} }
localTask1 := &task.LocalTask{Task: *task1}
localTask2 := &task.LocalTask{Task: *task2}
mstorer, err := mstore.NewMemory(task.KnownFolders) mstorer, err := mstore.NewMemory(task.KnownFolders)
test.OK(t, err) test.OK(t, err)
test.OK(t, mstorer.Add(task1.Folder, task1.FormatSubject(), task1.FormatBody())) test.OK(t, mstorer.Add(task1.Folder, task1.FormatSubject(), task1.FormatBody()))
@ -37,8 +40,8 @@ func TestSyncProcess(t *testing.T) {
test.Equals(t, 2, actResult.Count) test.Equals(t, 2, actResult.Count)
actTasks1, err := local.FindAllInFolder(task.FOLDER_NEW) actTasks1, err := local.FindAllInFolder(task.FOLDER_NEW)
test.OK(t, err) test.OK(t, err)
test.Equals(t, []*task.Task{task1}, actTasks1) test.Equals(t, []*task.LocalTask{localTask1}, actTasks1)
actTasks2, err := local.FindAllInFolder(task.FOLDER_UNPLANNED) actTasks2, err := local.FindAllInFolder(task.FOLDER_UNPLANNED)
test.OK(t, err) test.OK(t, err)
test.Equals(t, []*task.Task{task2}, actTasks2) test.Equals(t, []*task.LocalTask{localTask2}, actTasks2)
} }

View File

@ -52,7 +52,7 @@ func (u *Update) Process() error {
} }
} }
if err := u.disp.Dispatch(tsk); err != nil { if err := u.disp.Dispatch(&tsk.Task); err != nil {
return fmt.Errorf("%w: %v", ErrUpdateTask, err) return fmt.Errorf("%w: %v", ErrUpdateTask, err)
} }

View File

@ -15,10 +15,10 @@ var (
type LocalRepository interface { type LocalRepository interface {
LatestSync() (time.Time, error) LatestSync() (time.Time, error)
SetTasks(tasks []*task.Task) error SetTasks(tasks []*task.Task) error
FindAllInFolder(folder string) ([]*task.Task, error) FindAllInFolder(folder string) ([]*task.LocalTask, error)
FindAllInProject(project string) ([]*task.Task, error) FindAllInProject(project string) ([]*task.LocalTask, error)
FindById(id string) (*task.Task, error) FindById(id string) (*task.LocalTask, error)
FindByLocalId(id int) (*task.Task, error) FindByLocalId(id int) (*task.LocalTask, error)
LocalIds() (map[string]int, error) LocalIds() (map[string]int, error)
} }

View File

@ -48,47 +48,47 @@ func (m *Memory) setLocalId(id string) {
m.localIds[id] = next m.localIds[id] = next
} }
func (m *Memory) FindAllInFolder(folder string) ([]*task.Task, error) { func (m *Memory) FindAllInFolder(folder string) ([]*task.LocalTask, error) {
tasks := []*task.Task{} tasks := []*task.LocalTask{}
for _, t := range m.tasks { for _, t := range m.tasks {
if t.Folder == folder { if t.Folder == folder {
tasks = append(tasks, t) tasks = append(tasks, &task.LocalTask{Task: *t})
} }
} }
return tasks, nil return tasks, nil
} }
func (m *Memory) FindAllInProject(project string) ([]*task.Task, error) { func (m *Memory) FindAllInProject(project string) ([]*task.LocalTask, error) {
tasks := []*task.Task{} tasks := []*task.LocalTask{}
for _, t := range m.tasks { for _, t := range m.tasks {
if t.Project == project { if t.Project == project {
tasks = append(tasks, t) tasks = append(tasks, &task.LocalTask{Task: *t})
} }
} }
return tasks, nil return tasks, nil
} }
func (m *Memory) FindById(id string) (*task.Task, error) { func (m *Memory) FindById(id string) (*task.LocalTask, error) {
for _, t := range m.tasks { for _, t := range m.tasks {
if t.Id == id { if t.Id == id {
return t, nil return &task.LocalTask{Task: *t}, nil
} }
} }
return &task.Task{}, ErrTaskNotFound return &task.LocalTask{}, ErrTaskNotFound
} }
func (m *Memory) FindByLocalId(localId int) (*task.Task, error) { func (m *Memory) FindByLocalId(localId int) (*task.LocalTask, error) {
for _, t := range m.tasks { for _, t := range m.tasks {
if m.localIds[t.Id] == localId { if m.localIds[t.Id] == localId {
return t, nil return &task.LocalTask{Task: *t}, nil
} }
} }
return &task.Task{}, ErrTaskNotFound return &task.LocalTask{}, ErrTaskNotFound
} }
func (m *Memory) LocalIds() (map[string]int, error) { func (m *Memory) LocalIds() (map[string]int, error) {

View File

@ -41,6 +41,9 @@ func TestMemory(t *testing.T) {
}, },
} }
tasks := []*task.Task{task1, task2, task3} tasks := []*task.Task{task1, task2, task3}
localTask1 := &task.LocalTask{Task: *task1}
localTask2 := &task.LocalTask{Task: *task2}
localTask3 := &task.LocalTask{Task: *task3}
t.Run("sync", func(t *testing.T) { t.Run("sync", func(t *testing.T) {
mem := storage.NewMemory() mem := storage.NewMemory()
@ -60,7 +63,7 @@ func TestMemory(t *testing.T) {
test.OK(t, mem.SetTasks(tasks)) test.OK(t, mem.SetTasks(tasks))
act, err := mem.FindAllInFolder(folder1) act, err := mem.FindAllInFolder(folder1)
test.OK(t, err) test.OK(t, err)
exp := []*task.Task{task1, task2} exp := []*task.LocalTask{localTask1, localTask2}
for _, tsk := range exp { for _, tsk := range exp {
tsk.Message = nil tsk.Message = nil
} }
@ -72,7 +75,7 @@ func TestMemory(t *testing.T) {
test.OK(t, mem.SetTasks(tasks)) test.OK(t, mem.SetTasks(tasks))
act, err := mem.FindAllInProject(project1) act, err := mem.FindAllInProject(project1)
test.OK(t, err) test.OK(t, err)
exp := []*task.Task{task1, task3} exp := []*task.LocalTask{localTask1, localTask3}
for _, tsk := range exp { for _, tsk := range exp {
tsk.Message = nil tsk.Message = nil
} }
@ -84,7 +87,7 @@ func TestMemory(t *testing.T) {
test.OK(t, mem.SetTasks(tasks)) test.OK(t, mem.SetTasks(tasks))
act, err := mem.FindById("id-2") act, err := mem.FindById("id-2")
test.OK(t, err) test.OK(t, err)
test.Equals(t, task2, act) test.Equals(t, localTask2, act)
}) })
t.Run("findbylocalid", func(t *testing.T) { t.Run("findbylocalid", func(t *testing.T) {
@ -92,7 +95,7 @@ func TestMemory(t *testing.T) {
test.OK(t, mem.SetTasks(tasks)) test.OK(t, mem.SetTasks(tasks))
act, err := mem.FindByLocalId(2) act, err := mem.FindByLocalId(2)
test.OK(t, err) test.OK(t, err)
test.Equals(t, task2, act) test.Equals(t, localTask2, act)
}) })
t.Run("localids", func(t *testing.T) { t.Run("localids", func(t *testing.T) {

View File

@ -150,31 +150,31 @@ SET latest_sync = ?`,
return nil return nil
} }
func (s *Sqlite) FindAllInFolder(folder string) ([]*task.Task, error) { func (s *Sqlite) FindAllInFolder(folder string) ([]*task.LocalTask, error) {
rows, err := s.db.Query(` rows, err := s.db.Query(`
SELECT id, version, folder, action, project, due, recur SELECT id, version, folder, action, project, due, recur
FROM task FROM task
WHERE folder = ?`, folder) WHERE folder = ?`, folder)
if err != nil { if err != nil {
return []*task.Task{}, fmt.Errorf("%w: %v", ErrSqliteFailure, err) return []*task.LocalTask{}, fmt.Errorf("%w: %v", ErrSqliteFailure, err)
} }
return tasksFromRows(rows) return tasksFromRows(rows)
} }
func (s *Sqlite) FindAllInProject(project string) ([]*task.Task, error) { func (s *Sqlite) FindAllInProject(project string) ([]*task.LocalTask, error) {
rows, err := s.db.Query(` rows, err := s.db.Query(`
SELECT id, version, folder, action, project, due, recur SELECT id, version, folder, action, project, due, recur
FROM task FROM task
WHERE project = ?`, project) WHERE project = ?`, project)
if err != nil { if err != nil {
return []*task.Task{}, fmt.Errorf("%w: %v", ErrSqliteFailure, err) return []*task.LocalTask{}, fmt.Errorf("%w: %v", ErrSqliteFailure, err)
} }
return tasksFromRows(rows) return tasksFromRows(rows)
} }
func (s *Sqlite) FindById(id string) (*task.Task, error) { func (s *Sqlite) FindById(id string) (*task.LocalTask, error) {
var folder, action, project, due, recur string var folder, action, project, due, recur string
var version int var version int
row := s.db.QueryRow(` row := s.db.QueryRow(`
@ -183,30 +183,31 @@ FROM task
WHERE id = ? WHERE id = ?
LIMIT 1`, id) LIMIT 1`, id)
if err := row.Scan(&version, &folder, &action, &project, &due, &recur); err != nil { if err := row.Scan(&version, &folder, &action, &project, &due, &recur); err != nil {
return &task.Task{}, fmt.Errorf("%w: %v", ErrSqliteFailure, err) return &task.LocalTask{}, fmt.Errorf("%w: %v", ErrSqliteFailure, err)
} }
return &task.Task{ return &task.LocalTask{
Id: id, Task: task.Task{
Version: version, Id: id,
Folder: folder, Version: version,
Action: action, Folder: folder,
Project: project, Action: action,
Due: task.NewDateFromString(due), Project: project,
Recur: task.NewRecurrer(recur), Due: task.NewDateFromString(due),
}, nil Recur: task.NewRecurrer(recur),
}}, nil
} }
func (s *Sqlite) FindByLocalId(localId int) (*task.Task, error) { func (s *Sqlite) FindByLocalId(localId int) (*task.LocalTask, error) {
var id string var id string
row := s.db.QueryRow(`SELECT id FROM local_id WHERE local_id = ?`, localId) row := s.db.QueryRow(`SELECT id FROM local_id WHERE local_id = ?`, localId)
if err := row.Scan(&id); err != nil { if err := row.Scan(&id); err != nil {
return &task.Task{}, fmt.Errorf("%w: %v", ErrSqliteFailure, err) return &task.LocalTask{}, fmt.Errorf("%w: %v", ErrSqliteFailure, err)
} }
t, err := s.FindById(id) t, err := s.FindById(id)
if err != nil { if err != nil {
return &task.Task{}, nil return &task.LocalTask{}, nil
} }
return t, nil return t, nil
@ -232,25 +233,26 @@ func (s *Sqlite) LocalIds() (map[string]int, error) {
return idMap, nil return idMap, nil
} }
func tasksFromRows(rows *sql.Rows) ([]*task.Task, error) { func tasksFromRows(rows *sql.Rows) ([]*task.LocalTask, error) {
tasks := []*task.Task{} tasks := []*task.LocalTask{}
defer rows.Close() defer rows.Close()
for rows.Next() { for rows.Next() {
var id, folder, action, project, due, recur string var id, folder, action, project, due, recur string
var version int var version int
if err := rows.Scan(&id, &version, &folder, &action, &project, &due, &recur); err != nil { if err := rows.Scan(&id, &version, &folder, &action, &project, &due, &recur); err != nil {
return []*task.Task{}, fmt.Errorf("%w: %v", ErrSqliteFailure, err) return []*task.LocalTask{}, fmt.Errorf("%w: %v", ErrSqliteFailure, err)
} }
tasks = append(tasks, &task.Task{ tasks = append(tasks, &task.LocalTask{
Id: id, Task: task.Task{
Version: version, Id: id,
Folder: folder, Version: version,
Action: action, Folder: folder,
Project: project, Action: action,
Due: task.NewDateFromString(due), Project: project,
Recur: task.NewRecurrer(recur), Due: task.NewDateFromString(due),
}) Recur: task.NewRecurrer(recur),
}})
} }
return tasks, nil return tasks, nil

View File

@ -0,0 +1,5 @@
package task
type LocalTask struct {
Task
}