planner/plan/storage/memory/task.go

68 lines
1.1 KiB
Go
Raw Normal View History

2024-12-24 08:00:23 +01:00
package memory
import (
"sort"
"sync"
"go-mod.ewintr.nl/planner/item"
"go-mod.ewintr.nl/planner/plan/storage"
)
type Task struct {
tasks map[string]item.Task
mutex sync.RWMutex
}
func NewTask() *Task {
return &Task{
tasks: make(map[string]item.Task),
}
}
func (t *Task) Find(id string) (item.Task, error) {
t.mutex.RLock()
defer t.mutex.RUnlock()
task, exists := t.tasks[id]
if !exists {
return item.Task{}, storage.ErrNotFound
}
return task, nil
}
func (t *Task) FindAll() ([]item.Task, error) {
t.mutex.RLock()
defer t.mutex.RUnlock()
tasks := make([]item.Task, 0, len(t.tasks))
2024-12-25 10:11:56 +01:00
for _, tsk := range t.tasks {
tasks = append(tasks, tsk)
2024-12-24 08:00:23 +01:00
}
sort.Slice(tasks, func(i, j int) bool {
return tasks[i].ID < tasks[j].ID
})
return tasks, nil
}
func (t *Task) Store(tsk item.Task) error {
t.mutex.Lock()
defer t.mutex.Unlock()
t.tasks[tsk.ID] = tsk
return nil
}
func (t *Task) Delete(id string) error {
t.mutex.Lock()
defer t.mutex.Unlock()
if _, exists := t.tasks[id]; !exists {
return storage.ErrNotFound
}
delete(t.tasks, id)
return nil
}