From bd67eb0dfe662cdf1c3a371d399ec0794940dca0 Mon Sep 17 00:00:00 2001 From: bel Date: Mon, 5 May 2025 22:41:37 -0600 Subject: [PATCH] impl feeds.Update --- src/feeds/db.go | 33 +++++++++++++++++++++++++++++++-- src/feeds/db_test.go | 26 ++++++++++++++++++++++++++ src/feeds/http.go | 6 ++++++ src/feeds/http_test.go | 1 + src/feeds/item.go | 1 + 5 files changed, 65 insertions(+), 2 deletions(-) diff --git a/src/feeds/db.go b/src/feeds/db.go index 6979f53..59cde60 100644 --- a/src/feeds/db.go +++ b/src/feeds/db.go @@ -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 } diff --git a/src/feeds/db_test.go b/src/feeds/db_test.go index ce5efa2..3d2af5b 100644 --- a/src/feeds/db_test.go +++ b/src/feeds/db_test.go @@ -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) + } }) } diff --git a/src/feeds/http.go b/src/feeds/http.go index 6818825..5ea40d2 100644 --- a/src/feeds/http.go +++ b/src/feeds/http.go @@ -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, diff --git a/src/feeds/http_test.go b/src/feeds/http_test.go index 6648c23..71c8786 100644 --- a/src/feeds/http_test.go +++ b/src/feeds/http_test.go @@ -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...`, } diff --git a/src/feeds/item.go b/src/feeds/item.go index 6c9f33d..519ac59 100644 --- a/src/feeds/item.go +++ b/src/feeds/item.go @@ -4,6 +4,7 @@ type Items []Item type Item struct { Title string + Link string Links []string Preview string Body string