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-14 18:22:34 -09:00
|
|
|
"github.com/cloudsonic/sonic-server/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-19 11:37:41 -09:00
|
|
|
resp, err := s.ds.Artist().Search(q, offset, size)
|
2016-03-10 20:37:07 -09:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
2016-03-21 07:57:04 -08:00
|
|
|
res := make(Entries, 0, len(resp))
|
2020-01-13 12:02:49 -09:00
|
|
|
for _, ar := range resp {
|
|
|
|
|
res = append(res, FromArtist(&ar))
|
2016-03-11 05:10:40 -09:00
|
|
|
}
|
2016-03-18 13:30:38 -08:00
|
|
|
return res, 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-19 11:37:41 -09:00
|
|
|
resp, err := s.ds.Album().Search(q, offset, size)
|
2016-03-11 05:10:40 -09:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
2016-03-21 07:57:04 -08:00
|
|
|
res := make(Entries, 0, len(resp))
|
2020-01-13 11:41:14 -09:00
|
|
|
for _, al := range resp {
|
2020-01-13 12:02:49 -09:00
|
|
|
res = append(res, FromAlbum(&al))
|
2016-03-10 20:37:07 -09:00
|
|
|
}
|
2016-03-18 13:30:38 -08:00
|
|
|
return res, 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-19 11:37:41 -09:00
|
|
|
resp, err := s.ds.MediaFile().Search(q, offset, size)
|
2016-03-11 05:10:40 -09:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
2016-03-21 07:57:04 -08:00
|
|
|
res := make(Entries, 0, len(resp))
|
2020-01-13 06:44:35 -09:00
|
|
|
for _, mf := range resp {
|
2020-01-13 12:02:49 -09:00
|
|
|
res = append(res, FromMediaFile(&mf))
|
2016-03-11 05:10:40 -09:00
|
|
|
}
|
2016-03-18 13:30:38 -08:00
|
|
|
return res, nil
|
2016-03-11 05:10:40 -09:00
|
|
|
}
|