Add user preferences for random playback

This commit is contained in:
Elias Projahn 2022-04-08 19:59:49 +02:00
parent 3ae5727f39
commit 5c64bdef7e
14 changed files with 402 additions and 156 deletions

View file

@ -76,7 +76,7 @@ impl PersonOrEnsemble {
}
/// Database table data for a recording.
#[derive(Insertable, Queryable, Debug, Clone)]
#[derive(Insertable, Queryable, QueryableByName, Debug, Clone)]
#[table_name = "recordings"]
struct RecordingRow {
pub id: String,
@ -201,6 +201,17 @@ impl Database {
Ok(recording)
}
/// Get a random recording from the database.
pub fn random_recording(&self) -> Result<Recording> {
let row = diesel::sql_query("SELECT * FROM recordings ORDER BY RANDOM() LIMIT 1")
.load::<RecordingRow>(&self.connection)?
.into_iter()
.next()
.ok_or(Error::Other("Failed to find random recording."))?;
self.get_recording_data(row)
}
/// Retrieve all available information on a recording from related tables.
fn get_recording_data(&self, row: RecordingRow) -> Result<Recording> {
let mut performance_descriptions: Vec<Performance> = Vec::new();