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" -}} + + +
+ + + +{{ range $channel, $sources := .ChannelSources }} + ++ + | + +{{ . }} | +