impl feeds.Update

main
bel 2025-05-05 22:41:37 -06:00
parent 7a94c74226
commit bd67eb0dfe
5 changed files with 65 additions and 2 deletions

View File

@ -119,10 +119,9 @@ func Get(ctx context.Context, id string) (Feed, error) {
LIMIT 1
) AS "Execution.Version"
FROM entry
JOIN "feed.versions" version_entries_id ON
version_entries_id.entries_id=entry.ID
JOIN "feed.versions" versions ON
versions.created_at=entry.Updated
WHERE versions.entries_id=entry.ID
`, id, id)
}
@ -177,6 +176,36 @@ func Insert(ctx context.Context, url, cron, pattern, webhookMethod, webhookURL,
)
}
func Update(ctx context.Context, id string, url, cron, pattern, webhookMethod, webhookURL, webhookBody string) error {
if err := initDB(ctx); err != nil {
return err
}
if _, err := Get(ctx, id); err != nil {
return err
}
now := time.Now()
return db.Exec(ctx, `
BEGIN;
UPDATE "feed.entries" SET updated_at=$1 WHERE id=$2;
INSERT INTO "feed.versions" (
entries_id,
created_at,
url,
cron,
pattern,
webhook_method,
webhook_url,
webhook_body
) VALUES ($3, $4, $5, $6, $7, $8, $9, $10);
COMMIT;
`,
now, id,
id, now, url, cron, pattern, webhookMethod, webhookURL, webhookBody,
)
}
func (feed Feed) Update(ctx context.Context, url, cron, pattern, tag *string) error {
return io.EOF
}

View File

@ -112,5 +112,31 @@ func TestFeeds(t *testing.T) {
} else if n == 0 {
t.Errorf("for each didnt hit known get")
}
if err := feeds.Update(ctx, id, "url2", "cron2", "pattern2", "wmethod2", "wurl2", "wbody2"); err != nil {
t.Fatal("cannot update:", err)
}
got, err = feeds.Get(ctx, id)
if err != nil {
t.Fatal("cannot get updated:", err)
}
if v := got.Version.URL; v != "url2" {
t.Error(v)
}
if v := got.Version.Cron; v != "cron2" {
t.Error(v)
}
if v := got.Version.Pattern; v != "pattern2" {
t.Error(v)
}
if v := got.Version.WebhookMethod; v != "wmethod2" {
t.Error(v)
}
if v := got.Version.WebhookURL; v != "wurl2" {
t.Error(v)
}
if v := got.Version.WebhookBody; v != "wbody2" {
t.Error(v)
}
})
}

View File

@ -100,8 +100,14 @@ func (feed Feed) Fetch(ctx context.Context) (Items, error) {
slices.Sort(links)
links = slices.Compact(links)
var link string
if len(links) > 0 {
link = links[0]
}
result = append(result, Item{
Title: gitem.Title,
Link: link,
Links: links,
Preview: preview,
Body: body,

View File

@ -67,6 +67,7 @@ func TestFeedFetch(t *testing.T) {
expect := feeds.Item{
Title: `Cheap 'Transforming' Electric Truck Announced by Jeff Bezos-Backed Startup`,
Link: `https://tech.slashdot.org/story/25/04/26/0425259/cheap-transforming-electric-truck-announced-by-jeff-bezos-backed-startup?utm_source=rss1.0mainlinkanon&utm_medium=feed`,
Links: []string{`https://tech.slashdot.org/story/25/04/26/0425259/cheap-transforming-electric-truck-announced-by-jeff-bezos-backed-startup?utm_source=rss1.0mainlinkanon&utm_medium=feed`},
Preview: `It's a pickup truck "that can change into whatever...`,
}

View File

@ -4,6 +4,7 @@ type Items []Item
type Item struct {
Title string
Link string
Links []string
Preview string
Body string