Skip to content

🎧 Your Personal Streaming Service

Fix Count methods

Deluana0cd58540121
diff --git a/persistence/album_repository.go b/persistence/album_repository.go
index 12a147c65..8f5b348dd 100644
--- a/persistence/album_repository.go
+++ b/persistence/album_repository.go
@@ -82,24 +82,22 @@ func artistFilter(field string, value interface{}) Sqlizer {
}
func (r *albumRepository) CountAll(options ...model.QueryOptions) (int64, error) {
- sql := r.selectAlbum()
- sql = sql.LeftJoin("album_genres ag on album.id = ag.album_id").
- LeftJoin("genre on ag.genre_id = genre.id").
- GroupBy("album.id")
-
+ sql := r.newSelectWithAnnotation("album.id")
+ sql = r.withGenres(sql)
return r.count(sql, options...)
}
func (r *albumRepository) Exists(id string) (bool, error) {
- return r.exists(Select().Where(Eq{"id": id}))
+ return r.exists(Select().Where(Eq{"album.id": id}))
}
func (r *albumRepository) selectAlbum(options ...model.QueryOptions) SelectBuilder {
- return r.newSelectWithAnnotation("album.id", options...).Columns("album.*")
+ sql := r.newSelectWithAnnotation("album.id", options...).Columns("album.*")
+ return r.withGenres(sql).GroupBy("album.id")
}
func (r *albumRepository) Get(id string) (*model.Album, error) {
- sq := r.selectAlbum().Where(Eq{"id": id})
+ sq := r.selectAlbum().Where(Eq{"album.id": id})
var res model.Albums
if err := r.queryAll(sq, &res); err != nil {
return nil, err
@@ -123,10 +121,7 @@ func (r *albumRepository) Put(m *model.Album) error {
}
func (r *albumRepository) GetAll(options ...model.QueryOptions) (model.Albums, error) {
- sq := r.selectAlbum(options...).
- LeftJoin("album_genres ag on album.id = ag.album_id").
- LeftJoin("genre on ag.genre_id = genre.id").
- GroupBy("album.id")
+ sq := r.selectAlbum(options...)
res := model.Albums{}
err := r.queryAll(sq, &res)
if err != nil {
@@ -398,7 +393,7 @@ func (r *albumRepository) NewInstance() interface{} {
}
func (r albumRepository) Delete(id string) error {
- return r.delete(Eq{"id": id})
+ return r.delete(Eq{"album.id": id})
}
func (r albumRepository) Save(entity interface{}) (string, error) {
diff --git a/persistence/artist_repository.go b/persistence/artist_repository.go
index b00f71587..3482306ea 100644
--- a/persistence/artist_repository.go
+++ b/persistence/artist_repository.go
@@ -45,14 +45,13 @@ func NewArtistRepository(ctx context.Context, o orm.Ormer) model.ArtistRepositor
}
func (r *artistRepository) selectArtist(options ...model.QueryOptions) SelectBuilder {
- return r.newSelectWithAnnotation("artist.id", options...).Columns("artist.*")
+ sql := r.newSelectWithAnnotation("artist.id", options...).Columns("artist.*")
+ return r.withGenres(sql).GroupBy("artist.id")
}
func (r *artistRepository) CountAll(options ...model.QueryOptions) (int64, error) {
sql := r.newSelectWithAnnotation("artist.id")
- sql = sql.LeftJoin("artist_genres ag on artist.id = ag.artist_id").
- LeftJoin("genre on ag.genre_id = genre.id").
- GroupBy("artist.id")
+ sql = r.withGenres(sql)
return r.count(sql, options...)
}
@@ -88,10 +87,7 @@ func (r *artistRepository) Get(id string) (*model.Artist, error) {
}
func (r *artistRepository) GetAll(options ...model.QueryOptions) (model.Artists, error) {
- sel := r.selectArtist(options...).
- LeftJoin("artist_genres ag on artist.id = ag.artist_id").
- LeftJoin("genre on ag.genre_id = genre.id").
- GroupBy("artist.id")
+ sel := r.selectArtist(options...)
var dba []dbArtist
err := r.queryAll(sel, &dba)
if err != nil {
diff --git a/persistence/mediafile_repository.go b/persistence/mediafile_repository.go
index f9307a1e1..8164d05c9 100644
--- a/persistence/mediafile_repository.go
+++ b/persistence/mediafile_repository.go
@@ -39,9 +39,7 @@ func NewMediaFileRepository(ctx context.Context, o orm.Ormer) *mediaFileReposito
func (r *mediaFileRepository) CountAll(options ...model.QueryOptions) (int64, error) {
sql := r.newSelectWithAnnotation("media_file.id")
- sql = sql.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")
+ sql = r.withGenres(sql)
return r.count(sql, options...)
}
@@ -64,7 +62,8 @@ func (r *mediaFileRepository) Put(m *model.MediaFile) error {
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")
+ sql = r.withBookmark(sql, "media_file.id")
+ return r.withGenres(sql).GroupBy("media_file.id")
}
func (r *mediaFileRepository) Get(id string) (*model.MediaFile, error) {
@@ -81,10 +80,7 @@ func (r *mediaFileRepository) Get(id string) (*model.MediaFile, error) {
}
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")
+ sq := r.selectMediaFile(options...)
res := model.MediaFiles{}
err := r.queryAll(sq, &res)
if err != nil {
diff --git a/persistence/sql_base_repository.go b/persistence/sql_base_repository.go
index ef5986339..8ac2d43f5 100644
--- a/persistence/sql_base_repository.go
+++ b/persistence/sql_base_repository.go
@@ -179,7 +179,7 @@ func (r sqlRepository) exists(existsQuery SelectBuilder) (bool, error) {
}
func (r sqlRepository) count(countQuery SelectBuilder, options ...model.QueryOptions) (int64, error) {
- countQuery = countQuery.Columns("count(*) as count").From(r.tableName)
+ countQuery = countQuery.Columns("count(distinct " + r.tableName + ".id) as count").From(r.tableName)
countQuery = r.applyFilters(countQuery, options...)
var res struct{ Count int64 }
err := r.queryOne(countQuery, &res)
diff --git a/persistence/sql_genres.go b/persistence/sql_genres.go
index f8d8bd3cd..31c1d9e06 100644
--- a/persistence/sql_genres.go
+++ b/persistence/sql_genres.go
@@ -5,6 +5,11 @@ import (
"github.com/navidrome/navidrome/model"
)
+func (r sqlRepository) withGenres(sql SelectBuilder) SelectBuilder {
+ return sql.LeftJoin(r.tableName + "_genres ag on " + r.tableName + ".id = ag." + r.tableName + "_id").
+ LeftJoin("genre on ag.genre_id = genre.id")
+}
+
func (r *sqlRepository) updateGenres(id string, tableName string, genres model.Genres) error {
var ids []string
for _, g := range genres {