Refactor getGenres. Remove engine.Browser

This commit is contained in:
Deluan 2020-08-13 22:07:50 -04:00 committed by Deluan Quintão
parent eba8395146
commit 22f34b3347
8 changed files with 21 additions and 102 deletions

View file

@ -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
}

View file

@ -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()

View file

@ -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 = "<Empty>"
}
}
sort.Slice(genres, func(i, j int) bool {
return genres[i].Name < genres[j].Name
})
response := NewResponse()
response.Genres = ToGenres(genres)

View file

@ -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 = "<Empty>"
}
}
sort.Slice(genres, func(i, j int) bool {
return genres[i].Name < genres[j].Name
})
return genres, err
}

View file

@ -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: "<Empty>", 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
}

View file

@ -5,7 +5,6 @@ import (
)
var Set = wire.NewSet(
NewBrowser,
NewListGenerator,
NewPlaylists,
NewScrobbler,

View file

@ -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"),
)

View file

@ -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"),
)