This commit is contained in:
Erik Winter 2024-12-12 14:00:45 +01:00
parent a70f13be35
commit bb5058c45a
6 changed files with 17 additions and 12 deletions

View File

@ -146,8 +146,7 @@ func (s *Server) SyncPost(w http.ResponseWriter, r *http.Request) {
s.logger.Info(msg)
return
}
it.Updated = time.Now()
if err := s.syncer.Update(it); err != nil {
if err := s.syncer.Update(it, time.Now()); err != nil {
msg := err.Error()
http.Error(w, fmtError(msg), http.StatusInternalServerError)
s.logger.Error(msg)

View File

@ -20,10 +20,11 @@ func NewMemory() *Memory {
}
}
func (m *Memory) Update(item item.Item) error {
func (m *Memory) Update(item item.Item, ts time.Time) error {
m.mutex.Lock()
defer m.mutex.Unlock()
item.Updated = ts
m.items[item.ID] = item
return nil
@ -59,7 +60,7 @@ func (m *Memory) RecursBefore(date time.Time) ([]item.Item, error) {
return res, nil
}
func (m *Memory) RecursNext(id string, date time.Time) error {
func (m *Memory) RecursNext(id string, date time.Time, ts time.Time) error {
i, ok := m.items[id]
if !ok {
return ErrNotFound
@ -71,7 +72,7 @@ func (m *Memory) RecursNext(id string, date time.Time) error {
return fmt.Errorf("item does not recur on %v", date)
}
i.RecurNext = date
i.Updated = time.Now()
i.Updated = ts
m.items[id] = i
return nil

View File

@ -157,7 +157,7 @@ func (p *Postgres) RecursNext(id string, date time.Time) error {
_, err = p.db.Exec(`
UPDATE items
SET recur_next = $1
SET recur_next = $1 // TODO set updated
WHERE id = $2`, date, id)
if err != nil {
return fmt.Errorf("%w: %v", ErrPostgresFailure, err)

View File

@ -4,6 +4,7 @@ import (
"log/slog"
"time"
"github.com/google/uuid"
"go-mod.ewintr.nl/planner/item"
)
@ -46,17 +47,21 @@ func (r *Recur) Recur() error {
return err
}
y, m, d := i.RecurNext.Date()
ne.ID = uuid.New().String()
ne.Recurrer = nil
ne.RecurNext = time.Time{}
ne.Start = time.Date(y, m, d, ne.Start.Hour(), ne.Start.Minute(), 0, 0, time.UTC)
ni, err := ne.Item()
if err != nil {
return err
}
if err := r.repoSync.Update(ni); err != nil {
if err := r.repoSync.Update(ni, time.Now()); err != nil {
return err
}
// set next
if err := r.repoRecur.RecursNext(i.ID, i.Recurrer.NextAfter(i.RecurNext)); err != nil {
if err := r.repoRecur.RecursNext(i.ID, i.Recurrer.NextAfter(i.RecurNext), time.Now()); err != nil {
return err
}
}

View File

@ -1,8 +1,8 @@
package main
import (
"io"
"log/slog"
"os"
"testing"
"time"
@ -14,7 +14,7 @@ func TestRecur(t *testing.T) {
now := time.Date(2024, 1, 1, 10, 0, 0, 0, time.UTC)
mem := NewMemory()
rec := NewRecur(mem, mem, time.Second, slog.New(slog.NewJSONHandler(os.Stdout, nil)))
rec := NewRecur(mem, mem, time.Second, slog.New(slog.NewTextHandler(io.Discard, nil)))
// Create a recurring item
recur := &item.Recur{

View File

@ -13,11 +13,11 @@ var (
)
type Syncer interface {
Update(item item.Item) error
Update(item item.Item, t time.Time) error
Updated(kind []item.Kind, t time.Time) ([]item.Item, error)
}
type Recurrer interface {
RecursBefore(date time.Time) ([]item.Item, error)
RecursNext(id string, date time.Time) error
RecursNext(id string, date time.Time, t time.Time) error
}