Add multiple genres to MediaFile
This commit is contained in:
@@ -37,28 +37,34 @@ func NewMediaFileRepository(ctx context.Context, o orm.Ormer) *mediaFileReposito
|
||||
return r
|
||||
}
|
||||
|
||||
func (r mediaFileRepository) CountAll(options ...model.QueryOptions) (int64, error) {
|
||||
func (r *mediaFileRepository) CountAll(options ...model.QueryOptions) (int64, error) {
|
||||
return r.count(r.newSelectWithAnnotation("media_file.id"), options...)
|
||||
}
|
||||
|
||||
func (r mediaFileRepository) Exists(id string) (bool, error) {
|
||||
func (r *mediaFileRepository) Exists(id string) (bool, error) {
|
||||
return r.exists(Select().Where(Eq{"id": id}))
|
||||
}
|
||||
|
||||
func (r mediaFileRepository) Put(m *model.MediaFile) error {
|
||||
func (r *mediaFileRepository) Put(m *model.MediaFile) error {
|
||||
m.FullText = getFullText(m.Title, m.Album, m.Artist, m.AlbumArtist,
|
||||
m.SortTitle, m.SortAlbumName, m.SortArtistName, m.SortAlbumArtistName, m.DiscSubtitle)
|
||||
genres := m.Genres
|
||||
m.Genres = nil
|
||||
defer func() { m.Genres = genres }()
|
||||
_, err := r.put(m.ID, m)
|
||||
return err
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return r.updateGenres(m.ID, r.tableName, genres)
|
||||
}
|
||||
|
||||
func (r mediaFileRepository) selectMediaFile(options ...model.QueryOptions) SelectBuilder {
|
||||
func (r *mediaFileRepository) selectMediaFile(options ...model.QueryOptions) SelectBuilder {
|
||||
sql := r.newSelectWithAnnotation("media_file.id", options...).Columns("media_file.*")
|
||||
return r.withBookmark(sql, "media_file.id")
|
||||
}
|
||||
|
||||
func (r mediaFileRepository) Get(id string) (*model.MediaFile, error) {
|
||||
sel := r.selectMediaFile().Where(Eq{"id": id})
|
||||
func (r *mediaFileRepository) Get(id string) (*model.MediaFile, error) {
|
||||
sel := r.selectMediaFile().Where(Eq{"media_file.id": id})
|
||||
var res model.MediaFiles
|
||||
if err := r.queryAll(sel, &res); err != nil {
|
||||
return nil, err
|
||||
@@ -66,24 +72,33 @@ func (r mediaFileRepository) Get(id string) (*model.MediaFile, error) {
|
||||
if len(res) == 0 {
|
||||
return nil, model.ErrNotFound
|
||||
}
|
||||
return &res[0], nil
|
||||
err := r.loadMediaFileGenres(&res)
|
||||
return &res[0], err
|
||||
}
|
||||
|
||||
func (r mediaFileRepository) GetAll(options ...model.QueryOptions) (model.MediaFiles, error) {
|
||||
sq := r.selectMediaFile(options...)
|
||||
func (r *mediaFileRepository) GetAll(options ...model.QueryOptions) (model.MediaFiles, error) {
|
||||
sq := r.selectMediaFile(options...).
|
||||
LeftJoin("media_file_genres mfg on media_file.id = mfg.media_file_id").
|
||||
LeftJoin("genre on mfg.genre_id = genre.id").
|
||||
GroupBy("media_file.id")
|
||||
res := model.MediaFiles{}
|
||||
err := r.queryAll(sq, &res)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = r.loadMediaFileGenres(&res)
|
||||
return res, err
|
||||
}
|
||||
|
||||
func (r mediaFileRepository) FindByAlbum(albumId string) (model.MediaFiles, error) {
|
||||
sel := r.selectMediaFile(model.QueryOptions{Sort: "album"}).Where(Eq{"album_id": albumId})
|
||||
res := model.MediaFiles{}
|
||||
err := r.queryAll(sel, &res)
|
||||
return res, err
|
||||
func (r *mediaFileRepository) FindByAlbum(albumId string) (model.MediaFiles, error) {
|
||||
options := model.QueryOptions{
|
||||
Filters: Eq{"album_id": albumId},
|
||||
Sort: "album",
|
||||
}
|
||||
return r.GetAll(options)
|
||||
}
|
||||
|
||||
func (r mediaFileRepository) FindByPath(path string) (*model.MediaFile, error) {
|
||||
func (r *mediaFileRepository) FindByPath(path string) (*model.MediaFile, error) {
|
||||
sel := r.selectMediaFile().Where(Eq{"path": path})
|
||||
var res model.MediaFiles
|
||||
if err := r.queryAll(sel, &res); err != nil {
|
||||
@@ -109,7 +124,7 @@ func pathStartsWith(path string) Eq {
|
||||
}
|
||||
|
||||
// FindAllByPath only return mediafiles that are direct children of requested path
|
||||
func (r mediaFileRepository) FindAllByPath(path string) (model.MediaFiles, error) {
|
||||
func (r *mediaFileRepository) FindAllByPath(path string) (model.MediaFiles, error) {
|
||||
// Query by path based on https://stackoverflow.com/a/13911906/653632
|
||||
path = cleanPath(path)
|
||||
pathLen := utf8.RuneCountInString(path)
|
||||
@@ -124,7 +139,7 @@ func (r mediaFileRepository) FindAllByPath(path string) (model.MediaFiles, error
|
||||
}
|
||||
|
||||
// FindPathsRecursively returns a list of all subfolders of basePath, recursively
|
||||
func (r mediaFileRepository) FindPathsRecursively(basePath string) ([]string, error) {
|
||||
func (r *mediaFileRepository) FindPathsRecursively(basePath string) ([]string, error) {
|
||||
path := cleanPath(basePath)
|
||||
// Query based on https://stackoverflow.com/a/38330814/653632
|
||||
sel := r.newSelect().Columns(fmt.Sprintf("distinct rtrim(path, replace(path, '%s', ''))", string(os.PathSeparator))).
|
||||
@@ -134,7 +149,7 @@ func (r mediaFileRepository) FindPathsRecursively(basePath string) ([]string, er
|
||||
return res, err
|
||||
}
|
||||
|
||||
func (r mediaFileRepository) deleteNotInPath(basePath string) error {
|
||||
func (r *mediaFileRepository) deleteNotInPath(basePath string) error {
|
||||
path := cleanPath(basePath)
|
||||
sel := Delete(r.tableName).Where(NotEq(pathStartsWith(path)))
|
||||
c, err := r.executeSQL(sel)
|
||||
@@ -146,28 +161,29 @@ func (r mediaFileRepository) deleteNotInPath(basePath string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func (r mediaFileRepository) GetStarred(options ...model.QueryOptions) (model.MediaFiles, error) {
|
||||
sq := r.selectMediaFile(options...).Where("starred = true")
|
||||
starred := model.MediaFiles{}
|
||||
err := r.queryAll(sq, &starred)
|
||||
return starred, err
|
||||
func (r *mediaFileRepository) GetStarred(options ...model.QueryOptions) (model.MediaFiles, error) {
|
||||
if len(options) == 0 {
|
||||
options = []model.QueryOptions{{}}
|
||||
}
|
||||
options[0].Filters = Eq{"starred": true}
|
||||
return r.GetAll(options...)
|
||||
}
|
||||
|
||||
// TODO Keep order when paginating
|
||||
func (r mediaFileRepository) GetRandom(options ...model.QueryOptions) (model.MediaFiles, error) {
|
||||
sq := r.selectMediaFile(options...)
|
||||
sq = sq.OrderBy("RANDOM()")
|
||||
results := model.MediaFiles{}
|
||||
err := r.queryAll(sq, &results)
|
||||
return results, err
|
||||
func (r *mediaFileRepository) GetRandom(options ...model.QueryOptions) (model.MediaFiles, error) {
|
||||
if len(options) == 0 {
|
||||
options = []model.QueryOptions{{}}
|
||||
}
|
||||
options[0].Sort = "random()"
|
||||
return r.GetAll(options...)
|
||||
}
|
||||
|
||||
func (r mediaFileRepository) Delete(id string) error {
|
||||
func (r *mediaFileRepository) Delete(id string) error {
|
||||
return r.delete(Eq{"id": id})
|
||||
}
|
||||
|
||||
// DeleteByPath delete from the DB all mediafiles that are direct children of path
|
||||
func (r mediaFileRepository) DeleteByPath(basePath string) (int64, error) {
|
||||
func (r *mediaFileRepository) DeleteByPath(basePath string) (int64, error) {
|
||||
path := cleanPath(basePath)
|
||||
pathLen := utf8.RuneCountInString(path)
|
||||
del := Delete(r.tableName).
|
||||
@@ -177,39 +193,39 @@ func (r mediaFileRepository) DeleteByPath(basePath string) (int64, error) {
|
||||
return r.executeSQL(del)
|
||||
}
|
||||
|
||||
func (r mediaFileRepository) Search(q string, offset int, size int) (model.MediaFiles, error) {
|
||||
func (r *mediaFileRepository) Search(q string, offset int, size int) (model.MediaFiles, error) {
|
||||
results := model.MediaFiles{}
|
||||
err := r.doSearch(q, offset, size, &results, "title")
|
||||
return results, err
|
||||
}
|
||||
|
||||
func (r mediaFileRepository) Count(options ...rest.QueryOptions) (int64, error) {
|
||||
func (r *mediaFileRepository) Count(options ...rest.QueryOptions) (int64, error) {
|
||||
return r.CountAll(r.parseRestOptions(options...))
|
||||
}
|
||||
|
||||
func (r mediaFileRepository) Read(id string) (interface{}, error) {
|
||||
func (r *mediaFileRepository) Read(id string) (interface{}, error) {
|
||||
return r.Get(id)
|
||||
}
|
||||
|
||||
func (r mediaFileRepository) ReadAll(options ...rest.QueryOptions) (interface{}, error) {
|
||||
func (r *mediaFileRepository) ReadAll(options ...rest.QueryOptions) (interface{}, error) {
|
||||
return r.GetAll(r.parseRestOptions(options...))
|
||||
}
|
||||
|
||||
func (r mediaFileRepository) EntityName() string {
|
||||
func (r *mediaFileRepository) EntityName() string {
|
||||
return "mediafile"
|
||||
}
|
||||
|
||||
func (r mediaFileRepository) NewInstance() interface{} {
|
||||
func (r *mediaFileRepository) NewInstance() interface{} {
|
||||
return &model.MediaFile{}
|
||||
}
|
||||
|
||||
func (r mediaFileRepository) Save(entity interface{}) (string, error) {
|
||||
func (r *mediaFileRepository) Save(entity interface{}) (string, error) {
|
||||
mf := entity.(*model.MediaFile)
|
||||
err := r.Put(mf)
|
||||
return mf.ID, err
|
||||
}
|
||||
|
||||
func (r mediaFileRepository) Update(entity interface{}, cols ...string) error {
|
||||
func (r *mediaFileRepository) Update(entity interface{}, cols ...string) error {
|
||||
mf := entity.(*model.MediaFile)
|
||||
return r.Put(mf)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user