Skip to content

Commit

Permalink
website: add data to lastplayed page input
Browse files Browse the repository at this point in the history
  • Loading branch information
Wessie committed Feb 3, 2024
1 parent 69e8511 commit 122dee7
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 6 deletions.
3 changes: 2 additions & 1 deletion templates/default/lastplayed.tmpl
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{{define "content"}}
Last Played {{.}}
Last Played
{{printjson .}}
{{end}}
59 changes: 54 additions & 5 deletions website/public/lastplayed.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,69 @@
package public

import (
"log"
"net/http"
"strconv"

radio "github.com/R-a-dio/valkyrie"
"github.com/R-a-dio/valkyrie/errors"
)

func (s State) GetLastPlayed(w http.ResponseWriter, r *http.Request) {
lpInput := struct {
const (
lastplayedSize = 20
)

func (s State) getLastPlayed(w http.ResponseWriter, r *http.Request) error {
input := struct {
shared
Songs []radio.Song
Page int
}{
shared: s.shared(r),
}

err := s.TemplateExecutor.ExecuteFull(theme, "lastplayed", w, lpInput)
page, offset, err := getPageOffset(r, lastplayedSize)
if err != nil {
return err
}

songs, err := s.Storage.Song(r.Context()).LastPlayed(offset, lastplayedSize)
if err != nil {
return err
}
input.Songs = songs
input.Page = page

err = s.TemplateExecutor.ExecuteFull(theme, "lastplayed", w, input)
if err != nil {
log.Println(err)
return err
}
return nil
}

func (s State) GetLastPlayed(w http.ResponseWriter, r *http.Request) {
err := s.getLastPlayed(w, r)
if err != nil {
s.errorHandler(w, r, err)
return
}
}

func getPageOffset(r *http.Request, pageSize int) (int, int, error) {
var page = 1
{
rawPage := r.Form.Get("page")
if rawPage == "" {
return page, 0, nil
}
parsedPage, err := strconv.Atoi(rawPage)
if err != nil {
return page, 0, errors.E(err, errors.InvalidForm)
}
page = parsedPage
}
var offset = (page - 1) * pageSize
if offset < 0 {
offset = 0
}
return page, offset, nil
}
7 changes: 7 additions & 0 deletions website/public/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/R-a-dio/valkyrie/templates"
"github.com/R-a-dio/valkyrie/util/daypass"
"github.com/R-a-dio/valkyrie/website/middleware"
"github.com/rs/zerolog/hlog"

"github.com/go-chi/chi/v5"
)
Expand All @@ -34,6 +35,12 @@ func (s *State) shared(r *http.Request) shared {
}
}

func (s *State) errorHandler(w http.ResponseWriter, r *http.Request, err error) {
hlog.FromRequest(r).Error().Err(err).Msg("")
// TODO: handle errors more gracefully
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
}

type shared struct {
IsUser bool
User *radio.User
Expand Down

0 comments on commit 122dee7

Please sign in to comment.