2020-11-17 15:52:47 +01:00
|
|
|
use super::schema::ensembles;
|
|
|
|
|
use super::Database;
|
2020-11-28 21:45:22 +01:00
|
|
|
use anyhow::Result;
|
2020-11-17 15:52:47 +01:00
|
|
|
use diesel::prelude::*;
|
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
|
|
|
|
|
/// An ensemble that takes part in recordings.
|
2020-11-28 21:45:22 +01:00
|
|
|
#[derive(Serialize, Deserialize, Insertable, Queryable, Debug, Clone)]
|
2020-11-17 15:52:47 +01:00
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
|
pub struct Ensemble {
|
2020-11-28 21:45:22 +01:00
|
|
|
pub id: String,
|
2020-11-17 15:52:47 +01:00
|
|
|
pub name: String,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Database {
|
|
|
|
|
/// Update an existing ensemble or insert a new one.
|
|
|
|
|
pub fn update_ensemble(&self, ensemble: Ensemble) -> Result<()> {
|
|
|
|
|
self.defer_foreign_keys()?;
|
|
|
|
|
|
|
|
|
|
self.connection.transaction(|| {
|
|
|
|
|
diesel::replace_into(ensembles::table)
|
2020-11-28 21:45:22 +01:00
|
|
|
.values(ensemble)
|
2020-11-17 15:52:47 +01:00
|
|
|
.execute(&self.connection)
|
|
|
|
|
})?;
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Get an existing ensemble.
|
2020-11-28 21:45:22 +01:00
|
|
|
pub fn get_ensemble(&self, id: &str) -> Result<Option<Ensemble>> {
|
|
|
|
|
let ensemble = ensembles::table
|
|
|
|
|
.filter(ensembles::id.eq(id))
|
|
|
|
|
.load::<Ensemble>(&self.connection)?
|
|
|
|
|
.into_iter()
|
|
|
|
|
.next();
|
2020-11-17 15:52:47 +01:00
|
|
|
|
|
|
|
|
Ok(ensemble)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Delete an existing ensemble.
|
2020-11-28 21:45:22 +01:00
|
|
|
pub fn delete_ensemble(&self, id: &str) -> Result<()> {
|
|
|
|
|
diesel::delete(ensembles::table.filter(ensembles::id.eq(id))).execute(&self.connection)?;
|
2020-11-17 15:52:47 +01:00
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Get all existing ensembles.
|
|
|
|
|
pub fn get_ensembles(&self) -> Result<Vec<Ensemble>> {
|
2020-11-28 21:45:22 +01:00
|
|
|
let ensembles = ensembles::table.load::<Ensemble>(&self.connection)?;
|
2020-11-17 15:52:47 +01:00
|
|
|
|
|
|
|
|
Ok(ensembles)
|
|
|
|
|
}
|
|
|
|
|
}
|