2020-01-15 23:49:09 +01:00
|
|
|
package persistence
|
|
|
|
|
|
|
|
import (
|
2020-01-28 14:22:17 +01:00
|
|
|
"context"
|
2020-01-15 23:49:09 +01:00
|
|
|
|
2020-01-28 14:22:17 +01:00
|
|
|
. "github.com/Masterminds/squirrel"
|
2020-01-15 23:49:09 +01:00
|
|
|
"github.com/astaxie/beego/orm"
|
2020-01-24 01:44:08 +01:00
|
|
|
"github.com/deluan/navidrome/model"
|
2020-01-15 23:49:09 +01:00
|
|
|
)
|
|
|
|
|
2020-01-19 21:37:41 +01:00
|
|
|
type genreRepository struct {
|
2020-01-28 14:22:17 +01:00
|
|
|
sqlRepository
|
2020-01-19 21:37:41 +01:00
|
|
|
}
|
2020-01-15 23:49:09 +01:00
|
|
|
|
2020-01-28 14:22:17 +01:00
|
|
|
func NewGenreRepository(ctx context.Context, o orm.Ormer) model.GenreRepository {
|
|
|
|
r := &genreRepository{}
|
|
|
|
r.ctx = ctx
|
|
|
|
r.ormer = o
|
|
|
|
r.tableName = "media_file"
|
|
|
|
return r
|
2020-01-15 23:49:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r genreRepository) GetAll() (model.Genres, error) {
|
2020-01-28 14:22:17 +01:00
|
|
|
sq := Select("genre as name", "count(distinct album_id) as album_count", "count(distinct id) as song_count").
|
|
|
|
From("media_file").GroupBy("genre")
|
|
|
|
sql, args, err := r.toSql(sq)
|
2020-01-15 23:49:09 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-28 14:22:17 +01:00
|
|
|
var res model.Genres
|
|
|
|
_, err = r.ormer.Raw(sql, args).QueryRows(&res)
|
|
|
|
return res, err
|
2020-01-15 23:49:09 +01:00
|
|
|
}
|