gte/internal/storage/memory.go

127 lines
2.6 KiB
Go
Raw Normal View History

2021-06-25 09:14:27 +02:00
package storage
import (
"time"
"git.ewintr.nl/gte/internal/task"
)
type localData struct {
LocalId int
LocalUpdate *task.LocalUpdate
}
2021-06-25 09:14:27 +02:00
// Memory is an in memory implementation of LocalRepository
2021-08-24 07:26:58 +02:00
//
// It is meant for testing and does not make an attempt to
// keep local state between consecutive calls to SetTasks()
2021-06-25 09:14:27 +02:00
type Memory struct {
tasks []*task.Task
latestSync time.Time
localData map[string]localData
2021-06-25 09:14:27 +02:00
}
func NewMemory() *Memory {
return &Memory{
tasks: []*task.Task{},
localData: map[string]localData{},
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 _, ld := range m.localData {
used = append(used, ld.LocalId)
2021-07-14 07:17:53 +02:00
}
next := NextLocalId(used)
m.localData[id] = localData{
LocalId: next,
}
2021-07-14 07:17:53 +02:00
}
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.localData[t.Id].LocalId,
LocalUpdate: m.localData[t.Id].LocalUpdate,
2021-08-20 09:30:49 +02:00
})
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.localData[t.Id].LocalId,
LocalUpdate: m.localData[t.Id].LocalUpdate,
2021-08-20 09:30:49 +02:00
})
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.localData[t.Id].LocalId,
LocalUpdate: m.localData[t.Id].LocalUpdate,
2021-08-20 09:30:49 +02:00
}, 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.localData[t.Id].LocalId == localId {
2021-08-20 09:30:49 +02:00
return &task.LocalTask{
Task: *t,
LocalId: localId,
LocalUpdate: m.localData[t.Id].LocalUpdate,
2021-08-20 09:30:49 +02:00
}, 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
}
2021-08-22 13:29:04 +02:00
func (m *Memory) SetLocalUpdate(tsk *task.LocalTask) error {
m.localData[tsk.Id] = localData{
LocalId: tsk.LocalId,
LocalUpdate: tsk.LocalUpdate,
}
return nil
}