quintodrome/engine/search.go

66 lines
1.8 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 {
2020-01-14 18:22:34 -09:00
artistRepo model.ArtistRepository
albumRepo model.AlbumRepository
mfileRepo model.MediaFileRepository
}
2020-01-14 18:22:34 -09:00
func NewSearch(ar model.ArtistRepository, alr model.AlbumRepository, mr model.MediaFileRepository) Search {
2016-03-21 19:11:57 -08:00
s := &search{artistRepo: ar, albumRepo: alr, mfileRepo: mr}
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.artistRepo.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, "*")))
2020-01-13 11:41:14 -09:00
resp, err := s.albumRepo.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, "*")))
2020-01-13 11:41:14 -09:00
resp, err := s.mfileRepo.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
}