Skip to content

Commit

Permalink
website: implement search input data
Browse files Browse the repository at this point in the history
website: fixed BasicAuth middleware taking wrong StorageService
  • Loading branch information
Wessie committed Feb 17, 2024
1 parent 4ceb0a0 commit 51dc4ff
Show file tree
Hide file tree
Showing 12 changed files with 93 additions and 92 deletions.
2 changes: 1 addition & 1 deletion radio.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ type SongInfo struct {
}

type SearchService interface {
Search(ctx context.Context, query string, limit int, offset int) (*SearchResult, error)
Search(ctx context.Context, query string, limit int64, offset int64) (*SearchResult, error)
Update(context.Context, ...Song) error
Delete(context.Context, ...Song) error
}
Expand Down
2 changes: 1 addition & 1 deletion storage/mariadb/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ ORDER BY
score DESC;
`)

func (ss SearchService) Search(ctx context.Context, search_query string, limit int, offset int) (*radio.SearchResult, error) {
func (ss SearchService) Search(ctx context.Context, search_query string, limit int64, offset int64) (*radio.SearchResult, error) {
search_query, err := processQuery(search_query)
if err != nil {
return nil, err
Expand Down
54 changes: 0 additions & 54 deletions templates/default/lastplayed.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -15,60 +15,6 @@
{{printjson .}}
{{end}}

{{define "pagination"}}
<div class="columns">
<nav class="column pagination is-left is-flex is-hidden-mobile mb-0" hx-boost="true" hx-push-url="true" hx-target="#content" role="navigation">
{{with .Prev 1}}
<a class="pagination-previous" href="{{.URL}}">Previous</a>
{{else}}
<a class="pagination-previous is-disabled disable-pointer">Previous</a>
{{end}}
{{with .Next 1}}
<a class="pagination-next" href="{{.URL}}">Next</a>
{{else}}
<a class="pagination-next is-disabled disable-pointer">Next</a>
{{end}}
<ul class="pagination-list m-0 p-0">
{{with $prev2 := .Prev 2}}
{{if gt .Nr 1}}
{{with .First}}
{{template "page-link" .}}
{{if gt $prev2.Nr 2}}
<li class="m-0"><span class="pagination-ellipsis">&hellip;</span></li>
{{end}}
{{end}}
{{end}}
{{template "page-link" .}}
{{end}}
{{with .Prev 1}}{{template "page-link" .}}{{end}}
{{with .}}<li class="m-0"><a class="pagination-link is-current" href="{{.URL}}">{{.Nr}}</a></li>{{end}}
{{with .Next 1}}{{template "page-link" .}}{{end}}
{{with $next2 := .Next 2}}
{{template "page-link" .}}
{{if lt .Nr .Total}}
{{with .Last}}
{{if lt $next2.Nr (Sub .Total 1)}}
<li class="m-0"><span class="pagination-ellipsis">&hellip;</span></li>
{{end}}
{{template "page-link" .}}
{{end}}
{{end}}
{{end}}
</ul>
</nav>
<form action="/last-played" class="column control is-2 is-flex" hx-boost="true" hx-push-url="true" hx-target="#content">
<input class="input" type="text" id="page" name="page" placeholder="Enter Page Number">
<input class="is-hidden" type="submit">
</form>
</div>
{{end}}

{{define "page-link"}}
<li class="m-0">
<a class="pagination-link" href="{{.URL}}">{{.Nr}}</a>
</li>
{{end}}

{{define "song"}}
<div class="block has-background-white-ter song-info disable-pointer">
<div class="columns is-vcentered has-text-centered">
Expand Down
14 changes: 7 additions & 7 deletions templates/default/search.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,29 @@
</div>
<div class="columns is-centered mb-5">
<form action="/search" 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="Search" value="">
<input class="input" type="text" name="q" placeholder="Search" value="{{.Query}}">
<input class="submit button is-link ml-3" type="submit" value="Submit">
</form>
</div>
{{template "search-header"}}
{{/*template "pagination" .*/}}
{{template "search-results" .}}
{{template "search-results" .Songs}}
{{template "pagination" .Page}}
</div>
</div>
</section>
{{end}}

{{define "search-results"}}
{{with .}}
{{range .}}
<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>
<div class="column">{{.Artist}}</div>
<div class="column">{{.Title}}</div>
<div class="column">
<button class="submit button is-primary">Request</button>
</div>
{{/*end*/}}
</div>
{{end}}
{{end}}
{{end}}

Expand Down
2 changes: 1 addition & 1 deletion website/api/php/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ func (a *API) getSearch(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
// key from the url router, query is part of the url
query := chi.URLParamFromCtx(ctx, "query")
result, err := a.search.Search(ctx, query, limit, offset)
result, err := a.search.Search(ctx, query, int64(limit), int64(offset))
if err != nil {
hlog.FromRequest(r).Error().Err(err).Msg("")
return
Expand Down
7 changes: 7 additions & 0 deletions website/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/R-a-dio/valkyrie/config"
"github.com/R-a-dio/valkyrie/errors"
"github.com/R-a-dio/valkyrie/search"
"github.com/R-a-dio/valkyrie/storage"
"github.com/R-a-dio/valkyrie/templates"
"github.com/R-a-dio/valkyrie/util/daypass"
Expand Down Expand Up @@ -63,6 +64,11 @@ func Execute(ctx context.Context, cfg config.Config) error {
executor := siteTemplates.Executor()
// daypass generation
dpass := daypass.New(ctx)
// search service
searchService, err := search.Open(ctx, cfg)
if err != nil {
return errors.E(op, err)
}

r := NewRouter()

Expand Down Expand Up @@ -145,6 +151,7 @@ func Execute(ctx context.Context, cfg config.Config) error {
Manager: manager,
Streamer: streamer,
Storage: storage,
Search: searchService,
}))

// setup the http server
Expand Down
3 changes: 2 additions & 1 deletion website/middleware/authentication.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ func RequestWithUser(r *http.Request, u *radio.User) *http.Request {
//
// This should ONLY be used for situations where a human cannot input the
// login info somehow. Namely for icecast source clients.
func BasicAuth(us radio.UserStorage) func(http.Handler) http.Handler {
func BasicAuth(uss radio.UserStorageService) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
username, passwd, ok := r.BasicAuth()
Expand All @@ -406,6 +406,7 @@ func BasicAuth(us radio.UserStorage) func(http.Handler) http.Handler {
}
}

us := uss.User(r.Context())
user, err := us.Get(username)
if err != nil {
hlog.FromRequest(r).Error().Err(err).Str("username", username).Msg("database error")
Expand Down
22 changes: 13 additions & 9 deletions website/middleware/authentication_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,19 @@ func TestBasicAuth(t *testing.T) {
for _, test := range basicAuthCases {
test := test
t.Run(test.Name, func(t *testing.T) {
us := &mocks.UserStorageMock{
GetFunc: func(name string) (*radio.User, error) {
if test.GetFuncRet == nil {
return nil, test.GetFuncErr
storage := &mocks.StorageServiceMock{
UserFunc: func(contextMoqParam context.Context) radio.UserStorage {
return &mocks.UserStorageMock{
GetFunc: func(name string) (*radio.User, error) {
if test.GetFuncRet == nil {
return nil, test.GetFuncErr
}
if test.GetFuncRet.Username != name {
return nil, errors.E(errors.UserUnknown)
}
return test.GetFuncRet, test.GetFuncErr
},
}
if test.GetFuncRet.Username != name {
return nil, errors.E(errors.UserUnknown)
}
return test.GetFuncRet, test.GetFuncErr
},
}

Expand All @@ -141,7 +145,7 @@ func TestBasicAuth(t *testing.T) {
w := httptest.NewRecorder()

r := chi.NewRouter()
r.Use(BasicAuth(us))
r.Use(BasicAuth(storage))
r.Get("/*", func(w http.ResponseWriter, r *http.Request) {
user := UserFromContext(r.Context())
assert.Equal(t, test.GetFuncRet, user)
Expand Down
2 changes: 1 addition & 1 deletion website/public/lastplayed.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func NewLastPlayedInput(s radio.SongStorageService, r *http.Request) (*LastPlaye
Songs: songs,
Page: shared.NewPagination(
page, shared.PageCount(total, lastplayedSize),
"/last-played?page=%d",
r.URL,
),
}, nil
}
Expand Down
43 changes: 38 additions & 5 deletions website/public/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,60 @@ package public
import (
"net/http"

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

const searchPageSize = 20

type SearchInput struct {
middleware.Input

Query string
Songs []radio.Song
Page *shared.Pagination
}

func NewSearchInput(r *http.Request) SearchInput {
return SearchInput{
Input: middleware.InputFromRequest(r),
func NewSearchInput(s radio.SearchService, r *http.Request) (*SearchInput, error) {
const op errors.Op = "website/public.NewSearchInput"
ctx := r.Context()

page, offset, err := getPageOffset(r, searchPageSize)
if err != nil {
return nil, errors.E(op, err)
}

query := r.FormValue("q")
result, err := s.Search(ctx, query, searchPageSize, offset)
if err != nil {
return nil, err
}

return &SearchInput{
Input: middleware.InputFromRequest(r),
Query: query,
Songs: result.Songs,
Page: shared.NewPagination(
page, shared.PageCount(int64(result.TotalHits), searchPageSize),
r.URL,
),
}, nil
}

func (SearchInput) TemplateBundle() string {
return "search"
}

func (s State) GetSearch(w http.ResponseWriter, r *http.Request) {
input := NewSearchInput(r)
input, err := NewSearchInput(s.Search, 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
1 change: 1 addition & 0 deletions website/public/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type State struct {
Manager radio.ManagerService
Streamer radio.StreamerService
Storage radio.StorageService
Search radio.SearchService
}

func (s *State) errorHandler(w http.ResponseWriter, r *http.Request, err error) {
Expand Down
33 changes: 21 additions & 12 deletions website/shared/pagination.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package shared

import (
"fmt"
"html/template"
"net/url"
"strconv"
)

func PageCount(total, size int64) int64 {
Expand All @@ -13,22 +14,30 @@ func PageCount(total, size int64) int64 {
return full
}

func NewPagination(current, total int64, urlFormat string) *Pagination {
func NewPagination(current, total int64, uri *url.URL) *Pagination {
return &Pagination{
Nr: current,
Total: total,
format: urlFormat,
Nr: current,
Total: total,
uri: uri,
}
}

type Pagination struct {
Nr int64
Total int64
format string
Nr int64
Total int64
uri *url.URL
}

func (p *Pagination) URL() template.URL {
return template.URL(fmt.Sprintf(p.format, p.Nr))
u := *p.uri
v := u.Query()
v.Set("page", strconv.FormatInt(p.Nr, 10))
u.RawQuery = v.Encode()
return template.URL(u.RequestURI())
}

func (p *Pagination) BaseURL() template.URL {
return template.URL(p.uri.Path)
}

func (p *Pagination) createPage(page int64) *Pagination {
Expand All @@ -43,9 +52,9 @@ func (p *Pagination) createPage(page int64) *Pagination {
}

return &Pagination{
Nr: page,
Total: p.Total,
format: p.format,
Nr: page,
Total: p.Total,
uri: p.uri,
}
}

Expand Down

0 comments on commit 51dc4ff

Please sign in to comment.