2021-06-25 09:14:27 +02:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"git.ewintr.nl/gte/internal/task"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Memory is an in memory implementation of LocalRepository
|
|
|
|
type Memory struct {
|
|
|
|
tasks []*task.Task
|
|
|
|
latestSync time.Time
|
2021-07-14 07:17:53 +02:00
|
|
|
localIds map[string]int
|
2021-06-25 09:14:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewMemory() *Memory {
|
|
|
|
return &Memory{
|
2021-07-14 07:17:53 +02:00
|
|
|
tasks: []*task.Task{},
|
|
|
|
localIds: map[string]int{},
|
2021-06-25 09:14:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Memory) LatestSync() (time.Time, error) {
|
|
|
|
return m.latestSync, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Memory) SetTasks(tasks []*task.Task) error {
|
|
|
|
nTasks := []*task.Task{}
|
|
|
|
for _, t := range tasks {
|
|
|
|
nt := *t
|
|
|
|
nt.Message = nil
|
|
|
|
nTasks = append(nTasks, &nt)
|
2021-07-14 07:17:53 +02:00
|
|
|
m.setLocalId(t.Id)
|
2021-06-25 09:14:27 +02:00
|
|
|
}
|
|
|
|
m.tasks = nTasks
|
|
|
|
m.latestSync = time.Now()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-07-14 07:17:53 +02:00
|
|
|
func (m *Memory) setLocalId(id string) {
|
|
|
|
used := []int{}
|
|
|
|
for _, id := range m.localIds {
|
|
|
|
used = append(used, id)
|
|
|
|
}
|
|
|
|
|
|
|
|
next := NextLocalId(used)
|
|
|
|
m.localIds[id] = next
|
|
|
|
}
|
|
|
|
|
2021-08-20 09:06:35 +02:00
|
|
|
func (m *Memory) FindAllInFolder(folder string) ([]*task.LocalTask, error) {
|
|
|
|
tasks := []*task.LocalTask{}
|
2021-06-25 09:14:27 +02:00
|
|
|
for _, t := range m.tasks {
|
|
|
|
if t.Folder == folder {
|
2021-08-20 09:30:49 +02:00
|
|
|
tasks = append(tasks, &task.LocalTask{
|
|
|
|
Task: *t,
|
|
|
|
LocalId: m.localIds[t.Id],
|
|
|
|
})
|
2021-06-25 09:14:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return tasks, nil
|
|
|
|
}
|
|
|
|
|
2021-08-20 09:06:35 +02:00
|
|
|
func (m *Memory) FindAllInProject(project string) ([]*task.LocalTask, error) {
|
|
|
|
tasks := []*task.LocalTask{}
|
2021-06-25 09:14:27 +02:00
|
|
|
for _, t := range m.tasks {
|
|
|
|
if t.Project == project {
|
2021-08-20 09:30:49 +02:00
|
|
|
tasks = append(tasks, &task.LocalTask{
|
|
|
|
Task: *t,
|
|
|
|
LocalId: m.localIds[t.Id],
|
|
|
|
})
|
2021-06-25 09:14:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return tasks, nil
|
|
|
|
}
|
2021-07-10 12:30:38 +02:00
|
|
|
|
2021-08-20 09:06:35 +02:00
|
|
|
func (m *Memory) FindById(id string) (*task.LocalTask, error) {
|
2021-07-10 12:30:38 +02:00
|
|
|
for _, t := range m.tasks {
|
|
|
|
if t.Id == id {
|
2021-08-20 09:30:49 +02:00
|
|
|
return &task.LocalTask{
|
|
|
|
Task: *t,
|
|
|
|
LocalId: m.localIds[t.Id],
|
|
|
|
}, nil
|
2021-07-10 12:30:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-20 09:06:35 +02:00
|
|
|
return &task.LocalTask{}, ErrTaskNotFound
|
2021-07-10 12:30:38 +02:00
|
|
|
}
|
2021-07-14 07:17:53 +02:00
|
|
|
|
2021-08-20 09:06:35 +02:00
|
|
|
func (m *Memory) FindByLocalId(localId int) (*task.LocalTask, error) {
|
2021-07-14 07:17:53 +02:00
|
|
|
for _, t := range m.tasks {
|
|
|
|
if m.localIds[t.Id] == localId {
|
2021-08-20 09:30:49 +02:00
|
|
|
return &task.LocalTask{
|
|
|
|
Task: *t,
|
|
|
|
LocalId: localId,
|
|
|
|
}, nil
|
2021-07-14 07:17:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-20 09:06:35 +02:00
|
|
|
return &task.LocalTask{}, ErrTaskNotFound
|
2021-07-14 07:17:53 +02:00
|
|
|
}
|