Skip to content

Commit

Permalink
Add x-total-count to Subsonic API getAlbumList (#1360)
Browse files Browse the repository at this point in the history
* Add x-total-count to Subsonic API getAlbumList

* Rename variable

Co-authored-by: Deluan <deluan@navidrome.org>
  • Loading branch information
caiocotts and deluan committed Sep 22, 2021
1 parent 73a2271 commit 210dc6b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 22 deletions.
25 changes: 18 additions & 7 deletions server/subsonic/album_lists.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"net/http"
"strconv"
"time"

"github.com/navidrome/navidrome/core/scrobbler"
Expand All @@ -27,10 +28,10 @@ func NewAlbumListController(ds model.DataStore, scrobbler scrobbler.PlayTracker)
return c
}

func (c *AlbumListController) getAlbumList(r *http.Request) (model.Albums, error) {
func (c *AlbumListController) getAlbumList(r *http.Request) (model.Albums, int64, error) {
typ, err := requiredParamString(r, "type")
if err != nil {
return nil, err
return nil, 0, err
}

var opts filter.Options
Expand All @@ -57,7 +58,7 @@ func (c *AlbumListController) getAlbumList(r *http.Request) (model.Albums, error
opts = filter.AlbumsByYear(utils.ParamInt(r, "fromYear", 0), utils.ParamInt(r, "toYear", 0))
default:
log.Error(r, "albumList type not implemented", "type", typ)
return nil, errors.New("not implemented")
return nil, 0, errors.New("not implemented")
}

opts.Offset = utils.ParamInt(r, "offset", 0)
Expand All @@ -66,29 +67,39 @@ func (c *AlbumListController) getAlbumList(r *http.Request) (model.Albums, error

if err != nil {
log.Error(r, "Error retrieving albums", "error", err)
return nil, errors.New("internal Error")
return nil, 0, errors.New("internal error")
}

count, err := c.ds.Album(r.Context()).CountAll(opts)
if err != nil {
log.Error(r, "Error counting albums", "error", err)
return nil, 0, errors.New("internal error")
}

return albums, nil
return albums, count, nil
}

func (c *AlbumListController) GetAlbumList(w http.ResponseWriter, r *http.Request) (*responses.Subsonic, error) {
albums, err := c.getAlbumList(r)
albums, count, err := c.getAlbumList(r)
if err != nil {
return nil, newError(responses.ErrorGeneric, err.Error())
}

w.Header().Set("x-total-count", strconv.Itoa(int(count)))

response := newResponse()
response.AlbumList = &responses.AlbumList{Album: childrenFromAlbums(r.Context(), albums)}
return response, nil
}

func (c *AlbumListController) GetAlbumList2(w http.ResponseWriter, r *http.Request) (*responses.Subsonic, error) {
albums, err := c.getAlbumList(r)
albums, pageCount, err := c.getAlbumList(r)
if err != nil {
return nil, newError(responses.ErrorGeneric, err.Error())
}

w.Header().Set("x-total-count", strconv.FormatInt(pageCount, 10))

response := newResponse()
response.AlbumList2 = &responses.AlbumList{Album: childrenFromAlbums(r.Context(), albums)}
return response, nil
Expand Down
2 changes: 2 additions & 0 deletions server/subsonic/album_lists_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ var _ = Describe("AlbumListController", func() {
Expect(err).To(BeNil())
Expect(resp.AlbumList.Album[0].Id).To(Equal("1"))
Expect(resp.AlbumList.Album[1].Id).To(Equal("2"))
Expect(w.Header().Get("x-total-count")).To(Equal("2"))
Expect(mockRepo.Options.Offset).To(Equal(10))
Expect(mockRepo.Options.Max).To(Equal(20))
})
Expand Down Expand Up @@ -68,6 +69,7 @@ var _ = Describe("AlbumListController", func() {
Expect(err).To(BeNil())
Expect(resp.AlbumList2.Album[0].Id).To(Equal("1"))
Expect(resp.AlbumList2.Album[1].Id).To(Equal("2"))
Expect(w.Header().Get("x-total-count")).To(Equal("2"))
Expect(mockRepo.Options.Offset).To(Equal(10))
Expect(mockRepo.Options.Max).To(Equal(20))
})
Expand Down
17 changes: 2 additions & 15 deletions tests/mock_album_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,21 +85,8 @@ func (m *MockAlbumRepo) IncPlayCount(id string, timestamp time.Time) error {
}
return model.ErrNotFound
}

func (m *MockAlbumRepo) FindByArtist(artistId string) (model.Albums, error) {
if m.err {
return nil, errors.New("Error!")
}
var res = make(model.Albums, len(m.data))
i := 0
for _, a := range m.data {
if a.AlbumArtistID == artistId {
res[i] = *a
i++
}
}

return res, nil
func (m *MockAlbumRepo) CountAll(...model.QueryOptions) (int64, error) {
return int64(len(m.all)), nil
}

var _ model.AlbumRepository = (*MockAlbumRepo)(nil)

0 comments on commit 210dc6b

Please sign in to comment.