feeds.ForEach
parent
05587ac28e
commit
f57408d003
|
|
@ -99,6 +99,27 @@ type (
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func (f *Feeds) ForEach(ctx context.Context, cb func(Feed) error) error {
|
||||||
|
type id struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
}
|
||||||
|
ids, err := db.Query[id](ctx, `SELECT id FROM "feed.entries" WHERE deleted_at IS NULL`)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, id := range ids {
|
||||||
|
feed, err := f.Get(ctx, id.ID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
} else if err := cb(feed); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (f *Feeds) Get(ctx context.Context, id string) (Feed, error) {
|
func (f *Feeds) Get(ctx context.Context, id string) (Feed, error) {
|
||||||
return db.QueryOne[Feed](ctx, `
|
return db.QueryOne[Feed](ctx, `
|
||||||
WITH
|
WITH
|
||||||
|
|
|
||||||
|
|
@ -107,10 +107,25 @@ func TestFeeds(t *testing.T) {
|
||||||
|
|
||||||
if err := f.Executed(ctx, got.Entry.ID, got.Version.Created); err != nil {
|
if err := f.Executed(ctx, got.Entry.ID, got.Version.Created); err != nil {
|
||||||
t.Fatal("cannot executed again:", err)
|
t.Fatal("cannot executed again:", err)
|
||||||
} else if got3, err := f.Get(ctx, id); err != nil {
|
}
|
||||||
|
got3, err := f.Get(ctx, id)
|
||||||
|
if err != nil {
|
||||||
t.Fatal("cannot get w executed again:", err)
|
t.Fatal("cannot get w executed again:", err)
|
||||||
} else if got2.Execution == got3.Execution {
|
} else if got2.Execution == got3.Execution {
|
||||||
t.Errorf("getting after second execution returned first execution")
|
t.Errorf("getting after second execution returned first execution")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
n := 0
|
||||||
|
if err := f.ForEach(ctx, func(feed feeds.Feed) error {
|
||||||
|
n += 1
|
||||||
|
if feed != got3 {
|
||||||
|
t.Errorf("for each yielded difference than last get")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}); err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
} else if n == 0 {
|
||||||
|
t.Errorf("for each didnt hit known get")
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue