2016-03-10 19:19:13 -09:00
|
|
|
package engine
|
|
|
|
|
|
|
|
|
|
import (
|
2020-01-08 16:45:07 -09:00
|
|
|
"context"
|
2016-03-10 19:19:13 -09:00
|
|
|
"strings"
|
|
|
|
|
|
2020-01-23 15:44:08 -09:00
|
|
|
"github.com/deluan/navidrome/model"
|
2016-03-15 12:23:39 -08:00
|
|
|
"github.com/kennygrant/sanitize"
|
2016-03-10 19:19:13 -09:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Search interface {
|
2020-01-08 16:45:07 -09:00
|
|
|
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)
|
2016-03-10 19:19:13 -09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type search struct {
|
2020-01-19 11:37:41 -09:00
|
|
|
ds model.DataStore
|
2016-03-10 19:19:13 -09:00
|
|
|
}
|
|
|
|
|
|
2020-01-19 11:37:41 -09:00
|
|
|
func NewSearch(ds model.DataStore) Search {
|
|
|
|
|
s := &search{ds}
|
2016-03-10 20:37:07 -09:00
|
|
|
return s
|
2016-03-10 19:19:13 -09:00
|
|
|
}
|
|
|
|
|
|
2020-01-08 16:45:07 -09:00
|
|
|
func (s *search) SearchArtist(ctx context.Context, q string, offset int, size int) (Entries, error) {
|
2016-03-15 12:23:39 -08:00
|
|
|
q = sanitize.Accents(strings.ToLower(strings.TrimSuffix(q, "*")))
|
2020-01-27 05:41:33 -09:00
|
|
|
artists, err := s.ds.Artist(ctx).Search(q, offset, size)
|
2020-01-21 19:01:43 -09:00
|
|
|
if len(artists) == 0 || err != nil {
|
2016-03-10 20:37:07 -09:00
|
|
|
return nil, nil
|
|
|
|
|
}
|
2020-01-21 19:01:43 -09:00
|
|
|
|
|
|
|
|
artistIds := make([]string, len(artists))
|
|
|
|
|
for i, al := range artists {
|
|
|
|
|
artistIds[i] = al.ID
|
|
|
|
|
}
|
2020-01-30 18:07:02 -09:00
|
|
|
return FromArtists(artists), nil
|
2016-03-11 05:10:40 -09:00
|
|
|
}
|
|
|
|
|
|
2020-01-08 16:45:07 -09:00
|
|
|
func (s *search) SearchAlbum(ctx context.Context, q string, offset int, size int) (Entries, error) {
|
2016-03-15 12:23:39 -08:00
|
|
|
q = sanitize.Accents(strings.ToLower(strings.TrimSuffix(q, "*")))
|
2020-01-27 05:41:33 -09:00
|
|
|
albums, err := s.ds.Album(ctx).Search(q, offset, size)
|
2020-01-21 19:01:43 -09:00
|
|
|
if len(albums) == 0 || err != nil {
|
2016-03-11 05:10:40 -09:00
|
|
|
return nil, nil
|
|
|
|
|
}
|
2020-01-21 19:01:43 -09:00
|
|
|
|
|
|
|
|
albumIds := make([]string, len(albums))
|
|
|
|
|
for i, al := range albums {
|
|
|
|
|
albumIds[i] = al.ID
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-30 18:07:02 -09:00
|
|
|
return FromAlbums(albums), nil
|
2016-03-10 19:19:13 -09:00
|
|
|
}
|
2016-03-10 20:37:07 -09:00
|
|
|
|
2020-01-08 16:45:07 -09:00
|
|
|
func (s *search) SearchSong(ctx context.Context, q string, offset int, size int) (Entries, error) {
|
2016-03-15 12:23:39 -08:00
|
|
|
q = sanitize.Accents(strings.ToLower(strings.TrimSuffix(q, "*")))
|
2020-01-27 05:41:33 -09:00
|
|
|
mediaFiles, err := s.ds.MediaFile(ctx).Search(q, offset, size)
|
2020-01-21 19:01:43 -09:00
|
|
|
if len(mediaFiles) == 0 || err != nil {
|
2016-03-11 05:10:40 -09:00
|
|
|
return nil, nil
|
|
|
|
|
}
|
2020-01-21 19:01:43 -09:00
|
|
|
|
|
|
|
|
trackIds := make([]string, len(mediaFiles))
|
|
|
|
|
for i, mf := range mediaFiles {
|
|
|
|
|
trackIds[i] = mf.ID
|
2016-03-11 05:10:40 -09:00
|
|
|
}
|
2020-01-21 19:01:43 -09:00
|
|
|
|
2020-01-30 18:07:02 -09:00
|
|
|
return FromMediaFiles(mediaFiles), nil
|
2016-03-11 05:10:40 -09:00
|
|
|
}
|