Adding GetAlbums with AlbumOptions and default values, and unit tests

Signed-off-by: Kris Nóva <kris@nivenly.com>
This commit is contained in:
Kris Nóva 2021-02-11 14:21:44 -08:00
parent 6b742a1db4
commit 06fff46e43
8 changed files with 165 additions and 5 deletions

View file

@ -1,6 +1,54 @@
package api
type AlbumOptions struct {
ParamType string
Q string
Count int
Offset int
Category string
}
const (
DefaultAlbumOptionsParamType = "album"
DefaultAlbumOptionsQ = ""
DefaultAlbumOptionsCount = 24
DefaultAlbumOptionsOffset = 0
DefaultAlbumOptionsCategory = ""
)
// GET /api/v1/albums
//
// Example Params: http://localhost:8080/api/v1/albums?count=24&offset=0&q=&category=&type=album
func (v1 *V1Client) GetAlbums(options *AlbumOptions) ([]Album, error) {
albums := []Album{{}}
if options == nil {
// Default to sane options for query
options = &AlbumOptions{
ParamType: "album",
Q: "",
Count: 24,
Offset: 0,
Category: "",
}
}
// Checks for missing fields
if options.Count == 0 {
return albums, nil
}
if options.ParamType == "" {
options.ParamType = DefaultAlbumOptionsParamType
}
// NOTE: Even though this method is singular GetAlbum
// if will call the "albums" plural endpoint.
err := v1.GET("/api/v1/albums?count=%d&offset=%d&q=%s&category=%s&type=%s", options.Count, options.Offset, options.Q, options.Category, options.ParamType).JSON(&albums)
return albums, err
}
// GET /api/v1/albums/:uuid
func (v1 *V1Client) GetAlbum(uuid string) (Album, error) {
album := Album{}
@ -8,4 +56,4 @@ func (v1 *V1Client) GetAlbum(uuid string) (Album, error) {
// if will call the "albums" plural endpoint.
err := v1.GET("/api/v1/albums/%s", uuid).JSON(&album)
return album, err
}
}