planner/plan/storage/storage.go

95 lines
1.6 KiB
Go
Raw Normal View History

2024-09-30 07:34:40 +02:00
package storage
2024-10-03 07:32:48 +02:00
import (
"errors"
"sort"
2024-10-07 11:11:18 +02:00
"time"
2024-09-30 07:34:40 +02:00
2024-10-03 07:32:48 +02:00
"go-mod.ewintr.nl/planner/item"
)
var (
ErrNotFound = errors.New("not found")
)
type LocalID interface {
2024-12-29 09:32:49 +01:00
FindOne(lid int) (string, error)
2024-10-03 07:32:48 +02:00
FindAll() (map[string]int, error)
2024-10-07 11:11:18 +02:00
FindOrNext(id string) (int, error)
2024-10-03 07:32:48 +02:00
Next() (int, error)
Store(id string, localID int) error
Delete(id string) error
}
2024-10-07 11:11:18 +02:00
type Sync interface {
FindAll() ([]item.Item, error)
Store(i item.Item) error
DeleteAll() error
LastUpdate() (time.Time, error)
}
2024-12-29 11:31:33 +01:00
type TaskListParams struct {
2024-12-29 12:36:27 +01:00
Recurrer bool
Date item.Date
IncludeBefore bool
2024-12-29 11:31:33 +01:00
}
2024-12-24 08:00:23 +01:00
type Task interface {
Store(task item.Task) error
2024-12-29 11:31:33 +01:00
FindOne(id string) (item.Task, error)
FindMany(params TaskListParams) ([]item.Task, error)
2024-09-30 07:34:40 +02:00
Delete(id string) error
}
2024-10-03 07:32:48 +02:00
2024-12-29 12:36:27 +01:00
func Match(tsk item.Task, params TaskListParams) bool {
if params.Recurrer && tsk.Recurrer == nil {
2024-12-29 11:31:33 +01:00
return false
}
2024-12-29 12:36:27 +01:00
if !params.Date.IsZero() {
if !params.IncludeBefore && !params.Date.Equal(tsk.Date) {
return false
}
if params.IncludeBefore && tsk.Date.After(params.Date) {
return false
}
2024-12-29 11:31:33 +01:00
}
return true
}
2024-10-03 07:32:48 +02:00
func NextLocalID(used []int) int {
if len(used) == 0 {
return 1
}
sort.Ints(used)
usedMax := 1
for _, u := range used {
if u > usedMax {
usedMax = u
}
}
var limit int
for limit = 1; limit <= len(used) || limit < usedMax; limit *= 10 {
}
newId := used[len(used)-1] + 1
if newId < limit {
return newId
}
usedMap := map[int]bool{}
for _, u := range used {
usedMap[u] = true
}
for i := 1; i < limit; i++ {
if _, ok := usedMap[i]; !ok {
return i
}
}
return limit
}