From 879238e61e0033d71f910a251f5ff087489ab8aa Mon Sep 17 00:00:00 2001 From: Tim Van Baak Date: Wed, 12 Feb 2025 07:46:19 -0800 Subject: [PATCH] Web channel editing --- web/channel.go | 64 ++++++++++++++++++++++++++++++++++++++ web/html/editChannels.html | 49 +++++++++++++++++++++++++++++ web/html/home.html | 4 ++- web/html/html.go | 13 ++++++++ web/main.go | 3 ++ 5 files changed, 132 insertions(+), 1 deletion(-) create mode 100644 web/html/editChannels.html diff --git a/web/channel.go b/web/channel.go index 386c909..a67aca9 100644 --- a/web/channel.go +++ b/web/channel.go @@ -1,12 +1,76 @@ package web import ( + "fmt" "net/http" "github.com/Jaculabilis/intake/core" "github.com/Jaculabilis/intake/web/html" ) +func (env *Env) getChannels(writer http.ResponseWriter, req *http.Request) { + allSources, err := core.GetSources(env.db) + if err != nil { + http.Error(writer, err.Error(), 400) + return + } + + channelSources, err := core.GetSourcesInChannel(env.db) + if err != nil { + http.Error(writer, err.Error(), 400) + return + } + + data := html.EditChannelsData{ + Sources: allSources, + ChannelSources: channelSources, + } + html.EditChannels(writer, data) +} + +func (env *Env) editChannel(writer http.ResponseWriter, req *http.Request) { + if err := req.ParseForm(); err != nil { + http.Error(writer, err.Error(), 400) + return + } + + channel := req.Form.Get("channel") + source := req.Form.Get("source") + + if channel == "" { + http.Error(writer, "missing channel", 400) + return + } + if source == "" { + http.Error(writer, "missing source", 400) + return + } + if exists, err := core.SourceExists(env.db, source); err != nil || !exists { + http.Error(writer, fmt.Sprintf("could not find source %s: %v", source, err), 500) + return + } + + if req.Method == http.MethodPost { + if err := core.AddSourceToChannel(env.db, channel, source); err != nil { + http.Error(writer, err.Error(), 500) + return + } + writer.Header()["HX-Refresh"] = []string{"true"} + writer.WriteHeader(http.StatusNoContent) + return + } + + if req.Method == http.MethodDelete { + if err := core.DeleteSourceFromChannel(env.db, channel, source); err != nil { + http.Error(writer, err.Error(), 500) + return + } + writer.Header()["HX-Refresh"] = []string{"true"} + writer.WriteHeader(http.StatusNoContent) + return + } +} + func (env *Env) getChannel(writer http.ResponseWriter, req *http.Request) { channel := req.PathValue("channel") diff --git a/web/html/editChannels.html b/web/html/editChannels.html new file mode 100644 index 0000000..b852c92 --- /dev/null +++ b/web/html/editChannels.html @@ -0,0 +1,49 @@ +{{ define "title" }}Channels - Intake{{ end }} + +{{ define "content" -}} + + + +{{- end }} diff --git a/web/html/home.html b/web/html/home.html index a5690de..06f2c9c 100644 --- a/web/html/home.html +++ b/web/html/home.html @@ -8,7 +8,7 @@ {{ range .Channels }}

{{ if .Active }} -{{ .Name }} ({{ .Active }}) +({{ .Active }}) {{ .Name }} {{ else }} {{ .Name }} {{ end }} @@ -17,6 +17,8 @@ {{ else }}

No channels found.

{{ end }} + +

(Edit channels)

diff --git a/web/html/html.go b/web/html/html.go index ed157e8..6c0c1ff 100644 --- a/web/html/html.go +++ b/web/html/html.go @@ -169,3 +169,16 @@ func Fetch(writer io.Writer, data FetchData) { log.Printf("error: failed to render fetch: %v", err) } } + +var editChannels = load("editChannels.html") + +type EditChannelsData struct { + Sources []string + ChannelSources map[string][]string +} + +func EditChannels(writer io.Writer, data EditChannelsData) { + if err := editChannels.Execute(writer, data); err != nil { + log.Printf("error: failed to render edit channels: %v", err) + } +} diff --git a/web/main.go b/web/main.go index 4746a68..82dd731 100644 --- a/web/main.go +++ b/web/main.go @@ -41,6 +41,9 @@ func RunServer(db core.DB, addr string, port string) { handleFunc("POST /login", env.login, logged) handleFunc("GET /source/{source}", env.getSource, env.authed, logged) handleFunc("POST /source/{source}/fetch", env.fetchSource, env.authed, logged) + handleFunc("GET /channel/", env.getChannels, env.authed, logged) + handleFunc("POST /channel/", env.editChannel, env.authed, logged) + handleFunc("DELETE /channel/", env.editChannel, env.authed, logged) handleFunc("GET /channel/{channel}", env.getChannel, env.authed, logged) handleFunc("POST /item", env.addItem, env.authed, logged) handleFunc("GET /item/{source}/{id}", env.getItem, env.authed, logged)