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) s.logger.Info(msg)
return return
} }
it.Updated = time.Now() if err := s.syncer.Update(it, time.Now()); err != nil {
if err := s.syncer.Update(it); err != nil {
msg := err.Error() msg := err.Error()
http.Error(w, fmtError(msg), http.StatusInternalServerError) http.Error(w, fmtError(msg), http.StatusInternalServerError)
s.logger.Error(msg) 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() m.mutex.Lock()
defer m.mutex.Unlock() defer m.mutex.Unlock()
item.Updated = ts
m.items[item.ID] = item m.items[item.ID] = item
return nil return nil
@ -59,7 +60,7 @@ func (m *Memory) RecursBefore(date time.Time) ([]item.Item, error) {
return res, nil 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] i, ok := m.items[id]
if !ok { if !ok {
return ErrNotFound 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) return fmt.Errorf("item does not recur on %v", date)
} }
i.RecurNext = date i.RecurNext = date
i.Updated = time.Now() i.Updated = ts
m.items[id] = i m.items[id] = i
return nil return nil

View File

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

View File

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

View File

@ -1,8 +1,8 @@
package main package main
import ( import (
"io"
"log/slog" "log/slog"
"os"
"testing" "testing"
"time" "time"
@ -14,7 +14,7 @@ func TestRecur(t *testing.T) {
now := time.Date(2024, 1, 1, 10, 0, 0, 0, time.UTC) now := time.Date(2024, 1, 1, 10, 0, 0, 0, time.UTC)
mem := NewMemory() 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 // Create a recurring item
recur := &item.Recur{ recur := &item.Recur{

View File

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