84 lines
2.6 KiB
Go
84 lines
2.6 KiB
Go
package web
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
|
|
"git.noroutes.net/NoRoutes/vibetunes/internal/database"
|
|
)
|
|
|
|
func (h *Web) handleAdminPodcasts(w http.ResponseWriter, r *http.Request) {
|
|
channels, err := database.ListPodcastChannels(h.DB)
|
|
if err != nil {
|
|
http.Error(w, "internal error", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
type channelView struct {
|
|
Channel *database.PodcastChannel
|
|
Episodes []*database.PodcastEpisode
|
|
}
|
|
var views []channelView
|
|
for _, c := range channels {
|
|
episodes, _ := database.ListPodcastEpisodes(h.DB, c.ID)
|
|
views = append(views, channelView{Channel: c, Episodes: episodes})
|
|
}
|
|
h.page(w, r, "admin_podcasts", "admin_podcasts", "Podcasts", views)
|
|
}
|
|
|
|
func (h *Web) handleAdminPodcastCreate(w http.ResponseWriter, r *http.Request) {
|
|
if err := r.ParseForm(); err != nil {
|
|
http.Error(w, "bad request", http.StatusBadRequest)
|
|
return
|
|
}
|
|
feedURL := r.FormValue("url")
|
|
if feedURL == "" {
|
|
http.Redirect(w, r, "/admin/podcasts", http.StatusFound)
|
|
return
|
|
}
|
|
channel, err := database.CreatePodcastChannel(h.DB, feedURL)
|
|
if err != nil {
|
|
http.Error(w, "internal error", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
go func() { _ = h.Podcasts.RefreshChannel(context.Background(), channel.ID) }()
|
|
http.Redirect(w, r, "/admin/podcasts", http.StatusFound)
|
|
}
|
|
|
|
func (h *Web) handleAdminPodcastRefresh(w http.ResponseWriter, r *http.Request) {
|
|
id := r.PathValue("id")
|
|
if _, err := database.GetPodcastChannel(h.DB, id); err != nil {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
go func() { _ = h.Podcasts.RefreshChannel(context.Background(), id) }()
|
|
http.Redirect(w, r, "/admin/podcasts", http.StatusFound)
|
|
}
|
|
|
|
func (h *Web) handleAdminPodcastDelete(w http.ResponseWriter, r *http.Request) {
|
|
id := r.PathValue("id")
|
|
if err := database.DeletePodcastChannel(h.DB, id); err != nil {
|
|
http.Error(w, "internal error", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
http.Redirect(w, r, "/admin/podcasts", http.StatusFound)
|
|
}
|
|
|
|
func (h *Web) handleAdminPodcastEpisodeDownload(w http.ResponseWriter, r *http.Request) {
|
|
id := r.PathValue("id")
|
|
if _, err := database.GetPodcastEpisode(h.DB, id); err != nil {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
go func() { _ = h.Podcasts.DownloadEpisode(context.Background(), id) }()
|
|
http.Redirect(w, r, "/admin/podcasts", http.StatusFound)
|
|
}
|
|
|
|
func (h *Web) handleAdminPodcastEpisodeDelete(w http.ResponseWriter, r *http.Request) {
|
|
id := r.PathValue("id")
|
|
if err := database.DeletePodcastEpisode(h.DB, id); err != nil {
|
|
http.Error(w, "internal error", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
http.Redirect(w, r, "/admin/podcasts", http.StatusFound)
|
|
}
|