planner/sync/service/memory.go

80 lines
1.5 KiB
Go
Raw Normal View History

2024-09-18 07:40:50 +02:00
package main
2024-08-20 08:34:11 +02:00
2024-08-22 07:18:50 +02:00
import (
2024-09-11 07:33:22 +02:00
"slices"
2024-09-23 07:48:18 +02:00
"sync"
2024-08-22 07:18:50 +02:00
"time"
2024-09-18 07:55:14 +02:00
2024-09-20 07:09:30 +02:00
"go-mod.ewintr.nl/planner/item"
2024-08-22 07:18:50 +02:00
)
2024-08-16 14:25:06 +02:00
type Memory struct {
2024-09-18 07:55:14 +02:00
items map[string]item.Item
2024-09-23 07:48:18 +02:00
mutex sync.RWMutex
2024-08-16 14:25:06 +02:00
}
func NewMemory() *Memory {
return &Memory{
2024-09-18 07:55:14 +02:00
items: make(map[string]item.Item),
2024-08-16 14:25:06 +02:00
}
}
2024-12-01 10:22:47 +01:00
func (m *Memory) Update(item item.Item, ts time.Time) error {
2024-09-23 07:48:18 +02:00
m.mutex.Lock()
defer m.mutex.Unlock()
2024-12-01 10:22:47 +01:00
item.Updated = ts
2024-08-28 07:21:02 +02:00
m.items[item.ID] = item
2024-08-23 10:52:17 +02:00
return nil
}
2024-09-18 07:55:14 +02:00
func (m *Memory) Updated(kinds []item.Kind, timestamp time.Time) ([]item.Item, error) {
2024-09-23 07:48:18 +02:00
m.mutex.RLock()
defer m.mutex.RUnlock()
2024-09-18 07:55:14 +02:00
result := make([]item.Item, 0)
2024-08-22 07:18:50 +02:00
for _, i := range m.items {
2024-09-11 07:33:22 +02:00
timeOK := timestamp.IsZero() || i.Updated.Equal(timestamp) || i.Updated.After(timestamp)
kindOK := len(kinds) == 0 || slices.Contains(kinds, i.Kind)
if timeOK && kindOK {
2024-08-22 07:18:50 +02:00
result = append(result, i)
}
}
return result, nil
}
2024-12-01 10:22:47 +01:00
func (m *Memory) RecursBefore(date time.Time) ([]item.Item, error) {
2024-12-22 11:43:37 +01:00
// res := make([]item.Item, 0)
// for _, i := range m.items {
// if i.Recurrer == nil {
// continue
// }
// if i.RecurNext.Before(date) {
// res = append(res, i)
// }
// }
// return res, nil
return nil, nil
2024-12-01 10:22:47 +01:00
}
func (m *Memory) RecursNext(id string, date time.Time, ts time.Time) error {
2024-12-22 11:43:37 +01:00
// i, ok := m.items[id]
// if !ok {
// return ErrNotFound
// }
// if i.Recurrer == nil {
// return ErrNotARecurrer
// }
// if !i.Recurrer.On(date) {
// return fmt.Errorf("item does not recur on %v", date)
// }
// i.RecurNext = date
// i.Updated = ts
// m.items[id] = i
2024-12-01 10:22:47 +01:00
return nil
}