Skip to content

Commit

Permalink
website: add data to faves and search pages
Browse files Browse the repository at this point in the history
  • Loading branch information
Wessie committed Feb 22, 2024
1 parent 89bb288 commit 1d8552d
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 26 deletions.
16 changes: 8 additions & 8 deletions templates/default/faves.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
<h3>Favorites</h3>
</div>
<div class="columns is-centered mb-5">
<form action="/favorites" class="column is-8 control is-flex" hx-boost="true" hx-push-url="true" hx-target="#content">
<input class="input" type="text" id="something" name="something" placeholder="Enter a username..." value="">
<form action="/faves" class="column is-8 control is-flex" hx-boost="true" hx-push-url="true" hx-target="#content">
<input class="input" type="text" name="nick" placeholder="Enter a username..." value="">
<input class="submit button is-link ml-3" type="submit" value="Submit">
</form>
</div>
{{template "search-header"}}
{{/*template "pagination" .*/}}
{{template "search-results" .}}
{{template "pagination" .Page}}
{{template "search-results" .Faves}}
</div>
</div>
</section>
Expand All @@ -22,13 +22,13 @@
{{define "search-results"}}
{{with .}}
<div class="columns has-text-centered is-vcentered has-background-white-ter song-info">
{{/*range .Songs*/}}
<div class="column">{{/*.Artist*/}} Some artist</div>
<div class="column">{{/*.Title*/}} Some song</div>
{{range .}}
<div class="column">{{.Artist}} Some artist</div>
<div class="column">{{.Title}} Some song</div>
<div class="column">
<button class="submit button is-primary">Request</button>
</div>
{{/*end*/}}
{{end}}
</div>
{{end}}
{{end}}
Expand Down
39 changes: 34 additions & 5 deletions website/public/faves.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 29 additions & 10 deletions website/public/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package public

import (
"net/http"
"time"

radio "github.com/R-a-dio/valkyrie"
"github.com/R-a-dio/valkyrie/errors"
Expand All @@ -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()

Expand All @@ -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
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions website/shared/pagination.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
Expand Down

0 comments on commit 1d8552d

Please sign in to comment.