mirror of
https://github.com/navidrome/navidrome.git
synced 2025-04-23 15:20:33 +03:00
refactor: add search back to mediafiles
This commit is contained in:
parent
4f4af34595
commit
d755609d13
@ -33,7 +33,8 @@ func (r *albumRepository) Exists(id string) (bool, error) {
|
||||
}
|
||||
|
||||
func (r *albumRepository) Put(a *model.Album) error {
|
||||
return r.put(a.ID, a)
|
||||
_, err := r.put(a.ID, a)
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *albumRepository) selectAlbum(options ...model.QueryOptions) SelectBuilder {
|
||||
|
@ -53,7 +53,8 @@ func (r *artistRepository) getIndexKey(a *model.Artist) string {
|
||||
}
|
||||
|
||||
func (r *artistRepository) Put(a *model.Artist) error {
|
||||
return r.put(a.ID, a)
|
||||
_, err := r.put(a.ID, a)
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *artistRepository) Get(id string) (*model.Artist, error) {
|
||||
|
@ -2,13 +2,11 @@ package persistence
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
|
||||
. "github.com/Masterminds/squirrel"
|
||||
"github.com/astaxie/beego/orm"
|
||||
"github.com/deluan/navidrome/model"
|
||||
"github.com/deluan/rest"
|
||||
"github.com/kennygrant/sanitize"
|
||||
)
|
||||
|
||||
type mediaFileRepository struct {
|
||||
@ -32,7 +30,12 @@ func (r mediaFileRepository) Exists(id string) (bool, error) {
|
||||
}
|
||||
|
||||
func (r mediaFileRepository) Put(m *model.MediaFile) error {
|
||||
return r.put(m.ID, m)
|
||||
_, err := r.put(m.ID, m)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r.index(m.ID, m.Title)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r mediaFileRepository) selectMediaFile(options ...model.QueryOptions) SelectBuilder {
|
||||
@ -103,26 +106,8 @@ func (r mediaFileRepository) DeleteByPath(path string) error {
|
||||
}
|
||||
|
||||
func (r mediaFileRepository) Search(q string, offset int, size int) (model.MediaFiles, error) {
|
||||
q = strings.TrimSpace(sanitize.Accents(strings.ToLower(strings.TrimSuffix(q, "*"))))
|
||||
if len(q) <= 2 {
|
||||
return model.MediaFiles{}, nil
|
||||
}
|
||||
sq := Select("*").From(r.tableName)
|
||||
sq = sq.Limit(uint64(size)).Offset(uint64(offset)).OrderBy("title")
|
||||
sq = sq.Join("search").Where("search.id = " + r.tableName + ".id")
|
||||
parts := strings.Split(q, " ")
|
||||
for _, part := range parts {
|
||||
sq = sq.Where(Or{
|
||||
Like{"full_text": part + "%"},
|
||||
Like{"full_text": "%" + part + "%"},
|
||||
})
|
||||
}
|
||||
sql, args, err := r.toSql(sq)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var results model.MediaFiles
|
||||
_, err = r.ormer.Raw(sql, args...).QueryRows(results)
|
||||
err := r.doSearch(q, offset, size, &results, "title")
|
||||
return results, err
|
||||
}
|
||||
|
||||
|
@ -7,7 +7,6 @@ import (
|
||||
. "github.com/Masterminds/squirrel"
|
||||
"github.com/astaxie/beego/orm"
|
||||
"github.com/deluan/navidrome/model"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type playlist struct {
|
||||
@ -45,12 +44,9 @@ func (r *playlistRepository) Delete(id string) error {
|
||||
}
|
||||
|
||||
func (r *playlistRepository) Put(p *model.Playlist) error {
|
||||
if p.ID == "" {
|
||||
id, _ := uuid.NewRandom()
|
||||
p.ID = id.String()
|
||||
}
|
||||
pls := r.fromModel(p)
|
||||
return r.put(p.ID, &pls)
|
||||
_, err := r.put(pls.ID, pls)
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *playlistRepository) Get(id string) (*model.Playlist, error) {
|
||||
|
56
persistence/search.go
Normal file
56
persistence/search.go
Normal file
@ -0,0 +1,56 @@
|
||||
package persistence
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
. "github.com/Masterminds/squirrel"
|
||||
"github.com/kennygrant/sanitize"
|
||||
)
|
||||
|
||||
const searchTable = "search"
|
||||
|
||||
func (r sqlRepository) index(id string, text string) error {
|
||||
sanitizedText := strings.TrimSpace(sanitize.Accents(strings.ToLower(text)))
|
||||
|
||||
values := map[string]interface{}{
|
||||
"id": id,
|
||||
"full_text": sanitizedText,
|
||||
}
|
||||
update := Update(searchTable).Where(Eq{"id": id}).SetMap(values)
|
||||
count, err := r.executeSQL(update)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if count > 0 {
|
||||
return nil
|
||||
}
|
||||
insert := Insert(searchTable).SetMap(values)
|
||||
_, err = r.executeSQL(insert)
|
||||
return err
|
||||
}
|
||||
|
||||
func (r sqlRepository) doSearch(q string, offset, size int, results interface{}, orderBys ...string) error {
|
||||
q = strings.TrimSpace(sanitize.Accents(strings.ToLower(strings.TrimSuffix(q, "*"))))
|
||||
if len(q) <= 2 {
|
||||
return nil
|
||||
}
|
||||
sq := Select("*").From(r.tableName)
|
||||
sq = sq.Limit(uint64(size)).Offset(uint64(offset))
|
||||
if len(orderBys) > 0 {
|
||||
sq = sq.OrderBy(orderBys...)
|
||||
}
|
||||
sq = sq.Join("search").Where("search.id = " + r.tableName + ".id")
|
||||
parts := strings.Split(q, " ")
|
||||
for _, part := range parts {
|
||||
sq = sq.Where(Or{
|
||||
Like{"full_text": part + "%"},
|
||||
Like{"full_text": "%" + part + "%"},
|
||||
})
|
||||
}
|
||||
sql, args, err := r.toSql(sq)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = r.ormer.Raw(sql, args...).QueryRows(results)
|
||||
return err
|
||||
}
|
@ -137,26 +137,27 @@ func (r sqlRepository) count(countQuery SelectBuilder, options ...model.QueryOpt
|
||||
return res.Count, nil
|
||||
}
|
||||
|
||||
func (r *sqlRepository) put(id string, m interface{}) error {
|
||||
func (r *sqlRepository) put(id string, m interface{}) (newId string, err error) {
|
||||
values, _ := toSqlArgs(m)
|
||||
if id != "" {
|
||||
update := Update(r.tableName).Where(Eq{"id": id}).SetMap(values)
|
||||
count, err := r.executeSQL(update)
|
||||
if err != nil {
|
||||
return err
|
||||
return "", err
|
||||
}
|
||||
if count > 0 {
|
||||
return nil
|
||||
return id, nil
|
||||
}
|
||||
}
|
||||
// if does not have an id OR could not update (new record with predefined id)
|
||||
if id == "" {
|
||||
rand, _ := uuid.NewRandom()
|
||||
values["id"] = rand.String()
|
||||
id = rand.String()
|
||||
values["id"] = id
|
||||
}
|
||||
insert := Insert(r.tableName).SetMap(values)
|
||||
_, err := r.executeSQL(insert)
|
||||
return err
|
||||
_, err = r.executeSQL(insert)
|
||||
return id, err
|
||||
}
|
||||
|
||||
func (r sqlRepository) delete(cond Sqlizer) error {
|
||||
|
Loading…
x
Reference in New Issue
Block a user