diff --git a/templates/default/faves.tmpl b/templates/default/faves.tmpl
index 684777ce..7b22a63f 100644
--- a/templates/default/faves.tmpl
+++ b/templates/default/faves.tmpl
@@ -6,14 +6,14 @@
- {{/*range .Songs*/}}
-
{{/*.Artist*/}} Some artist
-
{{/*.Title*/}} Some song
+ {{range .}}
+
{{.Artist}} Some artist
+
{{.Title}} Some song
- {{/*end*/}}
+ {{end}}
{{end}}
{{end}}
diff --git a/website/public/faves.go b/website/public/faves.go
index 57166070..1abbd082 100644
--- a/website/public/faves.go
+++ b/website/public/faves.go
@@ -3,27 +3,56 @@ package public
import (
"net/http"
+ radio "github.com/R-a-dio/valkyrie"
"github.com/R-a-dio/valkyrie/website/middleware"
+ "github.com/R-a-dio/valkyrie/website/shared"
)
+const favesPageSize = 100
+
type FavesInput struct {
middleware.Input
+ Nickname string
+ Faves []radio.Song
+ Page *shared.Pagination
}
func (FavesInput) TemplateBundle() string {
return "faves"
}
-func NewFavesInput(r *http.Request) FavesInput {
- return FavesInput{
- Input: middleware.InputFromRequest(r),
+func NewFavesInput(ss radio.SongStorage, r *http.Request) (*FavesInput, error) {
+ page, offset, err := getPageOffset(r, favesPageSize)
+ if err != nil {
+ return nil, err
+ }
+ _ = offset
+
+ nickname := r.FormValue("nick")
+ faves, err := ss.FavoritesOf(nickname)
+ if err != nil {
+ return nil, err
}
+
+ return &FavesInput{
+ Nickname: nickname,
+ Faves: faves,
+ Page: shared.NewPagination(
+ page, shared.PageCount(int64(len(faves)), favesPageSize),
+ r.URL,
+ ),
+ Input: middleware.InputFromRequest(r),
+ }, nil
}
func (s State) GetFaves(w http.ResponseWriter, r *http.Request) {
- input := NewFavesInput(r)
+ input, err := NewFavesInput(s.Storage.Song(r.Context()), r)
+ if err != nil {
+ s.errorHandler(w, r, err)
+ return
+ }
- err := s.Templates.Execute(w, r, input)
+ err = s.Templates.Execute(w, r, input)
if err != nil {
s.errorHandler(w, r, err)
return
diff --git a/website/public/search.go b/website/public/search.go
index 9894a709..62ebbd40 100644
--- a/website/public/search.go
+++ b/website/public/search.go
@@ -2,6 +2,7 @@ package public
import (
"net/http"
+ "time"
radio "github.com/R-a-dio/valkyrie"
"github.com/R-a-dio/valkyrie/errors"
@@ -14,12 +15,14 @@ const searchPageSize = 20
type SearchInput struct {
middleware.Input
- Query string
- Songs []radio.Song
- Page *shared.Pagination
+ Query string
+ Songs []radio.Song
+ CanRequest bool
+ RequestCooldown time.Duration
+ Page *shared.Pagination
}
-func NewSearchInput(s radio.SearchService, r *http.Request) (*SearchInput, error) {
+func NewSearchInput(s radio.SearchService, rs radio.RequestStorage, r *http.Request, requestDelay time.Duration) (*SearchInput, error) {
const op errors.Op = "website/public.NewSearchInput"
ctx := r.Context()
@@ -29,17 +32,28 @@ func NewSearchInput(s radio.SearchService, r *http.Request) (*SearchInput, error
}
query := r.FormValue("q")
- result, err := s.Search(ctx, query, searchPageSize, offset)
+ searchResult, err := s.Search(ctx, query, searchPageSize, offset)
if err != nil {
return nil, err
}
+ // TODO(wessie): check if this is the right identifier
+ identifier := r.RemoteAddr
+ lastRequest, err := rs.LastRequest(identifier)
+ if err != nil {
+ return nil, err
+ }
+
+ cd, ok := radio.CalculateCooldown(requestDelay, lastRequest)
+
return &SearchInput{
- Input: middleware.InputFromRequest(r),
- Query: query,
- Songs: result.Songs,
+ Input: middleware.InputFromRequest(r),
+ Query: query,
+ Songs: searchResult.Songs,
+ CanRequest: ok,
+ RequestCooldown: cd,
Page: shared.NewPagination(
- page, shared.PageCount(int64(result.TotalHits), searchPageSize),
+ page, shared.PageCount(int64(searchResult.TotalHits), searchPageSize),
r.URL,
),
}, nil
@@ -50,7 +64,12 @@ func (SearchInput) TemplateBundle() string {
}
func (s State) GetSearch(w http.ResponseWriter, r *http.Request) {
- input, err := NewSearchInput(s.Search, r)
+ input, err := NewSearchInput(
+ s.Search,
+ s.Storage.Request(r.Context()),
+ r,
+ time.Duration(s.Conf().UserRequestDelay),
+ )
if err != nil {
s.errorHandler(w, r, err)
return
diff --git a/website/shared/pagination.go b/website/shared/pagination.go
index 049b5b99..0c4f3323 100644
--- a/website/shared/pagination.go
+++ b/website/shared/pagination.go
@@ -14,10 +14,10 @@ func PageCount(total, size int64) int64 {
return full
}
-func NewPagination(current, total int64, uri *url.URL) *Pagination {
+func NewPagination(currentPage, totalPages int64, uri *url.URL) *Pagination {
return &Pagination{
- Nr: current,
- Total: total,
+ Nr: currentPage,
+ Total: totalPages,
uri: uri,
}
}