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())
}
func FormatTaskTable(local storage.LocalRepository, tasks []*task.Task) string {
func FormatTaskTable(local storage.LocalRepository, tasks []*task.LocalTask) string {
if len(tasks) == 0 {
return "no tasks to display\n"
}
@ -29,7 +29,7 @@ func FormatTaskTable(local storage.LocalRepository, tasks []*task.Task) string {
return output
}
func FormatTask(id int, t *task.Task) string {
func FormatTask(id int, t *task.LocalTask) string {
output := fmt.Sprintf(`folder: %s
action: %s
project: %s

View File

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

View File

@ -47,7 +47,9 @@ func TestListProcess(t *testing.T) {
Project: "project2",
}
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()
test.OK(t, local.SetTasks(allTasks))
@ -60,14 +62,14 @@ func TestListProcess(t *testing.T) {
for _, tc := range []struct {
name string
reqs process.ListReqs
exp []*task.Task
exp []*task.LocalTask
}{
{
name: "due",
reqs: process.ListReqs{
Due: date2,
},
exp: []*task.Task{task3},
exp: []*task.LocalTask{localTask3},
},
{
name: "due and before",
@ -75,21 +77,21 @@ func TestListProcess(t *testing.T) {
Due: date2,
IncludeBefore: true,
},
exp: []*task.Task{task2, task3},
exp: []*task.LocalTask{localTask2, localTask3},
},
{
name: "folder",
reqs: process.ListReqs{
Folder: task.FOLDER_PLANNED,
},
exp: []*task.Task{task2, task3, task4},
exp: []*task.LocalTask{localTask2, localTask3, localTask4},
},
{
name: "project",
reqs: process.ListReqs{
Project: "project2",
},
exp: []*task.Task{task2, task4},
exp: []*task.LocalTask{localTask2, localTask4},
},
} {
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) {
allTasks := []*task.Task{}
allTasks := []*task.LocalTask{}
for _, folder := range []string{task.FOLDER_NEW, task.FOLDER_PLANNED, task.FOLDER_UNPLANNED} {
folderTasks, err := p.local.FindAllInFolder(folder)
if err != nil {

View File

@ -24,6 +24,9 @@ func TestSyncProcess(t *testing.T) {
Folder: task.FOLDER_UNPLANNED,
}
localTask1 := &task.LocalTask{Task: *task1}
localTask2 := &task.LocalTask{Task: *task2}
mstorer, err := mstore.NewMemory(task.KnownFolders)
test.OK(t, err)
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)
actTasks1, err := local.FindAllInFolder(task.FOLDER_NEW)
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)
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)
}

View File

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

View File

@ -48,47 +48,47 @@ func (m *Memory) setLocalId(id string) {
m.localIds[id] = next
}
func (m *Memory) FindAllInFolder(folder string) ([]*task.Task, error) {
tasks := []*task.Task{}
func (m *Memory) FindAllInFolder(folder string) ([]*task.LocalTask, error) {
tasks := []*task.LocalTask{}
for _, t := range m.tasks {
if t.Folder == folder {
tasks = append(tasks, t)
tasks = append(tasks, &task.LocalTask{Task: *t})
}
}
return tasks, nil
}
func (m *Memory) FindAllInProject(project string) ([]*task.Task, error) {
tasks := []*task.Task{}
func (m *Memory) FindAllInProject(project string) ([]*task.LocalTask, error) {
tasks := []*task.LocalTask{}
for _, t := range m.tasks {
if t.Project == project {
tasks = append(tasks, t)
tasks = append(tasks, &task.LocalTask{Task: *t})
}
}
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 {
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 {
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) {

View File

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

View File

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

View File

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