2016-03-10 23:19:13 -05:00
|
|
|
package engine
|
|
|
|
|
|
|
|
|
|
import (
|
2020-01-08 20:45:07 -05:00
|
|
|
"context"
|
2016-03-10 23:19:13 -05:00
|
|
|
"strings"
|
|
|
|
|
|
2020-01-14 22:22:34 -05:00
|
|
|
"github.com/cloudsonic/sonic-server/model"
|
2016-03-15 16:23:39 -04:00
|
|
|
"github.com/kennygrant/sanitize"
|
2016-03-10 23:19:13 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Search interface {
|
2020-01-08 20:45:07 -05: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 23:19:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type search struct {
|
2020-01-19 15:37:41 -05:00
|
|
|
ds model.DataStore
|
2016-03-10 23:19:13 -05:00
|
|
|
}
|
|
|
|
|
|
2020-01-19 15:37:41 -05:00
|
|
|
func NewSearch(ds model.DataStore) Search {
|
|
|
|
|
s := &search{ds}
|
2016-03-11 00:37:07 -05:00
|
|
|
return s
|
2016-03-10 23:19:13 -05:00
|
|
|
}
|
|
|
|
|
|
2020-01-08 20:45:07 -05:00
|
|
|
func (s *search) SearchArtist(ctx context.Context, q string, offset int, size int) (Entries, error) {
|
2016-03-15 16:23:39 -04:00
|
|
|
q = sanitize.Accents(strings.ToLower(strings.TrimSuffix(q, "*")))
|
2020-01-19 15:37:41 -05:00
|
|
|
resp, err := s.ds.Artist().Search(q, offset, size)
|
2016-03-11 00:37:07 -05:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
2016-03-21 11:57:04 -04:00
|
|
|
res := make(Entries, 0, len(resp))
|
2020-01-13 16:02:49 -05:00
|
|
|
for _, ar := range resp {
|
|
|
|
|
res = append(res, FromArtist(&ar))
|
2016-03-11 09:10:40 -05:00
|
|
|
}
|
2016-03-18 17:30:38 -04:00
|
|
|
return res, nil
|
2016-03-11 09:10:40 -05:00
|
|
|
}
|
|
|
|
|
|
2020-01-08 20:45:07 -05:00
|
|
|
func (s *search) SearchAlbum(ctx context.Context, q string, offset int, size int) (Entries, error) {
|
2016-03-15 16:23:39 -04:00
|
|
|
q = sanitize.Accents(strings.ToLower(strings.TrimSuffix(q, "*")))
|
2020-01-19 15:37:41 -05:00
|
|
|
resp, err := s.ds.Album().Search(q, offset, size)
|
2016-03-11 09:10:40 -05:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
2016-03-21 11:57:04 -04:00
|
|
|
res := make(Entries, 0, len(resp))
|
2020-01-13 15:41:14 -05:00
|
|
|
for _, al := range resp {
|
2020-01-13 16:02:49 -05:00
|
|
|
res = append(res, FromAlbum(&al))
|
2016-03-11 00:37:07 -05:00
|
|
|
}
|
2016-03-18 17:30:38 -04:00
|
|
|
return res, nil
|
2016-03-10 23:19:13 -05:00
|
|
|
}
|
2016-03-11 00:37:07 -05:00
|
|
|
|
2020-01-08 20:45:07 -05:00
|
|
|
func (s *search) SearchSong(ctx context.Context, q string, offset int, size int) (Entries, error) {
|
2016-03-15 16:23:39 -04:00
|
|
|
q = sanitize.Accents(strings.ToLower(strings.TrimSuffix(q, "*")))
|
2020-01-19 15:37:41 -05:00
|
|
|
resp, err := s.ds.MediaFile().Search(q, offset, size)
|
2016-03-11 09:10:40 -05:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
2016-03-21 11:57:04 -04:00
|
|
|
res := make(Entries, 0, len(resp))
|
2020-01-13 10:44:35 -05:00
|
|
|
for _, mf := range resp {
|
2020-01-13 16:02:49 -05:00
|
|
|
res = append(res, FromMediaFile(&mf))
|
2016-03-11 09:10:40 -05:00
|
|
|
}
|
2016-03-18 17:30:38 -04:00
|
|
|
return res, nil
|
2016-03-11 09:10:40 -05:00
|
|
|
}
|