2020-11-17 15:52:47 +01:00
|
|
|
use super::schema::instruments;
|
2021-02-04 21:47:22 +01:00
|
|
|
use super::{Database, Result};
|
2020-11-17 15:52:47 +01:00
|
|
|
use diesel::prelude::*;
|
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
|
|
|
|
|
/// An instrument or any other possible role within a recording.
|
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 Instrument {
|
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 instrument or insert a new one.
|
2021-02-04 21:47:22 +01:00
|
|
|
pub fn update_instrument(&self, instrument: Instrument) -> Result<()> {
|
2020-11-17 15:52:47 +01:00
|
|
|
self.defer_foreign_keys()?;
|
|
|
|
|
|
|
|
|
|
self.connection.transaction(|| {
|
|
|
|
|
diesel::replace_into(instruments::table)
|
2020-11-28 21:45:22 +01:00
|
|
|
.values(instrument)
|
2020-11-17 15:52:47 +01:00
|
|
|
.execute(&self.connection)
|
|
|
|
|
})?;
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Get an existing instrument.
|
2021-02-04 21:47:22 +01:00
|
|
|
pub fn get_instrument(&self, id: &str) -> Result<Option<Instrument>> {
|
2020-11-28 21:45:22 +01:00
|
|
|
let instrument = instruments::table
|
|
|
|
|
.filter(instruments::id.eq(id))
|
|
|
|
|
.load::<Instrument>(&self.connection)?
|
|
|
|
|
.into_iter()
|
|
|
|
|
.next();
|
2020-11-17 15:52:47 +01:00
|
|
|
|
|
|
|
|
Ok(instrument)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Delete an existing instrument.
|
2021-02-04 21:47:22 +01:00
|
|
|
pub fn delete_instrument(&self, id: &str) -> Result<()> {
|
2020-11-28 21:45:22 +01:00
|
|
|
diesel::delete(instruments::table.filter(instruments::id.eq(id)))
|
2020-11-17 15:52:47 +01:00
|
|
|
.execute(&self.connection)?;
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Get all existing instruments.
|
2021-02-04 21:47:22 +01:00
|
|
|
pub fn get_instruments(&self) -> Result<Vec<Instrument>> {
|
2020-11-28 21:45:22 +01:00
|
|
|
let instruments = instruments::table.load::<Instrument>(&self.connection)?;
|
2020-11-17 15:52:47 +01:00
|
|
|
|
|
|
|
|
Ok(instruments)
|
|
|
|
|
}
|
|
|
|
|
}
|