diff --git a/website/admin/pending.go b/website/admin/pending.go index 35b382ba..009f1cf0 100644 --- a/website/admin/pending.go +++ b/website/admin/pending.go @@ -35,6 +35,14 @@ type PendingForm struct { Errors map[string]string } +func (PendingForm) TemplateBundle() string { + return "admin-pending" +} + +func (PendingForm) TemplateName() string { + return "form_admin_pending" +} + func (pi *PendingInput) Prepare(s radio.SubmissionStorage) error { const op errors.Op = "website/admin.pendingInput.Prepare" @@ -87,7 +95,7 @@ func (s *State) PostPending(w http.ResponseWriter, r *http.Request) { // failed, handle the input and see if we can get info back to the user if public.IsHTMX(r) { // htmx, send just the form back - if err := s.TemplateExecutor.ExecuteTemplate("default", "admin-pending", "form_admin_pending", w, form); err != nil { + if err := s.TemplateExecutor.Execute(w, r, form); err != nil { hlog.FromRequest(r).Error().Err(err).Msg("template failure") } return diff --git a/website/middleware/context.go b/website/middleware/context.go index 973ff253..cf4e0232 100644 --- a/website/middleware/context.go +++ b/website/middleware/context.go @@ -9,13 +9,9 @@ import ( "github.com/go-chi/chi/v5" ) -type ctxKey int +type trackKey struct{} -const ( - trackKey ctxKey = iota - userKey - themeKey -) +type userKey struct{} // TrackCtx reads an URL parameter named TrackID and tries to find the track associated // with it. @@ -42,7 +38,7 @@ func TrackCtx(storage radio.TrackStorageService) func(http.Handler) http.Handler return } - ctx = context.WithValue(ctx, trackKey, *song) + ctx = context.WithValue(ctx, trackKey{}, *song) next.ServeHTTP(w, r.WithContext(ctx)) }) } @@ -51,7 +47,7 @@ func TrackCtx(storage radio.TrackStorageService) func(http.Handler) http.Handler // GetTrack returns the track from the given context if one exists. // See TrackCtx for supplier of this func GetTrack(ctx context.Context) (radio.Song, bool) { - song, ok := ctx.Value(trackKey).(radio.Song) + song, ok := ctx.Value(trackKey{}).(radio.Song) return song, ok } @@ -72,11 +68,11 @@ func UserByDJIDCtx(storage radio.UserStorageService) func(http.Handler) http.Han user, err := storage.User(ctx).GetByDJID(id) if err != nil { - // nothing really - panic("UserBYDJIDCtx: fuck do I know") + http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) + return } - ctx = context.WithValue(ctx, userKey, *user) + ctx = context.WithValue(ctx, userKey{}, *user) next.ServeHTTP(w, r.WithContext(ctx)) }) } @@ -84,6 +80,6 @@ func UserByDJIDCtx(storage radio.UserStorageService) func(http.Handler) http.Han // GetUser returns the user from the given context if one exists. func GetUser(ctx context.Context) (radio.User, bool) { - user, ok := ctx.Value(userKey).(radio.User) + user, ok := ctx.Value(userKey{}).(radio.User) return user, ok } diff --git a/website/public/news.go b/website/public/news.go index 1caf991f..2f1731ca 100644 --- a/website/public/news.go +++ b/website/public/news.go @@ -2,32 +2,50 @@ package public import ( "net/http" + + radio "github.com/R-a-dio/valkyrie" ) type NewsInput struct { SharedInput + + News radio.NewsList } func (NewsInput) TemplateBundle() string { return "news" } -func NewNewsInput(r *http.Request) NewsInput { - return NewsInput{ - SharedInput: NewSharedInput(r), +func NewNewsInput(s radio.NewsStorageService, r *http.Request) (*NewsInput, error) { + entries, err := s.News(r.Context()).ListPublic(20, 0) + if err != nil { + return nil, err } + + return &NewsInput{ + SharedInput: NewSharedInput(r), + News: entries, + }, nil } func (s State) GetNews(w http.ResponseWriter, r *http.Request) { - input := NewNewsInput(r) + input, err := NewNewsInput(s.Storage, r) + if err != nil { + s.errorHandler(w, r, err) + return + } - err := s.TemplateExecutor.Execute(w, r, input) + err = s.TemplateExecutor.Execute(w, r, input) if err != nil { s.errorHandler(w, r, err) return } } +func (s State) GetNewsEntry(w http.ResponseWriter, r *http.Request) { + return +} + func (s State) PostNews(w http.ResponseWriter, r *http.Request) { return } diff --git a/website/public/state.go b/website/public/state.go index d5a3e4d0..7c8c8613 100644 --- a/website/public/state.go +++ b/website/public/state.go @@ -54,6 +54,7 @@ func Route(ctx context.Context, s State) func(chi.Router) { return func(r chi.Router) { r.Get("/", s.GetHome) r.Get("/news", s.GetNews) + r.Get("/news/{NewsID:[0-9]+}`", s.GetNewsEntry) r.Post("/news", s.PostNews) r.Get("/schedule", s.GetSchedule) r.Get("/queue", s.GetQueue)