diff --git a/cmd/wire_gen.go b/cmd/wire_gen.go index db6c073ee..870937288 100644 --- a/cmd/wire_gen.go +++ b/cmd/wire_gen.go @@ -40,7 +40,6 @@ func CreateAppRouter() *app.Router { func CreateSubsonicAPIRouter() (*subsonic.Router, error) { dataStore := persistence.New() - browser := engine.NewBrowser(dataStore) artworkCache := core.NewImageCache() artwork := core.NewArtwork(dataStore, artworkCache) nowPlayingRepository := engine.NewNowPlayingRepository() @@ -54,7 +53,7 @@ func CreateSubsonicAPIRouter() (*subsonic.Router, error) { mediaStreamer := core.NewMediaStreamer(dataStore, transcoderTranscoder, transcodingCache) archiver := core.NewArchiver(dataStore) players := engine.NewPlayers(dataStore) - router := subsonic.New(browser, artwork, listGenerator, users, playlists, scrobbler, search, mediaStreamer, archiver, players, dataStore) + router := subsonic.New(artwork, listGenerator, users, playlists, scrobbler, search, mediaStreamer, archiver, players, dataStore) return router, nil } diff --git a/server/subsonic/api.go b/server/subsonic/api.go index a9b4a7bb7..faaa39bce 100644 --- a/server/subsonic/api.go +++ b/server/subsonic/api.go @@ -23,7 +23,6 @@ const Version = "1.12.0" type Handler = func(http.ResponseWriter, *http.Request) (*responses.Subsonic, error) type Router struct { - Browser engine.Browser Artwork core.Artwork ListGenerator engine.ListGenerator Playlists engine.Playlists @@ -38,10 +37,10 @@ type Router struct { mux http.Handler } -func New(browser engine.Browser, 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, streamer core.MediaStreamer, archiver core.Archiver, players engine.Players, ds model.DataStore) *Router { - r := &Router{Browser: browser, Artwork: artwork, ListGenerator: listGenerator, Playlists: playlists, + r := &Router{Artwork: artwork, ListGenerator: listGenerator, Playlists: playlists, Scrobbler: scrobbler, Search: search, Users: users, Streamer: streamer, Archiver: archiver, Players: players, DataStore: ds} r.mux = r.routes() diff --git a/server/subsonic/browsing.go b/server/subsonic/browsing.go index bcbea5cf2..d359d34a0 100644 --- a/server/subsonic/browsing.go +++ b/server/subsonic/browsing.go @@ -3,24 +3,24 @@ package subsonic import ( "context" "net/http" + "sort" "strconv" + "strings" "time" "github.com/deluan/navidrome/conf" "github.com/deluan/navidrome/log" "github.com/deluan/navidrome/model" - "github.com/deluan/navidrome/server/subsonic/engine" "github.com/deluan/navidrome/server/subsonic/responses" "github.com/deluan/navidrome/utils" ) type BrowsingController struct { - browser engine.Browser - ds model.DataStore + ds model.DataStore } -func NewBrowsingController(browser engine.Browser, ds model.DataStore) *BrowsingController { - return &BrowsingController{browser: browser, ds: ds} +func NewBrowsingController(ds model.DataStore) *BrowsingController { + return &BrowsingController{ds: ds} } func (c *BrowsingController) GetMusicFolders(w http.ResponseWriter, r *http.Request) (*responses.Subsonic, error) { @@ -210,11 +210,20 @@ func (c *BrowsingController) GetSong(w http.ResponseWriter, r *http.Request) (*r } func (c *BrowsingController) GetGenres(w http.ResponseWriter, r *http.Request) (*responses.Subsonic, error) { - genres, err := c.browser.GetGenres(r.Context()) + ctx := r.Context() + genres, err := c.ds.Genre(ctx).GetAll() if err != nil { log.Error(r, err) return nil, NewError(responses.ErrorGeneric, "Internal Error") } + for i, g := range genres { + if strings.TrimSpace(g.Name) == "" { + genres[i].Name = "" + } + } + sort.Slice(genres, func(i, j int) bool { + return genres[i].Name < genres[j].Name + }) response := NewResponse() response.Genres = ToGenres(genres) diff --git a/server/subsonic/engine/browser.go b/server/subsonic/engine/browser.go deleted file mode 100644 index 7e27c3bd3..000000000 --- a/server/subsonic/engine/browser.go +++ /dev/null @@ -1,34 +0,0 @@ -package engine - -import ( - "context" - "sort" - "strings" - - "github.com/deluan/navidrome/model" -) - -type Browser interface { - GetGenres(ctx context.Context) (model.Genres, error) -} - -func NewBrowser(ds model.DataStore) Browser { - return &browser{ds} -} - -type browser struct { - ds model.DataStore -} - -func (b *browser) GetGenres(ctx context.Context) (model.Genres, error) { - genres, err := b.ds.Genre(ctx).GetAll() - for i, g := range genres { - if strings.TrimSpace(g.Name) == "" { - genres[i].Name = "" - } - } - sort.Slice(genres, func(i, j int) bool { - return genres[i].Name < genres[j].Name - }) - return genres, err -} diff --git a/server/subsonic/engine/browser_test.go b/server/subsonic/engine/browser_test.go deleted file mode 100644 index 0d1442e22..000000000 --- a/server/subsonic/engine/browser_test.go +++ /dev/null @@ -1,52 +0,0 @@ -package engine - -import ( - "context" - "errors" - - "github.com/deluan/navidrome/model" - "github.com/deluan/navidrome/persistence" - . "github.com/onsi/ginkgo" - . "github.com/onsi/gomega" -) - -var _ = Describe("Browser", func() { - var repo *mockGenreRepository - var b Browser - - BeforeEach(func() { - repo = &mockGenreRepository{data: model.Genres{ - {Name: "Rock", SongCount: 1000, AlbumCount: 100}, - {Name: "", SongCount: 13, AlbumCount: 13}, - {Name: "Electronic", SongCount: 4000, AlbumCount: 40}, - }} - var ds = &persistence.MockDataStore{MockedGenre: repo} - b = &browser{ds: ds} - }) - - It("returns sorted data", func() { - Expect(b.GetGenres(context.TODO())).To(Equal(model.Genres{ - {Name: "", SongCount: 13, AlbumCount: 13}, - {Name: "Electronic", SongCount: 4000, AlbumCount: 40}, - {Name: "Rock", SongCount: 1000, AlbumCount: 100}, - })) - }) - - It("bubbles up errors", func() { - repo.err = errors.New("generic error") - _, err := b.GetGenres(context.TODO()) - Expect(err).ToNot(BeNil()) - }) -}) - -type mockGenreRepository struct { - data model.Genres - err error -} - -func (r *mockGenreRepository) GetAll() (model.Genres, error) { - if r.err != nil { - return nil, r.err - } - return r.data, nil -} diff --git a/server/subsonic/engine/wire_providers.go b/server/subsonic/engine/wire_providers.go index 36fef83f8..02c3b9a5c 100644 --- a/server/subsonic/engine/wire_providers.go +++ b/server/subsonic/engine/wire_providers.go @@ -5,7 +5,6 @@ import ( ) var Set = wire.NewSet( - NewBrowser, NewListGenerator, NewPlaylists, NewScrobbler, diff --git a/server/subsonic/wire_gen.go b/server/subsonic/wire_gen.go index 88b5de514..e4eeb900a 100644 --- a/server/subsonic/wire_gen.go +++ b/server/subsonic/wire_gen.go @@ -17,9 +17,8 @@ func initSystemController(router *Router) *SystemController { } func initBrowsingController(router *Router) *BrowsingController { - browser := router.Browser dataStore := router.DataStore - browsingController := NewBrowsingController(browser, dataStore) + browsingController := NewBrowsingController(dataStore) return browsingController } @@ -85,6 +84,6 @@ var allProviders = wire.NewSet( NewUsersController, NewMediaRetrievalController, NewStreamController, - NewBookmarksController, wire.FieldsOf(new(*Router), "Browser", "Artwork", "ListGenerator", "Playlists", "Scrobbler", + NewBookmarksController, wire.FieldsOf(new(*Router), "Artwork", "ListGenerator", "Playlists", "Scrobbler", "Search", "Streamer", "Archiver", "DataStore"), ) diff --git a/server/subsonic/wire_injectors.go b/server/subsonic/wire_injectors.go index a533ef786..00eb306ad 100644 --- a/server/subsonic/wire_injectors.go +++ b/server/subsonic/wire_injectors.go @@ -17,7 +17,7 @@ var allProviders = wire.NewSet( NewMediaRetrievalController, NewStreamController, NewBookmarksController, - wire.FieldsOf(new(*Router), "Browser", "Artwork", "ListGenerator", "Playlists", "Scrobbler", + wire.FieldsOf(new(*Router), "Artwork", "ListGenerator", "Playlists", "Scrobbler", "Search", "Streamer", "Archiver", "DataStore"), )