Keep playing random tracks after the playlist ends

This commit is contained in:
Elias Projahn 2022-02-08 17:44:21 +01:00
parent 42d1d047e3
commit 487fd0a048
3 changed files with 35 additions and 7 deletions

View file

@ -49,7 +49,7 @@ struct MediumRow {
}
/// Table data for a [`Track`].
#[derive(Insertable, Queryable, Debug, Clone)]
#[derive(Insertable, Queryable, QueryableByName, Debug, Clone)]
#[table_name = "tracks"]
struct TrackRow {
pub id: String,
@ -224,6 +224,17 @@ impl Database {
Ok(tracks)
}
/// Get a random track from the database.
pub fn random_track(&self) -> Result<Track> {
let row = diesel::sql_query("SELECT * FROM tracks ORDER BY RANDOM() LIMIT 1")
.load::<TrackRow>(&self.connection)?
.into_iter()
.next()
.ok_or(Error::Other("Failed to generate random track"))?;
self.get_track_from_row(row)
}
/// Retrieve all available information on a medium from related tables.
fn get_medium_data(&self, row: MediumRow) -> Result<Medium> {
let track_rows = tracks::table