Add GetGenre endpoint
This commit is contained in:
@@ -23,7 +23,7 @@ type Album struct {
|
||||
SongCount int ``
|
||||
Duration int ``
|
||||
Rating int `orm:"index"`
|
||||
Genre string ``
|
||||
Genre string `orm:"index"`
|
||||
StarredAt time.Time `orm:"null"`
|
||||
CreatedAt time.Time `orm:"null"`
|
||||
UpdatedAt time.Time `orm:"null"`
|
||||
|
||||
@@ -20,37 +20,37 @@ var _ = Describe("AlbumRepository", func() {
|
||||
|
||||
It("returns all records sorted", func() {
|
||||
Expect(repo.GetAll(model.QueryOptions{SortBy: "Name"})).To(Equal(model.Albums{
|
||||
{ID: "2", Name: "Abbey Road", Artist: "The Beatles", ArtistID: "1"},
|
||||
{ID: "3", Name: "Radioactivity", Artist: "Kraftwerk", ArtistID: "2", Starred: true},
|
||||
{ID: "1", Name: "Sgt Peppers", Artist: "The Beatles", ArtistID: "1"},
|
||||
albumAbbeyRoad,
|
||||
albumRadioactivity,
|
||||
albumSgtPeppers,
|
||||
}))
|
||||
})
|
||||
|
||||
It("returns all records sorted desc", func() {
|
||||
Expect(repo.GetAll(model.QueryOptions{SortBy: "Name", Desc: true})).To(Equal(model.Albums{
|
||||
{ID: "1", Name: "Sgt Peppers", Artist: "The Beatles", ArtistID: "1"},
|
||||
{ID: "3", Name: "Radioactivity", Artist: "Kraftwerk", ArtistID: "2", Starred: true},
|
||||
{ID: "2", Name: "Abbey Road", Artist: "The Beatles", ArtistID: "1"},
|
||||
albumSgtPeppers,
|
||||
albumRadioactivity,
|
||||
albumAbbeyRoad,
|
||||
}))
|
||||
})
|
||||
|
||||
It("paginates the result", func() {
|
||||
Expect(repo.GetAll(model.QueryOptions{Offset: 1, Size: 1})).To(Equal(model.Albums{
|
||||
{ID: "2", Name: "Abbey Road", Artist: "The Beatles", ArtistID: "1"},
|
||||
albumAbbeyRoad,
|
||||
}))
|
||||
})
|
||||
})
|
||||
|
||||
Describe("GetAllIds", func() {
|
||||
It("returns all records", func() {
|
||||
Expect(repo.GetAllIds()).To(Equal([]string{"1", "2", "3"}))
|
||||
Expect(repo.GetAllIds()).To(ConsistOf("1", "2", "3"))
|
||||
})
|
||||
})
|
||||
|
||||
Describe("GetStarred", func() {
|
||||
It("returns all starred records", func() {
|
||||
Expect(repo.GetStarred(model.QueryOptions{})).To(Equal(model.Albums{
|
||||
{ID: "3", Name: "Radioactivity", Artist: "Kraftwerk", ArtistID: "2", Starred: true},
|
||||
albumRadioactivity,
|
||||
}))
|
||||
})
|
||||
})
|
||||
@@ -58,8 +58,8 @@ var _ = Describe("AlbumRepository", func() {
|
||||
Describe("FindByArtist", func() {
|
||||
It("returns all records from a given ArtistID", func() {
|
||||
Expect(repo.FindByArtist("1")).To(Equal(model.Albums{
|
||||
{ID: "2", Name: "Abbey Road", Artist: "The Beatles", ArtistID: "1"},
|
||||
{ID: "1", Name: "Sgt Peppers", Artist: "The Beatles", ArtistID: "1"},
|
||||
albumAbbeyRoad,
|
||||
albumSgtPeppers,
|
||||
}))
|
||||
})
|
||||
})
|
||||
|
||||
@@ -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
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package persistence
|
||||
|
||||
import (
|
||||
"github.com/cloudsonic/sonic-server/model"
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
var _ = Describe("GenreRepository", func() {
|
||||
var repo model.GenreRepository
|
||||
|
||||
BeforeEach(func() {
|
||||
repo = NewGenreRepository()
|
||||
})
|
||||
|
||||
It("returns all records", func() {
|
||||
genres, err := repo.GetAll()
|
||||
Expect(err).To(BeNil())
|
||||
Expect(genres).To(ContainElement(model.Genre{Name: "Rock", AlbumCount: 2, SongCount: 2}))
|
||||
Expect(genres).To(ContainElement(model.Genre{Name: "Electronic", AlbumCount: 1, SongCount: 2}))
|
||||
})
|
||||
})
|
||||
@@ -24,7 +24,7 @@ type MediaFile struct {
|
||||
Suffix string ``
|
||||
Duration int ``
|
||||
BitRate int ``
|
||||
Genre string ``
|
||||
Genre string `orm:"index"`
|
||||
Compilation bool ``
|
||||
PlayCount int `orm:"index"`
|
||||
PlayDate time.Time `orm:"null"`
|
||||
|
||||
@@ -16,19 +16,38 @@ func TestPersistence(t *testing.T) {
|
||||
RunSpecs(t, "Persistence Suite")
|
||||
}
|
||||
|
||||
var testAlbums = model.Albums{
|
||||
{ID: "1", Name: "Sgt Peppers", Artist: "The Beatles", ArtistID: "1"},
|
||||
{ID: "2", Name: "Abbey Road", Artist: "The Beatles", ArtistID: "1"},
|
||||
{ID: "3", Name: "Radioactivity", Artist: "Kraftwerk", ArtistID: "2", Starred: true},
|
||||
}
|
||||
var artistSaaraSaara = model.Artist{ID: "1", Name: "Saara Saara", AlbumCount: 2}
|
||||
var artistKraftwerk = model.Artist{ID: "2", Name: "Kraftwerk"}
|
||||
var artistBeatles = model.Artist{ID: "3", Name: "The Beatles"}
|
||||
var testArtists = model.Artists{
|
||||
{ID: "1", Name: "Saara Saara", AlbumCount: 2},
|
||||
{ID: "2", Name: "Kraftwerk"},
|
||||
{ID: "3", Name: "The Beatles"},
|
||||
artistSaaraSaara,
|
||||
artistKraftwerk,
|
||||
artistBeatles,
|
||||
}
|
||||
|
||||
var albumSgtPeppers = model.Album{ID: "1", Name: "Sgt Peppers", Artist: "The Beatles", ArtistID: "1", Genre: "Rock"}
|
||||
var albumAbbeyRoad = model.Album{ID: "2", Name: "Abbey Road", Artist: "The Beatles", ArtistID: "1", Genre: "Rock"}
|
||||
var albumRadioactivity = model.Album{ID: "3", Name: "Radioactivity", Artist: "Kraftwerk", ArtistID: "2", Starred: true, Genre: "Electronic"}
|
||||
var testAlbums = model.Albums{
|
||||
albumSgtPeppers,
|
||||
albumAbbeyRoad,
|
||||
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 testSongs = model.MediaFiles{
|
||||
songDayInALife,
|
||||
songComeTogether,
|
||||
songRadioactivity,
|
||||
songAntenna,
|
||||
}
|
||||
|
||||
var _ = Describe("Initialize test DB", func() {
|
||||
BeforeSuite(func() {
|
||||
//log.SetLevel(log.LevelTrace)
|
||||
//conf.Sonic.DbPath, _ = ioutil.TempDir("", "cloudsonic_tests")
|
||||
//os.MkdirAll(conf.Sonic.DbPath, 0700)
|
||||
conf.Sonic.DbPath = ":memory:"
|
||||
@@ -44,5 +63,12 @@ var _ = Describe("Initialize test DB", func() {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
mediaFileRepository := NewMediaFileRepository()
|
||||
for _, s := range testSongs {
|
||||
err := mediaFileRepository.Put(&s)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
@@ -14,4 +14,5 @@ var Set = wire.NewSet(
|
||||
NewPlaylistRepository,
|
||||
NewNowPlayingRepository,
|
||||
NewMediaFolderRepository,
|
||||
NewGenreRepository,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user