53 lines
1.2 KiB
Go
53 lines
1.2 KiB
Go
package handler
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
"net/url"
|
|
"show-rss/src/feeds"
|
|
)
|
|
|
|
func (h Handler) feeds(w http.ResponseWriter, r *http.Request) error {
|
|
switch r.Method {
|
|
case http.MethodPost, http.MethodPut:
|
|
if err := r.ParseForm(); err != nil {
|
|
return err
|
|
}
|
|
if err := h.feedsUpsert(r.Context(), r.URL.Query().Get("id"), r.Form); err != nil {
|
|
return err
|
|
}
|
|
default:
|
|
http.NotFound(w, r)
|
|
return nil
|
|
}
|
|
|
|
u2 := *r.URL
|
|
u2.RawQuery = ""
|
|
u2.Path = "/experimental/ui"
|
|
http.Redirect(w, r, u2.String(), http.StatusSeeOther)
|
|
return nil
|
|
}
|
|
|
|
func (h Handler) feedsUpsert(ctx context.Context, id string, form url.Values) error {
|
|
var req feeds.Version
|
|
for k, v := range map[string]*string{
|
|
"Cron": &req.Cron,
|
|
"Pattern": &req.Pattern,
|
|
"URL": &req.URL,
|
|
"WebhookBody": &req.WebhookBody,
|
|
"WebhookMethod": &req.WebhookMethod,
|
|
"WebhookURL": &req.WebhookURL,
|
|
} {
|
|
if *v = form.Get(k); *v == "" {
|
|
return fmt.Errorf("no ?%s in %s", k, form.Encode())
|
|
}
|
|
}
|
|
|
|
if id == "" {
|
|
_, err := feeds.Insert(ctx, req.URL, req.Cron, req.Pattern, req.WebhookMethod, req.WebhookURL, req.WebhookBody)
|
|
return err
|
|
}
|
|
return feeds.Update(ctx, id, req.URL, req.Cron, req.Pattern, req.WebhookMethod, req.WebhookURL, req.WebhookBody)
|
|
}
|