mirror of
https://github.com/navidrome/navidrome.git
synced 2025-04-03 20:47:35 +03:00
Add GetGenre endpoint
This commit is contained in:
parent
ca2c897340
commit
36d93774bc
22 changed files with 303 additions and 26 deletions
59
persistence/genre_repository.go
Normal file
59
persistence/genre_repository.go
Normal file
|
@ -0,0 +1,59 @@
|
|||
package persistence
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
|
||||
"github.com/astaxie/beego/orm"
|
||||
"github.com/cloudsonic/sonic-server/model"
|
||||
)
|
||||
|
||||
type genreRepository struct{}
|
||||
|
||||
func NewGenreRepository() model.GenreRepository {
|
||||
return &genreRepository{}
|
||||
}
|
||||
|
||||
func (r genreRepository) GetAll() (model.Genres, error) {
|
||||
o := Db()
|
||||
genres := make(map[string]model.Genre)
|
||||
|
||||
// Collect SongCount
|
||||
var res []orm.Params
|
||||
_, err := o.Raw("select genre, count(*) as c from media_file group by genre").Values(&res)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, r := range res {
|
||||
name := r["genre"].(string)
|
||||
count := r["c"].(string)
|
||||
g, ok := genres[name]
|
||||
if !ok {
|
||||
g = model.Genre{Name: name}
|
||||
}
|
||||
g.SongCount, _ = strconv.Atoi(count)
|
||||
genres[name] = g
|
||||
}
|
||||
|
||||
// Collect AlbumCount
|
||||
_, err = o.Raw("select genre, count(*) as c from album group by genre").Values(&res)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, r := range res {
|
||||
name := r["genre"].(string)
|
||||
count := r["c"].(string)
|
||||
g, ok := genres[name]
|
||||
if !ok {
|
||||
g = model.Genre{Name: name}
|
||||
}
|
||||
g.AlbumCount, _ = strconv.Atoi(count)
|
||||
genres[name] = g
|
||||
}
|
||||
|
||||
// Build response
|
||||
result := model.Genres{}
|
||||
for _, g := range genres {
|
||||
result = append(result, g)
|
||||
}
|
||||
return result, err
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue