Simplify Subsonic API handler implementation

This commit is contained in:
Deluan
2022-11-21 12:57:56 -05:00
parent cd41d9a419
commit 19af11efbe
18 changed files with 280 additions and 564 deletions
+15 -19
View File
@@ -16,10 +16,6 @@ import (
"github.com/navidrome/navidrome/utils"
)
type SearchingController struct {
ds model.DataStore
}
type searchParams struct {
query string
artistCount int
@@ -30,11 +26,7 @@ type searchParams struct {
songOffset int
}
func NewSearchingController(ds model.DataStore) *SearchingController {
return &SearchingController{ds: ds}
}
func (c *SearchingController) getParams(r *http.Request) (*searchParams, error) {
func (api *Router) getParams(r *http.Request) (*searchParams, error) {
var err error
sp := &searchParams{}
sp.query, err = requiredParamString(r, "query")
@@ -78,15 +70,19 @@ func doSearch[T any](ctx context.Context, wg *sync.WaitGroup, s searchFunc[T], q
return res
}
func (c *SearchingController) searchAll(r *http.Request, sp *searchParams) (mediaFiles model.MediaFiles, albums model.Albums, artists model.Artists) {
func (api *Router) searchAll(r *http.Request, sp *searchParams) (mediaFiles model.MediaFiles, albums model.Albums, artists model.Artists) {
start := time.Now()
q := sanitize.Accents(strings.ToLower(strings.TrimSuffix(sp.query, "*")))
ctx := r.Context()
wg := &sync.WaitGroup{}
wg.Add(3)
go func() { mediaFiles = doSearch(ctx, wg, c.ds.MediaFile(ctx).Search, q, sp.songOffset, sp.songCount) }()
go func() { albums = doSearch(ctx, wg, c.ds.Album(ctx).Search, q, sp.albumOffset, sp.albumCount) }()
go func() { artists = doSearch(ctx, wg, c.ds.Artist(ctx).Search, q, sp.artistOffset, sp.artistCount) }()
go func() {
mediaFiles = doSearch(ctx, wg, api.ds.MediaFile(ctx).Search, q, sp.songOffset, sp.songCount)
}()
go func() { albums = doSearch(ctx, wg, api.ds.Album(ctx).Search, q, sp.albumOffset, sp.albumCount) }()
go func() {
artists = doSearch(ctx, wg, api.ds.Artist(ctx).Search, q, sp.artistOffset, sp.artistCount)
}()
wg.Wait()
if ctx.Err() == nil {
@@ -98,12 +94,12 @@ func (c *SearchingController) searchAll(r *http.Request, sp *searchParams) (medi
return mediaFiles, albums, artists
}
func (c *SearchingController) Search2(w http.ResponseWriter, r *http.Request) (*responses.Subsonic, error) {
sp, err := c.getParams(r)
func (api *Router) Search2(r *http.Request) (*responses.Subsonic, error) {
sp, err := api.getParams(r)
if err != nil {
return nil, err
}
mfs, als, as := c.searchAll(r, sp)
mfs, als, as := api.searchAll(r, sp)
response := newResponse()
searchResult2 := &responses.SearchResult2{}
@@ -126,13 +122,13 @@ func (c *SearchingController) Search2(w http.ResponseWriter, r *http.Request) (*
return response, nil
}
func (c *SearchingController) Search3(w http.ResponseWriter, r *http.Request) (*responses.Subsonic, error) {
func (api *Router) Search3(r *http.Request) (*responses.Subsonic, error) {
ctx := r.Context()
sp, err := c.getParams(r)
sp, err := api.getParams(r)
if err != nil {
return nil, err
}
mfs, als, as := c.searchAll(r, sp)
mfs, als, as := api.searchAll(r, sp)
response := newResponse()
searchResult3 := &responses.SearchResult3{}