mirror of
https://github.com/navidrome/navidrome.git
synced 2025-04-24 15:40:56 +03:00
Implement Album search with SQL
This commit is contained in:
parent
c2448d3880
commit
3a9559a860
@ -35,4 +35,5 @@ type AlbumRepository interface {
|
||||
PurgeInactive(active Albums) ([]string, error)
|
||||
GetAllIds() ([]string, error)
|
||||
GetStarred(...QueryOptions) (Albums, error)
|
||||
Search(q string, offset int, size int) (Albums, error)
|
||||
}
|
||||
|
@ -108,20 +108,17 @@ func (s *search) SearchArtist(ctx context.Context, q string, offset int, size in
|
||||
|
||||
func (s *search) SearchAlbum(ctx context.Context, q string, offset int, size int) (Entries, error) {
|
||||
q = sanitize.Accents(strings.ToLower(strings.TrimSuffix(q, "*")))
|
||||
min := offset
|
||||
max := min + size - 1
|
||||
resp, err := s.sAlbum.Search(q, min, max)
|
||||
resp, err := s.albumRepo.Search(q, offset, size)
|
||||
if err != nil {
|
||||
return nil, nil
|
||||
}
|
||||
res := make(Entries, 0, len(resp))
|
||||
for _, id := range resp {
|
||||
al, err := s.albumRepo.Get(id)
|
||||
if criticalError(ctx, "Album", id, err) {
|
||||
for _, al := range resp {
|
||||
if criticalError(ctx, "Album", al.ID, err) {
|
||||
return nil, err
|
||||
}
|
||||
if err == nil {
|
||||
res = append(res, FromAlbum(al))
|
||||
res = append(res, FromAlbum(&al))
|
||||
}
|
||||
}
|
||||
return res, nil
|
||||
@ -129,9 +126,7 @@ func (s *search) SearchAlbum(ctx context.Context, q string, offset int, size int
|
||||
|
||||
func (s *search) SearchSong(ctx context.Context, q string, offset int, size int) (Entries, error) {
|
||||
q = sanitize.Accents(strings.ToLower(strings.TrimSuffix(q, "*")))
|
||||
min := offset
|
||||
max := min + size
|
||||
resp, err := s.mfileRepo.Search(q, min, max)
|
||||
resp, err := s.mfileRepo.Search(q, offset, size)
|
||||
if err != nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
@ -71,4 +71,8 @@ func (r *albumRepository) GetStarred(options ...domain.QueryOptions) (domain.Alb
|
||||
return as, err
|
||||
}
|
||||
|
||||
func (r *albumRepository) Search(q string, offset int, size int) (domain.Albums, error) {
|
||||
return nil, errors.New("not implemented")
|
||||
}
|
||||
|
||||
var _ domain.AlbumRepository = (*albumRepository)(nil)
|
||||
|
@ -42,7 +42,11 @@ func NewAlbumRepository() domain.AlbumRepository {
|
||||
func (r *albumRepository) Put(a *domain.Album) error {
|
||||
ta := Album(*a)
|
||||
return WithTx(func(o orm.Ormer) error {
|
||||
return r.put(o, a.ID, &ta)
|
||||
err := r.put(o, a.ID, &ta)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return r.searcher.Index(o, r.tableName, a.ID, a.Name)
|
||||
})
|
||||
}
|
||||
|
||||
@ -65,7 +69,7 @@ func (r *albumRepository) FindByArtist(artistId string) (domain.Albums, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return r.toAlbums(albums)
|
||||
return r.toAlbums(albums), nil
|
||||
}
|
||||
|
||||
func (r *albumRepository) GetAll(options ...domain.QueryOptions) (domain.Albums, error) {
|
||||
@ -74,15 +78,15 @@ func (r *albumRepository) GetAll(options ...domain.QueryOptions) (domain.Albums,
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return r.toAlbums(all)
|
||||
return r.toAlbums(all), nil
|
||||
}
|
||||
|
||||
func (r *albumRepository) toAlbums(all []Album) (domain.Albums, error) {
|
||||
func (r *albumRepository) toAlbums(all []Album) domain.Albums {
|
||||
result := make(domain.Albums, len(all))
|
||||
for i, a := range all {
|
||||
result[i] = domain.Album(a)
|
||||
}
|
||||
return result, nil
|
||||
return result
|
||||
}
|
||||
|
||||
func (r *albumRepository) PurgeInactive(activeList domain.Albums) ([]string, error) {
|
||||
@ -97,7 +101,20 @@ func (r *albumRepository) GetStarred(options ...domain.QueryOptions) (domain.Alb
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return r.toAlbums(starred)
|
||||
return r.toAlbums(starred), nil
|
||||
}
|
||||
|
||||
func (r *albumRepository) Search(q string, offset int, size int) (domain.Albums, error) {
|
||||
if len(q) <= 2 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
var results []Album
|
||||
err := r.searcher.Search(r.tableName, q, offset, size, &results, "rating desc", "starred desc", "play_count desc", "name")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return r.toAlbums(results), nil
|
||||
}
|
||||
|
||||
var _ domain.AlbumRepository = (*albumRepository)(nil)
|
||||
|
Loading…
x
Reference in New Issue
Block a user