quintodrome/engine/search.go

64 lines
1.6 KiB
Go
Raw Normal View History

package engine
import (
2020-01-08 16:45:07 -09:00
"context"
"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"
)
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)
}
type search struct {
ds model.DataStore
}
func NewSearch(ds model.DataStore) Search {
s := &search{ds}
2016-03-10 20:37:07 -09:00
return s
}
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, "*")))
resp, err := s.ds.Artist().Search(q, offset, size)
2016-03-10 20:37:07 -09:00
if err != nil {
return nil, nil
}
res := make(Entries, 0, len(resp))
for _, ar := range resp {
res = append(res, FromArtist(&ar))
2016-03-11 05:10:40 -09: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, "*")))
resp, err := s.ds.Album().Search(q, offset, size)
2016-03-11 05:10:40 -09:00
if err != nil {
return nil, nil
}
res := make(Entries, 0, len(resp))
2020-01-13 11:41:14 -09:00
for _, al := range resp {
res = append(res, FromAlbum(&al))
2016-03-10 20:37:07 -09:00
}
return res, nil
}
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, "*")))
resp, err := s.ds.MediaFile().Search(q, offset, size)
2016-03-11 05:10:40 -09:00
if err != nil {
return nil, nil
}
res := make(Entries, 0, len(resp))
for _, mf := range resp {
res = append(res, FromMediaFile(&mf))
2016-03-11 05:10:40 -09:00
}
return res, nil
2016-03-11 05:10:40 -09:00
}