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)
playlists := engine.NewPlaylists(dataStore)
scrobbler := engine.NewScrobbler(dataStore, nowPlayingRepository)
search := engine.NewSearch(dataStore)
transcoderTranscoder := transcoder.New()
transcodingCache := core.NewTranscodingCache()
mediaStreamer := core.NewMediaStreamer(dataStore, transcoderTranscoder, transcodingCache)
archiver := core.NewArchiver(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
}

View File

@ -27,7 +27,6 @@ type Router struct {
ListGenerator engine.ListGenerator
Playlists engine.Playlists
Scrobbler engine.Scrobbler
Search engine.Search
Users engine.Users
Streamer core.MediaStreamer
Archiver core.Archiver
@ -38,10 +37,10 @@ type Router struct {
}
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 {
playlists engine.Playlists, scrobbler engine.Scrobbler, streamer core.MediaStreamer,
archiver core.Archiver, players engine.Players, ds model.DataStore) *Router {
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}
r.mux = r.routes()
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,
NewPlaylists,
NewScrobbler,
NewSearch,
NewNowPlayingRepository,
NewUsers,
NewPlayers,

View File

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

View File

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

View File

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