Skip to content

Commit

Permalink
skip random
Browse files Browse the repository at this point in the history
  • Loading branch information
th0rn0 committed May 3, 2024
1 parent a851701 commit c966fd7
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions api/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func getNextSong() (Track, error) {
if voteToSkipEnabled {
nextTrack, err = getNextSongRandom()
} else {
nextTrack, err = getNextSongByVotes()
nextTrack, err = getNextSongByVotes()
}
if err != nil {
// Assume no record - get from fallback playlist
Expand All @@ -28,7 +28,11 @@ func getNextSong() (Track, error) {
func getNextSongExcludeURI(excludeUri spotify.URI) (Track, error) {
var nextTrack Track
var err error
nextTrack, err = getNextSongByVotesExcludeURI(excludeUri)
if voteToSkipEnabled {
nextTrack, err = getNextSongRandomExcludeURI(excludeUri)
} else {
nextTrack, err = getNextSongByVotesExcludeURI(excludeUri)
}
if err != nil {
// Assume no record - get from fallback playlist
nextTrack = assignFallback(nextTrack)
Expand All @@ -46,6 +50,14 @@ func getNextSongRandom() (Track, error) {
return track, nil
}

func getNextSongRandomExcludeURI(excludeUri spotify.URI) (Track, error) {
var track Track
if err := db.Raw("SELECT * FROM tracks AND uri != ? ORDER BY random()", excludeUri).First(&track).Error; err != nil {
return track, err
}
return track, nil
}

func getNextSongByVotes() (Track, error) {
var track Track
if err := db.Raw("SELECT * FROM tracks WHERE votes = ( SELECT MAX(votes) FROM tracks )").First(&track).Error; err != nil {
Expand Down

0 comments on commit c966fd7

Please sign in to comment.