Compare commits

..

2 Commits

Author SHA1 Message Date
bel
d7f098bea0 ui can edit 2025-05-05 22:51:06 -06:00
bel
bd67eb0dfe impl feeds.Update 2025-05-05 22:41:37 -06:00
7 changed files with 74 additions and 8 deletions

View File

@@ -10,18 +10,18 @@ import (
func (h Handler) feeds(w http.ResponseWriter, r *http.Request) error { func (h Handler) feeds(w http.ResponseWriter, r *http.Request) error {
switch r.Method { switch r.Method {
case http.MethodPost: case http.MethodPost, http.MethodPut:
if err := r.ParseForm(); err != nil { if err := r.ParseForm(); err != nil {
return err return err
} }
return h.feedsPost(r.Context(), r.Form) return h.feedsUpsert(r.Context(), r.URL.Query().Get("id"), r.Form)
default: default:
http.NotFound(w, r) http.NotFound(w, r)
} }
return nil return nil
} }
func (h Handler) feedsPost(ctx context.Context, form url.Values) error { func (h Handler) feedsUpsert(ctx context.Context, id string, form url.Values) error {
var req feeds.Version var req feeds.Version
for k, v := range map[string]*string{ for k, v := range map[string]*string{
"Cron": &req.Cron, "Cron": &req.Cron,
@@ -36,6 +36,9 @@ func (h Handler) feedsPost(ctx context.Context, form url.Values) error {
} }
} }
if id == "" {
_, err := feeds.Insert(ctx, req.URL, req.Cron, req.Pattern, req.WebhookMethod, req.WebhookURL, req.WebhookBody) _, err := feeds.Insert(ctx, req.URL, req.Cron, req.Pattern, req.WebhookMethod, req.WebhookURL, req.WebhookBody)
return err return err
} }
return feeds.Update(ctx, id, req.URL, req.Cron, req.Pattern, req.WebhookMethod, req.WebhookURL, req.WebhookBody)
}

View File

@@ -26,7 +26,7 @@
Update <code><a target="_blank" href="{{ .editing_url }}">{{ .editing.URL }}</a></code> (<a href="?">clear</a>) Update <code><a target="_blank" href="{{ .editing_url }}">{{ .editing.URL }}</a></code> (<a href="?">clear</a>)
{{ end }} {{ end }}
</h3> </h3>
<form method="POST" action="/v1/feeds"> <form method="POST" action="/v1/feeds?id={{ .editing.ID }}">
{{ range $k, $v := .editing }} {{ range $k, $v := .editing }}
{{ if not (in $k "Created" "Deleted" "Updated" "ID") }} {{ if not (in $k "Created" "Deleted" "Updated" "ID") }}
<div> <div>

View File

@@ -119,10 +119,9 @@ func Get(ctx context.Context, id string) (Feed, error) {
LIMIT 1 LIMIT 1
) AS "Execution.Version" ) AS "Execution.Version"
FROM entry FROM entry
JOIN "feed.versions" version_entries_id ON
version_entries_id.entries_id=entry.ID
JOIN "feed.versions" versions ON JOIN "feed.versions" versions ON
versions.created_at=entry.Updated versions.created_at=entry.Updated
WHERE versions.entries_id=entry.ID
`, id, 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 { func (feed Feed) Update(ctx context.Context, url, cron, pattern, tag *string) error {
return io.EOF return io.EOF
} }

View File

@@ -112,5 +112,31 @@ func TestFeeds(t *testing.T) {
} else if n == 0 { } else if n == 0 {
t.Errorf("for each didnt hit known get") 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) slices.Sort(links)
links = slices.Compact(links) links = slices.Compact(links)
var link string
if len(links) > 0 {
link = links[0]
}
result = append(result, Item{ result = append(result, Item{
Title: gitem.Title, Title: gitem.Title,
Link: link,
Links: links, Links: links,
Preview: preview, Preview: preview,
Body: body, Body: body,

View File

@@ -67,6 +67,7 @@ func TestFeedFetch(t *testing.T) {
expect := feeds.Item{ expect := feeds.Item{
Title: `Cheap 'Transforming' Electric Truck Announced by Jeff Bezos-Backed Startup`, 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`}, 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...`, Preview: `It's a pickup truck "that can change into whatever...`,
} }

View File

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