Removed engine.Search

This commit is contained in:
Deluan 2020-08-13 22:27:37 -04:00 committed by Deluan Quintão
parent c271aa24d1
commit eacfc41665
7 changed files with 48 additions and 106 deletions

View file

@ -47,13 +47,12 @@ func CreateSubsonicAPIRouter() (*subsonic.Router, error) {
users := engine.NewUsers(dataStore) users := engine.NewUsers(dataStore)
playlists := engine.NewPlaylists(dataStore) playlists := engine.NewPlaylists(dataStore)
scrobbler := engine.NewScrobbler(dataStore, nowPlayingRepository) scrobbler := engine.NewScrobbler(dataStore, nowPlayingRepository)
search := engine.NewSearch(dataStore)
transcoderTranscoder := transcoder.New() transcoderTranscoder := transcoder.New()
transcodingCache := core.NewTranscodingCache() transcodingCache := core.NewTranscodingCache()
mediaStreamer := core.NewMediaStreamer(dataStore, transcoderTranscoder, transcodingCache) mediaStreamer := core.NewMediaStreamer(dataStore, transcoderTranscoder, transcodingCache)
archiver := core.NewArchiver(dataStore) archiver := core.NewArchiver(dataStore)
players := engine.NewPlayers(dataStore) players := engine.NewPlayers(dataStore)
router := subsonic.New(artwork, listGenerator, users, playlists, scrobbler, search, mediaStreamer, archiver, players, dataStore) router := subsonic.New(artwork, listGenerator, users, playlists, scrobbler, mediaStreamer, archiver, players, dataStore)
return router, nil return router, nil
} }

View file

@ -27,7 +27,6 @@ type Router struct {
ListGenerator engine.ListGenerator ListGenerator engine.ListGenerator
Playlists engine.Playlists Playlists engine.Playlists
Scrobbler engine.Scrobbler Scrobbler engine.Scrobbler
Search engine.Search
Users engine.Users Users engine.Users
Streamer core.MediaStreamer Streamer core.MediaStreamer
Archiver core.Archiver Archiver core.Archiver
@ -38,10 +37,10 @@ type Router struct {
} }
func New(artwork core.Artwork, listGenerator engine.ListGenerator, users engine.Users, func New(artwork core.Artwork, listGenerator engine.ListGenerator, users engine.Users,
playlists engine.Playlists, scrobbler engine.Scrobbler, search engine.Search, playlists engine.Playlists, scrobbler engine.Scrobbler, streamer core.MediaStreamer,
streamer core.MediaStreamer, archiver core.Archiver, players engine.Players, ds model.DataStore) *Router { archiver core.Archiver, players engine.Players, ds model.DataStore) *Router {
r := &Router{Artwork: artwork, ListGenerator: listGenerator, Playlists: playlists, r := &Router{Artwork: artwork, ListGenerator: listGenerator, Playlists: playlists,
Scrobbler: scrobbler, Search: search, Users: users, Streamer: streamer, Archiver: archiver, Scrobbler: scrobbler, Users: users, Streamer: streamer, Archiver: archiver,
Players: players, DataStore: ds} Players: players, DataStore: ds}
r.mux = r.routes() r.mux = r.routes()
return r return r

View file

@ -1,68 +0,0 @@
package engine
import (
"context"
"strings"
"github.com/deluan/navidrome/model"
"github.com/kennygrant/sanitize"
)
type Search interface {
SearchArtist(ctx context.Context, q string, offset int, size int) (Entries, error)
SearchAlbum(ctx context.Context, q string, offset int, size int) (Entries, error)
SearchSong(ctx context.Context, q string, offset int, size int) (Entries, error)
}
type search struct {
ds model.DataStore
}
func NewSearch(ds model.DataStore) Search {
s := &search{ds}
return s
}
func (s *search) SearchArtist(ctx context.Context, q string, offset int, size int) (Entries, error) {
q = sanitize.Accents(strings.ToLower(strings.TrimSuffix(q, "*")))
artists, err := s.ds.Artist(ctx).Search(q, offset, size)
if len(artists) == 0 || err != nil {
return nil, nil
}
artistIds := make([]string, len(artists))
for i, al := range artists {
artistIds[i] = al.ID
}
return FromArtists(artists), nil
}
func (s *search) SearchAlbum(ctx context.Context, q string, offset int, size int) (Entries, error) {
q = sanitize.Accents(strings.ToLower(strings.TrimSuffix(q, "*")))
albums, err := s.ds.Album(ctx).Search(q, offset, size)
if len(albums) == 0 || err != nil {
return nil, nil
}
albumIds := make([]string, len(albums))
for i, al := range albums {
albumIds[i] = al.ID
}
return FromAlbums(albums), nil
}
func (s *search) SearchSong(ctx context.Context, q string, offset int, size int) (Entries, error) {
q = sanitize.Accents(strings.ToLower(strings.TrimSuffix(q, "*")))
mediaFiles, err := s.ds.MediaFile(ctx).Search(q, offset, size)
if len(mediaFiles) == 0 || err != nil {
return nil, nil
}
trackIds := make([]string, len(mediaFiles))
for i, mf := range mediaFiles {
trackIds[i] = mf.ID
}
return FromMediaFiles(mediaFiles), nil
}

View file

@ -8,7 +8,6 @@ var Set = wire.NewSet(
NewListGenerator, NewListGenerator,
NewPlaylists, NewPlaylists,
NewScrobbler, NewScrobbler,
NewSearch,
NewNowPlayingRepository, NewNowPlayingRepository,
NewUsers, NewUsers,
NewPlayers, NewPlayers,

View file

@ -3,15 +3,17 @@ package subsonic
import ( import (
"fmt" "fmt"
"net/http" "net/http"
"strings"
"github.com/deluan/navidrome/log" "github.com/deluan/navidrome/log"
"github.com/deluan/navidrome/server/subsonic/engine" "github.com/deluan/navidrome/model"
"github.com/deluan/navidrome/server/subsonic/responses" "github.com/deluan/navidrome/server/subsonic/responses"
"github.com/deluan/navidrome/utils" "github.com/deluan/navidrome/utils"
"github.com/kennygrant/sanitize"
) )
type SearchingController struct { type SearchingController struct {
search engine.Search ds model.DataStore
} }
type searchParams struct { type searchParams struct {
@ -24,8 +26,8 @@ type searchParams struct {
songOffset int songOffset int
} }
func NewSearchingController(search engine.Search) *SearchingController { func NewSearchingController(ds model.DataStore) *SearchingController {
return &SearchingController{search: search} return &SearchingController{ds: ds}
} }
func (c *SearchingController) getParams(r *http.Request) (*searchParams, error) { func (c *SearchingController) getParams(r *http.Request) (*searchParams, error) {
@ -44,22 +46,26 @@ func (c *SearchingController) getParams(r *http.Request) (*searchParams, error)
return sp, nil return sp, nil
} }
func (c *SearchingController) searchAll(r *http.Request, sp *searchParams) (engine.Entries, engine.Entries, engine.Entries) { func (c *SearchingController) searchAll(r *http.Request, sp *searchParams) (model.MediaFiles, model.Albums, model.Artists) {
as, err := c.search.SearchArtist(r.Context(), sp.query, sp.artistOffset, sp.artistCount) q := sanitize.Accents(strings.ToLower(strings.TrimSuffix(sp.query, "*")))
ctx := r.Context()
artists, err := c.ds.Artist(ctx).Search(q, sp.artistOffset, sp.artistCount)
if err != nil { if err != nil {
log.Error(r, "Error searching for Artists", err) log.Error(ctx, "Error searching for Artists", err)
} }
als, err := c.search.SearchAlbum(r.Context(), sp.query, sp.albumOffset, sp.albumCount) albums, err := c.ds.Album(ctx).Search(q, sp.albumOffset, sp.albumCount)
if err != nil { if err != nil {
log.Error(r, "Error searching for Albums", err) log.Error(ctx, "Error searching for Albums", err)
} }
mfs, err := c.search.SearchSong(r.Context(), sp.query, sp.songOffset, sp.songCount) mediaFiles, err := c.ds.MediaFile(ctx).Search(q, sp.songOffset, sp.songCount)
if err != nil { if err != nil {
log.Error(r, "Error searching for MediaFiles", err) log.Error(ctx, "Error searching for MediaFiles", err)
} }
log.Debug(r, fmt.Sprintf("Search resulted in %d songs, %d albums and %d artists", len(mfs), len(als), len(as)), "query", sp.query) log.Debug(ctx, fmt.Sprintf("Search resulted in %d songs, %d albums and %d artists",
return mfs, als, as len(mediaFiles), len(albums), len(artists)), "query", sp.query)
return mediaFiles, albums, artists
} }
func (c *SearchingController) Search2(w http.ResponseWriter, r *http.Request) (*responses.Subsonic, error) { func (c *SearchingController) Search2(w http.ResponseWriter, r *http.Request) (*responses.Subsonic, error) {
@ -71,9 +77,19 @@ func (c *SearchingController) Search2(w http.ResponseWriter, r *http.Request) (*
response := newResponse() response := newResponse()
searchResult2 := &responses.SearchResult2{} searchResult2 := &responses.SearchResult2{}
searchResult2.Artist = toArtists(as) searchResult2.Artist = make([]responses.Artist, len(as))
searchResult2.Album = toChildren(r.Context(), als) for i, artist := range as {
searchResult2.Song = toChildren(r.Context(), mfs) searchResult2.Artist[i] = responses.Artist{
Id: artist.ID,
Name: artist.Name,
AlbumCount: artist.AlbumCount,
}
if artist.Starred {
searchResult2.Artist[i].Starred = &artist.StarredAt
}
}
searchResult2.Album = childrenFromAlbums(r.Context(), als)
searchResult2.Song = childrenFromMediaFiles(r.Context(), mfs)
response.SearchResult2 = searchResult2 response.SearchResult2 = searchResult2
return response, nil return response, nil
} }
@ -88,19 +104,18 @@ func (c *SearchingController) Search3(w http.ResponseWriter, r *http.Request) (*
response := newResponse() response := newResponse()
searchResult3 := &responses.SearchResult3{} searchResult3 := &responses.SearchResult3{}
searchResult3.Artist = make([]responses.ArtistID3, len(as)) searchResult3.Artist = make([]responses.ArtistID3, len(as))
for i, e := range as { for i, artist := range as {
searchResult3.Artist[i] = responses.ArtistID3{ searchResult3.Artist[i] = responses.ArtistID3{
Id: e.Id, Id: artist.ID,
Name: e.Title, Name: artist.Name,
CoverArt: e.CoverArt, AlbumCount: artist.AlbumCount,
AlbumCount: e.AlbumCount,
} }
if !e.Starred.IsZero() { if artist.Starred {
searchResult3.Artist[i].Starred = &e.Starred searchResult3.Artist[i].Starred = &artist.StarredAt
} }
} }
searchResult3.Album = toAlbums(r.Context(), als) searchResult3.Album = childrenFromAlbums(r.Context(), als)
searchResult3.Song = toChildren(r.Context(), mfs) searchResult3.Song = childrenFromMediaFiles(r.Context(), mfs)
response.SearchResult3 = searchResult3 response.SearchResult3 = searchResult3
return response, nil return response, nil
} }

View file

@ -42,8 +42,8 @@ func initPlaylistsController(router *Router) *PlaylistsController {
} }
func initSearchingController(router *Router) *SearchingController { func initSearchingController(router *Router) *SearchingController {
search := router.Search dataStore := router.DataStore
searchingController := NewSearchingController(search) searchingController := NewSearchingController(dataStore)
return searchingController return searchingController
} }
@ -84,6 +84,5 @@ var allProviders = wire.NewSet(
NewUsersController, NewUsersController,
NewMediaRetrievalController, NewMediaRetrievalController,
NewStreamController, NewStreamController,
NewBookmarksController, wire.FieldsOf(new(*Router), "Artwork", "ListGenerator", "Playlists", "Scrobbler", NewBookmarksController, wire.FieldsOf(new(*Router), "Artwork", "ListGenerator", "Playlists", "Scrobbler", "Streamer", "Archiver", "DataStore"),
"Search", "Streamer", "Archiver", "DataStore"),
) )

View file

@ -17,8 +17,7 @@ var allProviders = wire.NewSet(
NewMediaRetrievalController, NewMediaRetrievalController,
NewStreamController, NewStreamController,
NewBookmarksController, NewBookmarksController,
wire.FieldsOf(new(*Router), "Artwork", "ListGenerator", "Playlists", "Scrobbler", wire.FieldsOf(new(*Router), "Artwork", "ListGenerator", "Playlists", "Scrobbler", "Streamer", "Archiver", "DataStore"),
"Search", "Streamer", "Archiver", "DataStore"),
) )
func initSystemController(router *Router) *SystemController { func initSystemController(router *Router) *SystemController {