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 {
|
2021-08-25 06:52:48 +02:00
|
|
|
tasks map[string]*task.LocalTask
|
2021-06-25 09:14:27 +02:00
|
|
|
latestSync time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewMemory() *Memory {
|
|
|
|
return &Memory{
|
2021-08-25 06:52:48 +02:00
|
|
|
tasks: map[string]*task.LocalTask{},
|
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-08-22 13:29:04 +02:00
|
|
|
func (m *Memory) SetLocalUpdate(tsk *task.LocalTask) error {
|
2021-09-03 09:19:36 +02:00
|
|
|
tsk.LocalStatus = task.STATUS_UPDATED
|
2021-08-25 06:52:48 +02:00
|
|
|
m.tasks[tsk.Id] = tsk
|
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
|
|
|
|
}
|