diff --git a/src/cmd/server/handler/feeds.go b/src/cmd/server/handler/feeds.go index 4388012..e214c4b 100644 --- a/src/cmd/server/handler/feeds.go +++ b/src/cmd/server/handler/feeds.go @@ -2,14 +2,16 @@ package handler import ( "context" + "encoding/json" "io" "net/http" + "show-rss/src/feeds" ) func (h Handler) feeds(w http.ResponseWriter, r *http.Request) error { switch r.Method { case http.MethodPost: - return h.feedsPost(ctx, r.Body) + return h.feedsPost(r.Context(), r.Body) default: http.NotFound(w, r) } @@ -17,4 +19,18 @@ func (h Handler) feeds(w http.ResponseWriter, r *http.Request) error { } func (h Handler) feedsPost(ctx context.Context, r io.Reader) error { + var req struct { + URL string + Cron string + Pattern string + WebhookMethod string + WebhookURL string + WebhookBody string + } + if err := json.NewDecoder(r).Decode(&req); err != nil { + return err + } + + _, err := feeds.Insert(ctx, req.URL, req.Cron, req.Pattern, req.WebhookMethod, req.WebhookURL, req.WebhookBody) + return err } diff --git a/src/cmd/server/handler/feeds_test.go b/src/cmd/server/handler/feeds_test.go index bb0792b..6dc90a7 100644 --- a/src/cmd/server/handler/feeds_test.go +++ b/src/cmd/server/handler/feeds_test.go @@ -25,6 +25,7 @@ func TestFeeds(t *testing.T) { "webhookURL": "wurl", "webhookBody": "wbody" }`)) + r = r.WithContext(ctx) h.ServeHTTP(w, r) if w.Code != http.StatusOK { t.Errorf("(%d) %s", w.Code, w.Body.Bytes())