Add MediaFile.FindByPath

This commit is contained in:
Deluan 2020-01-16 15:56:24 -05:00
parent ed631edc08
commit 7a16d41abe
5 changed files with 56 additions and 5 deletions

View file

@ -45,8 +45,10 @@ type MediaFileRepository interface {
Put(m *MediaFile) error
Get(id string) (*MediaFile, error)
FindByAlbum(albumId string) (MediaFiles, error)
FindByPath(path string) (MediaFiles, error)
GetStarred(options ...QueryOptions) (MediaFiles, error)
PurgeInactive(active MediaFiles) error
GetAllIds() ([]string, error)
Search(q string, offset int, size int) (MediaFiles, error)
Delete(id string) error
}

View file

@ -1,6 +1,8 @@
package persistence
import (
"os"
"strings"
"time"
"github.com/astaxie/beego/orm"
@ -9,7 +11,7 @@ import (
type MediaFile struct {
ID string `orm:"pk;column(id)"`
Path string ``
Path string `orm:"index"`
Title string `orm:"index"`
Album string ``
Artist string ``
@ -82,6 +84,24 @@ func (r *mediaFileRepository) FindByAlbum(albumId string) (model.MediaFiles, err
return r.toMediaFiles(mfs), nil
}
func (r *mediaFileRepository) FindByPath(path string) (model.MediaFiles, error) {
var mfs []MediaFile
_, err := r.newQuery(Db()).Filter("path__istartswith", path).OrderBy("disc_number", "track_number").All(&mfs)
if err != nil {
return nil, err
}
var filtered []MediaFile
path = strings.ToLower(path) + string(os.PathSeparator)
for _, mf := range mfs {
filename := strings.TrimPrefix(strings.ToLower(mf.Path), path)
if len(strings.Split(filename, string(os.PathSeparator))) > 1 {
continue
}
filtered = append(filtered, mf)
}
return r.toMediaFiles(filtered), nil
}
func (r *mediaFileRepository) GetStarred(options ...model.QueryOptions) (model.MediaFiles, error) {
var starred []MediaFile
_, err := r.newQuery(Db(), options...).Filter("starred", true).All(&starred)

View file

@ -0,0 +1,24 @@
package persistence
import (
"github.com/cloudsonic/sonic-server/model"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("MediaFileRepository", func() {
var repo model.MediaFileRepository
BeforeEach(func() {
repo = NewMediaFileRepository()
})
Describe("FindByPath", func() {
It("returns all records from a given ArtistID", func() {
Expect(repo.FindByPath("/beatles/1")).To(Equal(model.MediaFiles{
songComeTogether,
}))
})
})
})

View file

@ -34,10 +34,10 @@ var testAlbums = model.Albums{
albumRadioactivity,
}
var songDayInALife = model.MediaFile{ID: "1", Title: "A Day In A Life", ArtistID: "3", AlbumID: "1", Genre: "Rock"}
var songComeTogether = model.MediaFile{ID: "2", Title: "Come Together", ArtistID: "3", AlbumID: "2", Genre: "Rock"}
var songRadioactivity = model.MediaFile{ID: "3", Title: "Radioactivity", ArtistID: "2", AlbumID: "3", Genre: "Electronic"}
var songAntenna = model.MediaFile{ID: "4", Title: "Antenna", ArtistID: "2", AlbumID: "3", Genre: "Electronic"}
var songDayInALife = model.MediaFile{ID: "1", Title: "A Day In A Life", ArtistID: "3", AlbumID: "1", Genre: "Rock", Path: "/beatles/1/sgt/a day.mp3"}
var songComeTogether = model.MediaFile{ID: "2", Title: "Come Together", ArtistID: "3", AlbumID: "2", Genre: "Rock", Path: "/beatles/1/come together.mp3"}
var songRadioactivity = model.MediaFile{ID: "3", Title: "Radioactivity", ArtistID: "2", AlbumID: "3", Genre: "Electronic", Path: "/kraft/radio/radio.mp3"}
var songAntenna = model.MediaFile{ID: "4", Title: "Antenna", ArtistID: "2", AlbumID: "3", Genre: "Electronic", Path: "/kraft/radio/antenna.mp3"}
var testSongs = model.MediaFiles{
songDayInALife,
songComeTogether,

View file

@ -112,6 +112,11 @@ func difference(slice1 []string, slice2 []string) []string {
return diffStr
}
func (r *sqlRepository) Delete(id string) error {
_, err := r.newQuery(Db()).Filter("id", id).Delete()
return err
}
func (r *sqlRepository) DeleteAll() error {
return withTx(func(o orm.Ormer) error {
_, err := r.newQuery(Db()).Filter("id__isnull", false).Delete()