Add x-total-count to Subsonic API getAlbumList (#1360)

* Add x-total-count to Subsonic API getAlbumList

* Rename variable

Co-authored-by: Deluan <deluan@navidrome.org>
This commit is contained in:
caiocotts 2021-09-21 20:40:39 -04:00 committed by GitHub
parent 73a2271cdd
commit 210dc6b12e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 22 deletions

View File

@ -4,6 +4,7 @@ import (
"context"
"errors"
"net/http"
"strconv"
"time"
"github.com/navidrome/navidrome/core/scrobbler"
@ -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
@ -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)
@ -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")
}
return albums, nil
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, 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

View File

@ -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))
})
@ -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))
})

View File

@ -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)