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