From bb5058c45abe1d5f565fa9d5d2e1d4cdc3f2fcea Mon Sep 17 00:00:00 2001 From: Erik Winter Date: Thu, 12 Dec 2024 14:00:45 +0100 Subject: [PATCH] wip --- sync/service/handler.go | 3 +-- sync/service/memory.go | 7 ++++--- sync/service/postgres.go | 2 +- sync/service/recur.go | 9 +++++++-- sync/service/recur_test.go | 4 ++-- sync/service/storage.go | 4 ++-- 6 files changed, 17 insertions(+), 12 deletions(-) diff --git a/sync/service/handler.go b/sync/service/handler.go index e5872dc..e93c10c 100644 --- a/sync/service/handler.go +++ b/sync/service/handler.go @@ -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) diff --git a/sync/service/memory.go b/sync/service/memory.go index 97439a6..ca92346 100644 --- a/sync/service/memory.go +++ b/sync/service/memory.go @@ -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 diff --git a/sync/service/postgres.go b/sync/service/postgres.go index f5eed03..0fe8361 100644 --- a/sync/service/postgres.go +++ b/sync/service/postgres.go @@ -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) diff --git a/sync/service/recur.go b/sync/service/recur.go index 14ca655..ad9d974 100644 --- a/sync/service/recur.go +++ b/sync/service/recur.go @@ -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 } } diff --git a/sync/service/recur_test.go b/sync/service/recur_test.go index fe9dccc..15123ca 100644 --- a/sync/service/recur_test.go +++ b/sync/service/recur_test.go @@ -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{ diff --git a/sync/service/storage.go b/sync/service/storage.go index 4ea186f..7240bbb 100644 --- a/sync/service/storage.go +++ b/sync/service/storage.go @@ -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 }