server: Switch to strings for IDs

This commit is contained in:
Elias Projahn 2020-11-28 20:30:14 +01:00
parent 3b8ed4bdb1
commit 157bdb2917
8 changed files with 132 additions and 140 deletions

View file

@ -9,7 +9,7 @@ use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct Ensemble {
pub id: u32,
pub id: String,
pub name: String,
}
@ -17,7 +17,7 @@ pub struct Ensemble {
#[derive(Insertable, Queryable, AsChangeset, Debug, Clone)]
#[table_name = "ensembles"]
struct EnsembleRow {
pub id: i64,
pub id: String,
pub name: String,
pub created_by: String,
}
@ -25,7 +25,7 @@ struct EnsembleRow {
impl From<EnsembleRow> for Ensemble {
fn from(row: EnsembleRow) -> Ensemble {
Ensemble {
id: row.id as u32,
id: row.id,
name: row.name,
}
}
@ -34,7 +34,7 @@ impl From<EnsembleRow> for Ensemble {
/// Update an existing ensemble or insert a new one. This will only work, if the provided user is
/// allowed to do that.
pub fn update_ensemble(conn: &DbConn, ensemble: &Ensemble, user: &User) -> Result<()> {
let old_row = get_ensemble_row(conn, ensemble.id)?;
let old_row = get_ensemble_row(conn, &ensemble.id)?;
let allowed = match old_row {
Some(row) => user.may_edit(&row.created_by),
@ -43,7 +43,7 @@ pub fn update_ensemble(conn: &DbConn, ensemble: &Ensemble, user: &User) -> Resul
if allowed {
let new_row = EnsembleRow {
id: ensemble.id as i64,
id: ensemble.id.clone(),
name: ensemble.name.clone(),
created_by: user.username.clone(),
};
@ -62,7 +62,7 @@ pub fn update_ensemble(conn: &DbConn, ensemble: &Ensemble, user: &User) -> Resul
}
/// Get an existing ensemble.
pub fn get_ensemble(conn: &DbConn, id: u32) -> Result<Option<Ensemble>> {
pub fn get_ensemble(conn: &DbConn, id: &str) -> Result<Option<Ensemble>> {
let row = get_ensemble_row(conn, id)?;
let ensemble = row.map(|row| row.into());
@ -70,9 +70,9 @@ pub fn get_ensemble(conn: &DbConn, id: u32) -> Result<Option<Ensemble>> {
}
/// Delete an existing ensemble. This will only work if the provided user is allowed to do that.
pub fn delete_ensemble(conn: &DbConn, id: u32, user: &User) -> Result<()> {
pub fn delete_ensemble(conn: &DbConn, id: &str, user: &User) -> Result<()> {
if user.may_delete() {
diesel::delete(ensembles::table.filter(ensembles::id.eq(id as i64))).execute(conn)?;
diesel::delete(ensembles::table.filter(ensembles::id.eq(id))).execute(conn)?;
Ok(())
} else {
Err(Error::new(ServerError::Forbidden))
@ -88,9 +88,9 @@ pub fn get_ensembles(conn: &DbConn) -> Result<Vec<Ensemble>> {
}
/// Get a ensemble row if it exists.
fn get_ensemble_row(conn: &DbConn, id: u32) -> Result<Option<EnsembleRow>> {
fn get_ensemble_row(conn: &DbConn, id: &str) -> Result<Option<EnsembleRow>> {
let row = ensembles::table
.filter(ensembles::id.eq(id as i64))
.filter(ensembles::id.eq(id))
.load::<EnsembleRow>(conn)?
.into_iter()
.next();

View file

@ -9,7 +9,7 @@ use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct Instrument {
pub id: u32,
pub id: String,
pub name: String,
}
@ -17,7 +17,7 @@ pub struct Instrument {
#[derive(Insertable, Queryable, AsChangeset, Debug, Clone)]
#[table_name = "instruments"]
struct InstrumentRow {
pub id: i64,
pub id: String,
pub name: String,
pub created_by: String,
}
@ -25,7 +25,7 @@ struct InstrumentRow {
impl From<InstrumentRow> for Instrument {
fn from(row: InstrumentRow) -> Instrument {
Instrument {
id: row.id as u32,
id: row.id,
name: row.name,
}
}
@ -34,7 +34,7 @@ impl From<InstrumentRow> for Instrument {
/// Update an existing instrument or insert a new one. This will only work, if the provided user is
/// allowed to do that.
pub fn update_instrument(conn: &DbConn, instrument: &Instrument, user: &User) -> Result<()> {
let old_row = get_instrument_row(conn, instrument.id)?;
let old_row = get_instrument_row(conn, &instrument.id)?;
let allowed = match old_row {
Some(row) => user.may_edit(&row.created_by),
@ -43,7 +43,7 @@ pub fn update_instrument(conn: &DbConn, instrument: &Instrument, user: &User) ->
if allowed {
let new_row = InstrumentRow {
id: instrument.id as i64,
id: instrument.id.clone(),
name: instrument.name.clone(),
created_by: user.username.clone(),
};
@ -62,7 +62,7 @@ pub fn update_instrument(conn: &DbConn, instrument: &Instrument, user: &User) ->
}
/// Get an existing instrument.
pub fn get_instrument(conn: &DbConn, id: u32) -> Result<Option<Instrument>> {
pub fn get_instrument(conn: &DbConn, id: &str) -> Result<Option<Instrument>> {
let row = get_instrument_row(conn, id)?;
let instrument = row.map(|row| row.into());
@ -70,9 +70,9 @@ pub fn get_instrument(conn: &DbConn, id: u32) -> Result<Option<Instrument>> {
}
/// Delete an existing instrument. This will only work if the provided user is allowed to do that.
pub fn delete_instrument(conn: &DbConn, id: u32, user: &User) -> Result<()> {
pub fn delete_instrument(conn: &DbConn, id: &str, user: &User) -> Result<()> {
if user.may_delete() {
diesel::delete(instruments::table.filter(instruments::id.eq(id as i64))).execute(conn)?;
diesel::delete(instruments::table.filter(instruments::id.eq(id))).execute(conn)?;
Ok(())
} else {
Err(Error::new(ServerError::Forbidden))
@ -88,9 +88,9 @@ pub fn get_instruments(conn: &DbConn) -> Result<Vec<Instrument>> {
}
/// Get a instrument row if it exists.
fn get_instrument_row(conn: &DbConn, id: u32) -> Result<Option<InstrumentRow>> {
fn get_instrument_row(conn: &DbConn, id: &str) -> Result<Option<InstrumentRow>> {
let row = instruments::table
.filter(instruments::id.eq(id as i64))
.filter(instruments::id.eq(id))
.load::<InstrumentRow>(conn)?
.into_iter()
.next();

View file

@ -9,7 +9,7 @@ use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct Person {
pub id: u32,
pub id: String,
pub first_name: String,
pub last_name: String,
}
@ -18,7 +18,7 @@ pub struct Person {
#[derive(Insertable, Queryable, AsChangeset, Debug, Clone)]
#[table_name = "persons"]
struct PersonRow {
pub id: i64,
pub id: String,
pub first_name: String,
pub last_name: String,
pub created_by: String,
@ -27,7 +27,7 @@ struct PersonRow {
impl From<PersonRow> for Person {
fn from(row: PersonRow) -> Person {
Person {
id: row.id as u32,
id: row.id,
first_name: row.first_name,
last_name: row.last_name,
}
@ -37,7 +37,7 @@ impl From<PersonRow> for Person {
/// Update an existing person or insert a new one. This will only work, if the provided user is
/// allowed to do that.
pub fn update_person(conn: &DbConn, person: &Person, user: &User) -> Result<()> {
let old_row = get_person_row(conn, person.id)?;
let old_row = get_person_row(conn, &person.id)?;
let allowed = match old_row {
Some(row) => user.may_edit(&row.created_by),
@ -46,7 +46,7 @@ pub fn update_person(conn: &DbConn, person: &Person, user: &User) -> Result<()>
if allowed {
let new_row = PersonRow {
id: person.id as i64,
id: person.id.clone(),
first_name: person.first_name.clone(),
last_name: person.last_name.clone(),
created_by: user.username.clone(),
@ -66,7 +66,7 @@ pub fn update_person(conn: &DbConn, person: &Person, user: &User) -> Result<()>
}
/// Get an existing person.
pub fn get_person(conn: &DbConn, id: u32) -> Result<Option<Person>> {
pub fn get_person(conn: &DbConn, id: &str) -> Result<Option<Person>> {
let row = get_person_row(conn, id)?;
let person = row.map(|row| row.into());
@ -74,9 +74,9 @@ pub fn get_person(conn: &DbConn, id: u32) -> Result<Option<Person>> {
}
/// Delete an existing person. This will only work if the provided user is allowed to do that.
pub fn delete_person(conn: &DbConn, id: u32, user: &User) -> Result<()> {
pub fn delete_person(conn: &DbConn, id: &str, user: &User) -> Result<()> {
if user.may_delete() {
diesel::delete(persons::table.filter(persons::id.eq(id as i64))).execute(conn)?;
diesel::delete(persons::table.filter(persons::id.eq(id))).execute(conn)?;
Ok(())
} else {
Err(Error::new(ServerError::Forbidden))
@ -92,9 +92,9 @@ pub fn get_persons(conn: &DbConn) -> Result<Vec<Person>> {
}
/// Get a person row if it exists.
fn get_person_row(conn: &DbConn, id: u32) -> Result<Option<PersonRow>> {
fn get_person_row(conn: &DbConn, id: &str) -> Result<Option<PersonRow>> {
let row = persons::table
.filter(persons::id.eq(id as i64))
.filter(persons::id.eq(id))
.load::<PersonRow>(conn)?
.into_iter()
.next();

View file

@ -10,7 +10,7 @@ use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct Recording {
pub id: u32,
pub id: String,
pub work: Work,
pub comment: String,
pub performances: Vec<Performance>,
@ -29,8 +29,8 @@ pub struct Performance {
#[derive(Insertable, Queryable, Debug, Clone)]
#[table_name = "recordings"]
struct RecordingRow {
pub id: i64,
pub work: i64,
pub id: String,
pub work: String,
pub comment: String,
pub created_by: String,
}
@ -40,10 +40,10 @@ struct RecordingRow {
#[table_name = "performances"]
struct PerformanceRow {
pub id: i64,
pub recording: i64,
pub person: Option<i64>,
pub ensemble: Option<i64>,
pub role: Option<i64>,
pub recording: String,
pub person: Option<String>,
pub ensemble: Option<String>,
pub role: Option<String>,
}
/// Update an existing recording or insert a new one. This will only work, if the provided user is
@ -51,7 +51,7 @@ struct PerformanceRow {
// TODO: Also add newly created associated items.
pub fn update_recording(conn: &DbConn, recording: &Recording, user: &User) -> Result<()> {
conn.transaction::<(), Error, _>(|| {
let old_row = get_recording_row(conn, recording.id)?;
let old_row = get_recording_row(conn, &recording.id)?;
let allowed = match old_row {
Some(row) => user.may_edit(&row.created_by),
@ -59,7 +59,7 @@ pub fn update_recording(conn: &DbConn, recording: &Recording, user: &User) -> Re
};
if allowed {
let id = recording.id as i64;
let id = &recording.id;
// This will also delete the old performances.
diesel::delete(recordings::table)
@ -67,8 +67,8 @@ pub fn update_recording(conn: &DbConn, recording: &Recording, user: &User) -> Re
.execute(conn)?;
let row = RecordingRow {
id,
work: recording.work.id as i64,
id: id.clone(),
work: recording.work.id.clone(),
comment: recording.comment.clone(),
created_by: user.username.clone(),
};
@ -81,13 +81,13 @@ pub fn update_recording(conn: &DbConn, recording: &Recording, user: &User) -> Re
diesel::insert_into(performances::table)
.values(PerformanceRow {
id: rand::random(),
recording: id,
person: performance.person.as_ref().map(|person| person.id as i64),
recording: id.clone(),
person: performance.person.as_ref().map(|person| person.id.clone()),
ensemble: performance
.ensemble
.as_ref()
.map(|ensemble| ensemble.id as i64),
role: performance.role.as_ref().map(|role| role.id as i64),
.map(|ensemble| ensemble.id.clone()),
role: performance.role.as_ref().map(|role| role.id.clone()),
})
.execute(conn)?;
}
@ -102,7 +102,7 @@ pub fn update_recording(conn: &DbConn, recording: &Recording, user: &User) -> Re
}
/// Get an existing recording and all available information from related tables.
pub fn get_recording(conn: &DbConn, id: u32) -> Result<Option<Recording>> {
pub fn get_recording(conn: &DbConn, id: &str) -> Result<Option<Recording>> {
let recording = match get_recording_row(conn, id)? {
Some(row) => Some(get_description_for_recording_row(conn, &row)?),
None => None,
@ -112,13 +112,13 @@ pub fn get_recording(conn: &DbConn, id: u32) -> Result<Option<Recording>> {
}
/// Get all available information on all recordings where a person is performing.
pub fn get_recordings_for_person(conn: &DbConn, person_id: u32) -> Result<Vec<Recording>> {
pub fn get_recordings_for_person(conn: &DbConn, person_id: &str) -> Result<Vec<Recording>> {
let mut recordings: Vec<Recording> = Vec::new();
let rows = recordings::table
.inner_join(performances::table.on(performances::recording.eq(recordings::id)))
.inner_join(persons::table.on(persons::id.nullable().eq(performances::person)))
.filter(persons::id.eq(person_id as i64))
.filter(persons::id.eq(person_id))
.select(recordings::table::all_columns())
.load::<RecordingRow>(conn)?;
@ -130,13 +130,13 @@ pub fn get_recordings_for_person(conn: &DbConn, person_id: u32) -> Result<Vec<Re
}
/// Get all available information on all recordings where an ensemble is performing.
pub fn get_recordings_for_ensemble(conn: &DbConn, ensemble_id: u32) -> Result<Vec<Recording>> {
pub fn get_recordings_for_ensemble(conn: &DbConn, ensemble_id: &str) -> Result<Vec<Recording>> {
let mut recordings: Vec<Recording> = Vec::new();
let rows = recordings::table
.inner_join(performances::table.on(performances::recording.eq(recordings::id)))
.inner_join(ensembles::table.on(ensembles::id.nullable().eq(performances::ensemble)))
.filter(ensembles::id.eq(ensemble_id as i64))
.filter(ensembles::id.eq(ensemble_id))
.select(recordings::table::all_columns())
.load::<RecordingRow>(conn)?;
@ -148,11 +148,11 @@ pub fn get_recordings_for_ensemble(conn: &DbConn, ensemble_id: u32) -> Result<Ve
}
/// Get allavailable information on all recordings of a work.
pub fn get_recordings_for_work(conn: &DbConn, work_id: u32) -> Result<Vec<Recording>> {
pub fn get_recordings_for_work(conn: &DbConn, work_id: &str) -> Result<Vec<Recording>> {
let mut recordings: Vec<Recording> = Vec::new();
let rows = recordings::table
.filter(recordings::work.eq(work_id as i64))
.filter(recordings::work.eq(work_id))
.load::<RecordingRow>(conn)?;
for row in rows {
@ -165,9 +165,9 @@ pub fn get_recordings_for_work(conn: &DbConn, work_id: u32) -> Result<Vec<Record
/// Delete an existing recording. This will fail if there are still references to this
/// recording from other tables that are not directly part of the recording data. Also, the
/// provided user has to be allowed to delete the recording.
pub fn delete_recording(conn: &DbConn, id: u32, user: &User) -> Result<()> {
pub fn delete_recording(conn: &DbConn, id: &str, user: &User) -> Result<()> {
if user.may_delete() {
diesel::delete(recordings::table.filter(recordings::id.eq(id as i64))).execute(conn)?;
diesel::delete(recordings::table.filter(recordings::id.eq(id))).execute(conn)?;
Ok(())
} else {
Err(Error::new(ServerError::Forbidden))
@ -175,9 +175,9 @@ pub fn delete_recording(conn: &DbConn, id: u32, user: &User) -> Result<()> {
}
/// Get an existing recording row.
fn get_recording_row(conn: &DbConn, id: u32) -> Result<Option<RecordingRow>> {
fn get_recording_row(conn: &DbConn, id: &str) -> Result<Option<RecordingRow>> {
Ok(recordings::table
.filter(recordings::id.eq(id as i64))
.filter(recordings::id.eq(id))
.load::<RecordingRow>(conn)?
.into_iter()
.next())
@ -188,43 +188,36 @@ fn get_description_for_recording_row(conn: &DbConn, row: &RecordingRow) -> Resul
let mut performances: Vec<Performance> = Vec::new();
let performance_rows = performances::table
.filter(performances::recording.eq(row.id))
.filter(performances::recording.eq(&row.id))
.load::<PerformanceRow>(conn)?;
for row in performance_rows {
performances.push(Performance {
person: match row.person {
Some(id) => {
let id = id as u32;
Some(get_person(conn, id)?.ok_or(anyhow!("No person with ID: {}", id))?)
Some(get_person(conn, &id)?.ok_or(anyhow!("No person with ID: {}", id))?)
}
None => None,
},
ensemble: match row.ensemble {
Some(id) => {
let id = id as u32;
Some(get_ensemble(conn, id)?.ok_or(anyhow!("No ensemble with ID: {}", id))?)
Some(get_ensemble(conn, &id)?.ok_or(anyhow!("No ensemble with ID: {}", id))?)
}
None => None,
},
role: match row.role {
Some(id) => {
let id = id as u32;
Some(
get_instrument(conn, id)?
.ok_or(anyhow!("No instrument with ID: {}", id))?,
)
}
Some(id) => Some(
get_instrument(conn, &id)?.ok_or(anyhow!("No instrument with ID: {}", id))?,
),
None => None,
},
});
}
let id = row.work as u32;
let work = get_work(conn, id)?.ok_or(anyhow!("No work with ID: {}", id))?;
let work = get_work(conn, &row.work)?.ok_or(anyhow!("No work with ID: {}", &row.work))?;
let recording = Recording {
id: row.id as u32,
id: row.id.clone(),
work,
comment: row.comment.clone(),
performances,

View file

@ -1,6 +1,6 @@
table! {
ensembles (id) {
id -> Int8,
id -> Text,
name -> Text,
created_by -> Text,
}
@ -9,14 +9,14 @@ table! {
table! {
instrumentations (id) {
id -> Int8,
work -> Int8,
instrument -> Int8,
work -> Text,
instrument -> Text,
}
}
table! {
instruments (id) {
id -> Int8,
id -> Text,
name -> Text,
created_by -> Text,
}
@ -25,16 +25,16 @@ table! {
table! {
performances (id) {
id -> Int8,
recording -> Int8,
person -> Nullable<Int8>,
ensemble -> Nullable<Int8>,
role -> Nullable<Int8>,
recording -> Text,
person -> Nullable<Text>,
ensemble -> Nullable<Text>,
role -> Nullable<Text>,
}
}
table! {
persons (id) {
id -> Int8,
id -> Text,
first_name -> Text,
last_name -> Text,
created_by -> Text,
@ -43,8 +43,8 @@ table! {
table! {
recordings (id) {
id -> Int8,
work -> Int8,
id -> Text,
work -> Text,
comment -> Text,
created_by -> Text,
}
@ -64,17 +64,17 @@ table! {
table! {
work_parts (id) {
id -> Int8,
work -> Int8,
work -> Text,
part_index -> Int8,
title -> Text,
composer -> Nullable<Int8>,
composer -> Nullable<Text>,
}
}
table! {
work_sections (id) {
id -> Int8,
work -> Int8,
work -> Text,
title -> Text,
before_index -> Int8,
}
@ -82,8 +82,8 @@ table! {
table! {
works (id) {
id -> Int8,
composer -> Int8,
id -> Text,
composer -> Text,
title -> Text,
created_by -> Text,
}

View file

@ -10,7 +10,7 @@ use std::convert::TryInto;
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct Work {
pub id: u32,
pub id: String,
pub title: String,
pub composer: Person,
pub instruments: Vec<Instrument>,
@ -38,8 +38,8 @@ pub struct WorkSection {
#[derive(Insertable, Queryable, Debug, Clone)]
#[table_name = "works"]
struct WorkRow {
pub id: i64,
pub composer: i64,
pub id: String,
pub composer: String,
pub title: String,
pub created_by: String,
}
@ -49,8 +49,8 @@ struct WorkRow {
#[table_name = "instrumentations"]
struct InstrumentationRow {
pub id: i64,
pub work: i64,
pub instrument: i64,
pub work: String,
pub instrument: String,
}
/// Table data for a work part.
@ -58,10 +58,10 @@ struct InstrumentationRow {
#[table_name = "work_parts"]
struct WorkPartRow {
pub id: i64,
pub work: i64,
pub work: String,
pub part_index: i64,
pub title: String,
pub composer: Option<i64>,
pub composer: Option<String>,
}
/// Table data for a work section.
@ -69,7 +69,7 @@ struct WorkPartRow {
#[derive(Insertable, Queryable, Debug, Clone)]
struct WorkSectionRow {
pub id: i64,
pub work: i64,
pub work: String,
pub title: String,
pub before_index: i64,
}
@ -79,7 +79,7 @@ struct WorkSectionRow {
// TODO: Also add newly created associated items.
pub fn update_work(conn: &DbConn, work: &Work, user: &User) -> Result<()> {
conn.transaction::<(), Error, _>(|| {
let old_row = get_work_row(conn, work.id)?;
let old_row = get_work_row(conn, &work.id)?;
let allowed = match old_row {
Some(row) => user.may_edit(&row.created_by),
@ -87,7 +87,7 @@ pub fn update_work(conn: &DbConn, work: &Work, user: &User) -> Result<()> {
};
if allowed {
let id = work.id as i64;
let id = &work.id;
// This will also delete rows from associated tables.
diesel::delete(works::table)
@ -95,8 +95,8 @@ pub fn update_work(conn: &DbConn, work: &Work, user: &User) -> Result<()> {
.execute(conn)?;
let row = WorkRow {
id,
composer: work.composer.id as i64,
id: id.clone(),
composer: work.composer.id.clone(),
title: work.title.clone(),
created_by: user.username.clone(),
};
@ -109,8 +109,8 @@ pub fn update_work(conn: &DbConn, work: &Work, user: &User) -> Result<()> {
diesel::insert_into(instrumentations::table)
.values(InstrumentationRow {
id: rand::random(),
work: id,
instrument: instrument.id as i64,
work: id.clone(),
instrument: instrument.id.clone(),
})
.execute(conn)?;
}
@ -118,10 +118,10 @@ pub fn update_work(conn: &DbConn, work: &Work, user: &User) -> Result<()> {
for (index, part) in work.parts.iter().enumerate() {
let row = WorkPartRow {
id: rand::random(),
work: id,
work: id.clone(),
part_index: index.try_into()?,
title: part.title.clone(),
composer: part.composer.as_ref().map(|person| person.id as i64),
composer: part.composer.as_ref().map(|person| person.id.clone()),
};
diesel::insert_into(work_parts::table)
@ -132,7 +132,7 @@ pub fn update_work(conn: &DbConn, work: &Work, user: &User) -> Result<()> {
for section in &work.sections {
let row = WorkSectionRow {
id: rand::random(),
work: id,
work: id.clone(),
title: section.title.clone(),
before_index: section.before_index,
};
@ -152,7 +152,7 @@ pub fn update_work(conn: &DbConn, work: &Work, user: &User) -> Result<()> {
}
/// Get an existing work and all available information from related tables.
pub fn get_work(conn: &DbConn, id: u32) -> Result<Option<Work>> {
pub fn get_work(conn: &DbConn, id: &str) -> Result<Option<Work>> {
let work = match get_work_row(conn, id)? {
Some(row) => Some(get_description_for_work_row(conn, &row)?),
None => None,
@ -164,9 +164,9 @@ pub fn get_work(conn: &DbConn, id: u32) -> Result<Option<Work>> {
/// Delete an existing work. This will fail if there are still other tables that relate to
/// this work except for the things that are part of the information on the work itself. Also,
/// this will only succeed, if the provided user is allowed to delete the work.
pub fn delete_work(conn: &DbConn, id: u32, user: &User) -> Result<()> {
pub fn delete_work(conn: &DbConn, id: &str, user: &User) -> Result<()> {
if user.may_delete() {
diesel::delete(works::table.filter(works::id.eq(id as i64))).execute(conn)?;
diesel::delete(works::table.filter(works::id.eq(id))).execute(conn)?;
Ok(())
} else {
Err(Error::new(ServerError::Forbidden))
@ -174,11 +174,11 @@ pub fn delete_work(conn: &DbConn, id: u32, user: &User) -> Result<()> {
}
/// Get all existing works by a composer and related information from other tables.
pub fn get_works(conn: &DbConn, composer_id: u32) -> Result<Vec<Work>> {
pub fn get_works(conn: &DbConn, composer_id: &str) -> Result<Vec<Work>> {
let mut works: Vec<Work> = Vec::new();
let rows = works::table
.filter(works::composer.eq(composer_id as i64))
.filter(works::composer.eq(composer_id))
.load::<WorkRow>(conn)?;
for row in rows {
@ -189,9 +189,9 @@ pub fn get_works(conn: &DbConn, composer_id: u32) -> Result<Vec<Work>> {
}
/// Get an already existing work without related rows from other tables.
fn get_work_row(conn: &DbConn, id: u32) -> Result<Option<WorkRow>> {
fn get_work_row(conn: &DbConn, id: &str) -> Result<Option<WorkRow>> {
Ok(works::table
.filter(works::id.eq(id as i64))
.filter(works::id.eq(id))
.load::<WorkRow>(conn)?
.into_iter()
.next())
@ -202,19 +202,19 @@ fn get_description_for_work_row(conn: &DbConn, row: &WorkRow) -> Result<Work> {
let mut instruments: Vec<Instrument> = Vec::new();
let instrumentations = instrumentations::table
.filter(instrumentations::work.eq(row.id))
.filter(instrumentations::work.eq(&row.id))
.load::<InstrumentationRow>(conn)?;
for instrumentation in instrumentations {
let id = instrumentation.instrument as u32;
let id = instrumentation.instrument.clone();
instruments
.push(get_instrument(conn, id)?.ok_or(anyhow!("No instrument with ID: {}", id))?);
.push(get_instrument(conn, &id)?.ok_or(anyhow!("No instrument with ID: {}", id))?);
}
let mut parts: Vec<WorkPart> = Vec::new();
let part_rows = work_parts::table
.filter(work_parts::work.eq(row.id))
.filter(work_parts::work.eq(&row.id))
.load::<WorkPartRow>(conn)?;
for part_row in part_rows {
@ -222,8 +222,7 @@ fn get_description_for_work_row(conn: &DbConn, row: &WorkRow) -> Result<Work> {
title: part_row.title,
composer: match part_row.composer {
Some(id) => {
let id = id as u32;
Some(get_person(conn, id)?.ok_or(anyhow!("No person with ID: {}", id))?)
Some(get_person(conn, &id)?.ok_or(anyhow!("No person with ID: {}", id))?)
}
None => None,
},
@ -233,7 +232,7 @@ fn get_description_for_work_row(conn: &DbConn, row: &WorkRow) -> Result<Work> {
let mut sections: Vec<WorkSection> = Vec::new();
let section_rows = work_sections::table
.filter(work_sections::work.eq(row.id))
.filter(work_sections::work.eq(&row.id))
.load::<WorkSectionRow>(conn)?;
for section in section_rows {
@ -243,11 +242,11 @@ fn get_description_for_work_row(conn: &DbConn, row: &WorkRow) -> Result<Work> {
});
}
let id = row.composer as u32;
let id = &row.composer;
let composer = get_person(conn, id)?.ok_or(anyhow!("No person with ID: {}", id))?;
Ok(Work {
id: row.id as u32,
id: row.id.clone(),
composer,
title: row.title.clone(),
instruments,

View file

@ -9,11 +9,11 @@ use actix_web_httpauth::extractors::bearer::BearerAuth;
#[get("/persons/{id}")]
pub async fn get_person(
db: web::Data<DbPool>,
id: web::Path<u32>,
id: web::Path<String>,
) -> Result<HttpResponse, ServerError> {
let data = web::block(move || {
let conn = db.into_inner().get()?;
database::get_person(&conn, id.into_inner())?.ok_or(ServerError::NotFound)
database::get_person(&conn, &id.into_inner())?.ok_or(ServerError::NotFound)
})
.await?;
@ -55,13 +55,13 @@ pub async fn get_persons(db: web::Data<DbPool>) -> Result<HttpResponse, ServerEr
pub async fn delete_person(
auth: BearerAuth,
db: web::Data<DbPool>,
id: web::Path<u32>,
id: web::Path<String>,
) -> Result<HttpResponse, ServerError> {
web::block(move || {
let conn = db.into_inner().get()?;
let user = authenticate(&conn, auth.token()).or(Err(ServerError::Unauthorized))?;
database::delete_person(&conn, id.into_inner(), &user)?;
database::delete_person(&conn, &id.into_inner(), &user)?;
Ok(())
})