Skip to content

Commit

Permalink
Implement browsing album lists by year
Browse files Browse the repository at this point in the history
  • Loading branch information
ironsmile committed May 19, 2024
1 parent 830556e commit bfd38af
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 14 deletions.
11 changes: 11 additions & 0 deletions src/library/browser.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ const (

// OrderByArtistName orders lists by the artist name of its items.
OrderByArtistName

// OrderByYear orders the lists by the year of recording.
OrderByYear
)

// BrowseArgs defines all arguments one can pass to the browse methods to later
Expand Down Expand Up @@ -79,6 +82,14 @@ type BrowseArgs struct {
// ArtistID may be used for filtering the results so that only results which
// belong this ArtistID are returned.
ArtistID int64

// FromYear is the inclusive lower limit for the year of recording of the returned
// results.
FromYear *int64

// To year is the inclusive upper limit for the year of recording for the returned
// results.
ToYear *int64
}

//counterfeiter:generate . Browser
Expand Down
43 changes: 33 additions & 10 deletions src/library/local_browse.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,32 +145,43 @@ func (lib *LocalLibrary) BrowseAlbums(args BrowseArgs) ([]Album, int) {
queryArgs = append(queryArgs, sql.Named("artistID", args.ArtistID))
}

order := "ASC"
orderBy := "al.name"
if args.FromYear != nil {
where = append(where, "tr.year >= @fromYear")
queryArgs = append(queryArgs, sql.Named("fromYear", *args.FromYear))
}

if args.ToYear != nil {
where = append(where, "tr.year <= @toYear")
queryArgs = append(queryArgs, sql.Named("toYear", *args.ToYear))
}

order := "ASC"
if args.Order == OrderDesc {
order = "DESC"
}

orderBy := "al.name " + order

switch args.OrderBy {
case OrderByID:
orderBy = "al.id"
orderBy = "al.id " + order
case OrderByRandom:
orderBy = "RANDOM()"
order = ""

// When ordering by random the offset does not matter. Only the limit
// does.
offset = 0
case OrderByFrequentlyPlayed:
orderBy = "SUM(us.play_count)"
orderBy = "SUM(us.play_count) " + order
case OrderByRecentlyPlayed:
orderBy = "MAX(us.last_played)"
orderBy = "MAX(us.last_played) " + order
case OrderByArtistName:
orderBy = "artist_name"
orderBy = "artist_name " + order
case OrderByFavourites:
orderBy = "als.favourite"
orderBy = "als.favourite " + order
where = append(where, "als.favourite IS NOT NULL AND als.favourite != 0")
case OrderByYear:
orderBy = "tr.year " + order
}

if len(where) > 0 {
Expand Down Expand Up @@ -230,10 +241,10 @@ func (lib *LocalLibrary) BrowseAlbums(args BrowseArgs) ([]Album, int) {
GROUP BY
tr.album_id
ORDER BY
%s %s
%s
LIMIT
@offset, @perPage
`, whereStr, orderBy, order), queryArgs...)
`, whereStr, orderBy), queryArgs...)

if err != nil {
return err
Expand Down Expand Up @@ -299,6 +310,16 @@ func (lib *LocalLibrary) BrowseTracks(args BrowseArgs) ([]TrackInfo, int) {
queryArgs = append(queryArgs, sql.Named("artistID", args.ArtistID))
}

if args.FromYear != nil {
where = append(where, "t.year >= @fromYear")
queryArgs = append(queryArgs, sql.Named("fromYear", *args.FromYear))
}

if args.ToYear != nil {
where = append(where, "t.year <= @toYear")
queryArgs = append(queryArgs, sql.Named("toYear", *args.ToYear))
}

order := "ASC"

if args.Order == OrderDesc {
Expand Down Expand Up @@ -327,6 +348,8 @@ func (lib *LocalLibrary) BrowseTracks(args BrowseArgs) ([]TrackInfo, int) {
case OrderByFavourites:
orderBy = "us.favourite " + order
where = append(where, "us.favourite IS NOT NULL AND us.favourite != 0")
case OrderByYear:
orderBy = "t.year " + order
}

queryArgs = append(
Expand Down
21 changes: 20 additions & 1 deletion src/webserver/subsonic/get_album_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,26 @@ func (s *subsonic) getAlbumList(w http.ResponseWriter, req *http.Request) {
case "alphabeticalByArtist":
browseArgs.OrderBy = library.OrderByArtistName
case "byYear":
fallthrough
fromYear, fromErr := strconv.ParseInt(req.Form.Get("fromYear"), 10, 64)
toYear, toErr := strconv.ParseInt(req.Form.Get("toYear"), 10, 64)
if fromErr != nil || toErr != nil {
resp := responseError(
errCodeMissingParameter,
"valid `fromYear` and `toYear` are required when type=byYear",
)
encodeResponse(w, req, resp)
return
}

browseArgs.Order = library.OrderAsc
browseArgs.FromYear = &fromYear
browseArgs.ToYear = &toYear

if fromYear > toYear {
browseArgs.Order = library.OrderDesc
browseArgs.FromYear = &toYear
browseArgs.ToYear = &fromYear
}
case "byGenre":
resp := responseError(
errCodeGeneric,
Expand Down
21 changes: 20 additions & 1 deletion src/webserver/subsonic/get_album_list_2.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,26 @@ func (s *subsonic) getAlbumList2(w http.ResponseWriter, req *http.Request) {
case "alphabeticalByArtist":
browseArgs.OrderBy = library.OrderByArtistName
case "byYear":
fallthrough
fromYear, fromErr := strconv.ParseInt(req.Form.Get("fromYear"), 10, 64)
toYear, toErr := strconv.ParseInt(req.Form.Get("toYear"), 10, 64)
if fromErr != nil || toErr != nil {
resp := responseError(
errCodeMissingParameter,
"valid `fromYear` and `toYear` are required when type=byYear",
)
encodeResponse(w, req, resp)
return
}

browseArgs.Order = library.OrderAsc
browseArgs.FromYear = &fromYear
browseArgs.ToYear = &toYear

if fromYear > toYear {
browseArgs.Order = library.OrderDesc
browseArgs.FromYear = &toYear
browseArgs.ToYear = &fromYear
}
case "byGenre":
resp := responseError(
errCodeGeneric,
Expand Down
4 changes: 2 additions & 2 deletions src/webserver/subsonic/progress.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
- [ ] getSimilarSongs
- [ ] getSimilarSongs2
- [x] getTopSongs
- [x] getAlbumList - `byYear` and `byGenre` not implemented yet
- [x] getAlbumList2 - `byYear` and `byGenre` not implemented yet
- [x] getAlbumList - `byGenre` not implemented yet
- [x] getAlbumList2 - `byGenre` not implemented yet
- [ ] getRandomSongs
- [ ] getSongsByGenre
- [ ] getNowPlaying
Expand Down

0 comments on commit bfd38af

Please sign in to comment.