2021-06-25 09:14:27 +02:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
2021-09-19 11:59:26 +02:00
|
|
|
"ewintr.nl/gte/internal/task"
|
2021-09-04 12:20:35 +02:00
|
|
|
"github.com/google/uuid"
|
2021-06-25 09:14:27 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Memory is an in memory implementation of LocalRepository
|
|
|
|
type Memory struct {
|
2021-08-25 06:52:48 +02:00
|
|
|
tasks map[string]*task.LocalTask
|
2021-06-25 09:14:27 +02:00
|
|
|
latestSync time.Time
|
|
|
|
}
|
|
|
|
|
2022-10-23 12:45:21 +02:00
|
|
|
func NewMemory(initTasks ...*task.Task) *Memory {
|
|
|
|
tasks := map[string]*task.LocalTask{}
|
|
|
|
for _, t := range initTasks {
|
|
|
|
tasks[t.Id] = &task.LocalTask{
|
|
|
|
Task: *t,
|
|
|
|
LocalUpdate: &task.LocalUpdate{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-25 09:14:27 +02:00
|
|
|
return &Memory{
|
2022-10-23 12:45:21 +02:00
|
|
|
tasks: tasks,
|
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 {
|
2021-08-25 06:52:48 +02:00
|
|
|
var oldTasks []*task.LocalTask
|
|
|
|
for _, ot := range m.tasks {
|
|
|
|
oldTasks = append(oldTasks, ot)
|
2021-06-25 09:14:27 +02:00
|
|
|
}
|
|
|
|
|
2021-08-25 06:52:48 +02:00
|
|
|
newTasks := MergeNewTaskSet(oldTasks, tasks)
|
2021-07-14 07:17:53 +02:00
|
|
|
|
2021-08-25 06:52:48 +02:00
|
|
|
m.tasks = map[string]*task.LocalTask{}
|
|
|
|
for _, nt := range newTasks {
|
|
|
|
m.tasks[nt.Id] = nt
|
2021-06-25 09:14:27 +02:00
|
|
|
}
|
2021-08-25 06:52:48 +02:00
|
|
|
m.latestSync = time.Now()
|
2021-06-25 09:14:27 +02:00
|
|
|
|
2021-08-25 06:52:48 +02:00
|
|
|
return nil
|
2021-06-25 09:14:27 +02:00
|
|
|
}
|
|
|
|
|
2021-08-25 06:52:48 +02:00
|
|
|
func (m *Memory) FindAll() ([]*task.LocalTask, error) {
|
2021-08-20 09:06:35 +02:00
|
|
|
tasks := []*task.LocalTask{}
|
2021-06-25 09:14:27 +02:00
|
|
|
for _, t := range m.tasks {
|
2021-08-25 06:52:48 +02:00
|
|
|
tasks = append(tasks, t)
|
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-25 06:52:48 +02:00
|
|
|
return t, 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 {
|
2021-08-25 06:52:48 +02:00
|
|
|
if t.LocalId == localId {
|
|
|
|
return t, 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-21 20:47:29 +02:00
|
|
|
|
2021-09-04 12:20:35 +02:00
|
|
|
func (m *Memory) SetLocalUpdate(id string, update *task.LocalUpdate) error {
|
|
|
|
m.tasks[id].LocalStatus = task.STATUS_UPDATED
|
|
|
|
m.tasks[id].LocalUpdate = update
|
2021-08-21 20:47:29 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2021-09-03 09:19:36 +02:00
|
|
|
|
|
|
|
func (m *Memory) MarkDispatched(localId int) error {
|
|
|
|
t, _ := m.FindByLocalId(localId)
|
|
|
|
m.tasks[t.Id].LocalStatus = task.STATUS_DISPATCHED
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2021-09-04 12:20:35 +02:00
|
|
|
|
|
|
|
func (m *Memory) Add(update *task.LocalUpdate) (*task.LocalTask, error) {
|
|
|
|
var used []int
|
|
|
|
for _, t := range m.tasks {
|
|
|
|
used = append(used, t.LocalId)
|
|
|
|
}
|
|
|
|
|
|
|
|
tsk := &task.LocalTask{
|
|
|
|
Task: task.Task{
|
|
|
|
Id: uuid.New().String(),
|
|
|
|
Version: 0,
|
|
|
|
Folder: task.FOLDER_NEW,
|
|
|
|
},
|
|
|
|
LocalId: NextLocalId(used),
|
|
|
|
LocalStatus: task.STATUS_UPDATED,
|
|
|
|
LocalUpdate: update,
|
|
|
|
}
|
|
|
|
|
|
|
|
m.tasks[tsk.Id] = tsk
|
|
|
|
|
|
|
|
return tsk, nil
|
|
|
|
}
|