Skip to content

Commit

Permalink
website: add news page data
Browse files Browse the repository at this point in the history
  • Loading branch information
Wessie committed Feb 4, 2024
1 parent 1f5b0f0 commit bc3d373
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 18 deletions.
10 changes: 9 additions & 1 deletion website/admin/pending.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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
Expand Down
20 changes: 8 additions & 12 deletions website/middleware/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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))
})
}
Expand All @@ -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
}

Expand All @@ -72,18 +68,18 @@ 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))
})
}
}

// 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
}
28 changes: 23 additions & 5 deletions website/public/news.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
1 change: 1 addition & 0 deletions website/public/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit bc3d373

Please sign in to comment.