diff --git a/musicus_server/migrations/2020-11-09-153819_initial_schema/up.sql b/musicus_server/migrations/2020-11-09-153819_initial_schema/up.sql index cef642f..3abb11b 100644 --- a/musicus_server/migrations/2020-11-09-153819_initial_schema/up.sql +++ b/musicus_server/migrations/2020-11-09-153819_initial_schema/up.sql @@ -8,63 +8,63 @@ CREATE TABLE users ( ); CREATE TABLE persons ( - id BIGINT NOT NULL PRIMARY KEY, + id TEXT NOT NULL PRIMARY KEY, first_name TEXT NOT NULL, last_name TEXT NOT NULL, created_by TEXT NOT NULL REFERENCES users(username) ); CREATE TABLE instruments ( - id BIGINT NOT NULL PRIMARY KEY, + id TEXT NOT NULL PRIMARY KEY, name TEXT NOT NULL, created_by TEXT NOT NULL REFERENCES users(username) ); CREATE TABLE works ( - id BIGINT NOT NULL PRIMARY KEY, - composer BIGINT NOT NULL REFERENCES persons(id), + id TEXT NOT NULL PRIMARY KEY, + composer TEXT NOT NULL REFERENCES persons(id), title TEXT NOT NULL, created_by TEXT NOT NULL REFERENCES users(username) ); CREATE TABLE instrumentations ( id BIGINT NOT NULL PRIMARY KEY, - work BIGINT NOT NULL REFERENCES works(id) ON DELETE CASCADE, - instrument BIGINT NOT NULL REFERENCES instruments(id) ON DELETE CASCADE + work TEXT NOT NULL REFERENCES works(id) ON DELETE CASCADE, + instrument TEXT NOT NULL REFERENCES instruments(id) ON DELETE CASCADE ); CREATE TABLE work_parts ( id BIGINT NOT NULL PRIMARY KEY, - work BIGINT NOT NULL REFERENCES works(id) ON DELETE CASCADE, + work TEXT NOT NULL REFERENCES works(id) ON DELETE CASCADE, part_index BIGINT NOT NULL, title TEXT NOT NULL, - composer BIGINT REFERENCES persons(id) + composer TEXT REFERENCES persons(id) ); CREATE TABLE work_sections ( id BIGINT NOT NULL PRIMARY KEY, - work BIGINT NOT NULL REFERENCES works(id) ON DELETE CASCADE, + work TEXT NOT NULL REFERENCES works(id) ON DELETE CASCADE, title TEXT NOT NULL, before_index BIGINT NOT NULL ); CREATE TABLE ensembles ( - id BIGINT NOT NULL PRIMARY KEY, + id TEXT NOT NULL PRIMARY KEY, name TEXT NOT NULL, created_by TEXT NOT NULL REFERENCES users(username) ); CREATE TABLE recordings ( - id BIGINT NOT NULL PRIMARY KEY, - work BIGINT NOT NULL REFERENCES works(id), + id TEXT NOT NULL PRIMARY KEY, + work TEXT NOT NULL REFERENCES works(id), comment TEXT NOT NULL, created_by TEXT NOT NULL REFERENCES users(username) ); CREATE TABLE performances ( id BIGINT NOT NULL PRIMARY KEY, - recording BIGINT NOT NULL REFERENCES recordings(id) ON DELETE CASCADE, - person BIGINT REFERENCES persons(id), - ensemble BIGINT REFERENCES ensembles(id), - role BIGINT REFERENCES instruments(id) + recording TEXT NOT NULL REFERENCES recordings(id) ON DELETE CASCADE, + person TEXT REFERENCES persons(id), + ensemble TEXT REFERENCES ensembles(id), + role TEXT REFERENCES instruments(id) ); \ No newline at end of file diff --git a/musicus_server/src/database/ensembles.rs b/musicus_server/src/database/ensembles.rs index 9330259..32eb94c 100644 --- a/musicus_server/src/database/ensembles.rs +++ b/musicus_server/src/database/ensembles.rs @@ -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 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 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> { +pub fn get_ensemble(conn: &DbConn, id: &str) -> Result> { 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> { } /// 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> { } /// Get a ensemble row if it exists. -fn get_ensemble_row(conn: &DbConn, id: u32) -> Result> { +fn get_ensemble_row(conn: &DbConn, id: &str) -> Result> { let row = ensembles::table - .filter(ensembles::id.eq(id as i64)) + .filter(ensembles::id.eq(id)) .load::(conn)? .into_iter() .next(); diff --git a/musicus_server/src/database/instruments.rs b/musicus_server/src/database/instruments.rs index a279a4d..3b4a7fe 100644 --- a/musicus_server/src/database/instruments.rs +++ b/musicus_server/src/database/instruments.rs @@ -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 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 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> { +pub fn get_instrument(conn: &DbConn, id: &str) -> Result> { 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> { } /// 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> { } /// Get a instrument row if it exists. -fn get_instrument_row(conn: &DbConn, id: u32) -> Result> { +fn get_instrument_row(conn: &DbConn, id: &str) -> Result> { let row = instruments::table - .filter(instruments::id.eq(id as i64)) + .filter(instruments::id.eq(id)) .load::(conn)? .into_iter() .next(); diff --git a/musicus_server/src/database/persons.rs b/musicus_server/src/database/persons.rs index 001f59c..df2c704 100644 --- a/musicus_server/src/database/persons.rs +++ b/musicus_server/src/database/persons.rs @@ -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 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 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> { +pub fn get_person(conn: &DbConn, id: &str) -> Result> { 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> { } /// 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> { } /// Get a person row if it exists. -fn get_person_row(conn: &DbConn, id: u32) -> Result> { +fn get_person_row(conn: &DbConn, id: &str) -> Result> { let row = persons::table - .filter(persons::id.eq(id as i64)) + .filter(persons::id.eq(id)) .load::(conn)? .into_iter() .next(); diff --git a/musicus_server/src/database/recordings.rs b/musicus_server/src/database/recordings.rs index dde7233..a756cee 100644 --- a/musicus_server/src/database/recordings.rs +++ b/musicus_server/src/database/recordings.rs @@ -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, @@ -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, - pub ensemble: Option, - pub role: Option, + pub recording: String, + pub person: Option, + pub ensemble: Option, + pub role: Option, } /// 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> { +pub fn get_recording(conn: &DbConn, id: &str) -> Result> { 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> { } /// Get all available information on all recordings where a person is performing. -pub fn get_recordings_for_person(conn: &DbConn, person_id: u32) -> Result> { +pub fn get_recordings_for_person(conn: &DbConn, person_id: &str) -> Result> { let mut recordings: Vec = 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::(conn)?; @@ -130,13 +130,13 @@ pub fn get_recordings_for_person(conn: &DbConn, person_id: u32) -> Result Result> { +pub fn get_recordings_for_ensemble(conn: &DbConn, ensemble_id: &str) -> Result> { let mut recordings: Vec = 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::(conn)?; @@ -148,11 +148,11 @@ pub fn get_recordings_for_ensemble(conn: &DbConn, ensemble_id: u32) -> Result Result> { +pub fn get_recordings_for_work(conn: &DbConn, work_id: &str) -> Result> { let mut recordings: Vec = Vec::new(); let rows = recordings::table - .filter(recordings::work.eq(work_id as i64)) + .filter(recordings::work.eq(work_id)) .load::(conn)?; for row in rows { @@ -165,9 +165,9 @@ pub fn get_recordings_for_work(conn: &DbConn, work_id: u32) -> Result 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> { +fn get_recording_row(conn: &DbConn, id: &str) -> Result> { Ok(recordings::table - .filter(recordings::id.eq(id as i64)) + .filter(recordings::id.eq(id)) .load::(conn)? .into_iter() .next()) @@ -188,43 +188,36 @@ fn get_description_for_recording_row(conn: &DbConn, row: &RecordingRow) -> Resul let mut performances: Vec = Vec::new(); let performance_rows = performances::table - .filter(performances::recording.eq(row.id)) + .filter(performances::recording.eq(&row.id)) .load::(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, diff --git a/musicus_server/src/database/schema.rs b/musicus_server/src/database/schema.rs index e9d74e7..87846e4 100644 --- a/musicus_server/src/database/schema.rs +++ b/musicus_server/src/database/schema.rs @@ -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, - ensemble -> Nullable, - role -> Nullable, + recording -> Text, + person -> Nullable, + ensemble -> Nullable, + role -> Nullable, } } 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, + composer -> Nullable, } } 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, } diff --git a/musicus_server/src/database/works.rs b/musicus_server/src/database/works.rs index cacc866..372330b 100644 --- a/musicus_server/src/database/works.rs +++ b/musicus_server/src/database/works.rs @@ -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, @@ -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, + pub composer: Option, } /// 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> { +pub fn get_work(conn: &DbConn, id: &str) -> Result> { 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> { /// 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> { +pub fn get_works(conn: &DbConn, composer_id: &str) -> Result> { let mut works: Vec = Vec::new(); let rows = works::table - .filter(works::composer.eq(composer_id as i64)) + .filter(works::composer.eq(composer_id)) .load::(conn)?; for row in rows { @@ -189,9 +189,9 @@ pub fn get_works(conn: &DbConn, composer_id: u32) -> Result> { } /// Get an already existing work without related rows from other tables. -fn get_work_row(conn: &DbConn, id: u32) -> Result> { +fn get_work_row(conn: &DbConn, id: &str) -> Result> { Ok(works::table - .filter(works::id.eq(id as i64)) + .filter(works::id.eq(id)) .load::(conn)? .into_iter() .next()) @@ -202,19 +202,19 @@ fn get_description_for_work_row(conn: &DbConn, row: &WorkRow) -> Result { let mut instruments: Vec = Vec::new(); let instrumentations = instrumentations::table - .filter(instrumentations::work.eq(row.id)) + .filter(instrumentations::work.eq(&row.id)) .load::(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 = Vec::new(); let part_rows = work_parts::table - .filter(work_parts::work.eq(row.id)) + .filter(work_parts::work.eq(&row.id)) .load::(conn)?; for part_row in part_rows { @@ -222,8 +222,7 @@ fn get_description_for_work_row(conn: &DbConn, row: &WorkRow) -> Result { 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 { let mut sections: Vec = Vec::new(); let section_rows = work_sections::table - .filter(work_sections::work.eq(row.id)) + .filter(work_sections::work.eq(&row.id)) .load::(conn)?; for section in section_rows { @@ -243,11 +242,11 @@ fn get_description_for_work_row(conn: &DbConn, row: &WorkRow) -> Result { }); } - 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, diff --git a/musicus_server/src/routes/persons.rs b/musicus_server/src/routes/persons.rs index 1c98d77..977e3b3 100644 --- a/musicus_server/src/routes/persons.rs +++ b/musicus_server/src/routes/persons.rs @@ -9,11 +9,11 @@ use actix_web_httpauth::extractors::bearer::BearerAuth; #[get("/persons/{id}")] pub async fn get_person( db: web::Data, - id: web::Path, + id: web::Path, ) -> Result { 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) -> Result, - id: web::Path, + id: web::Path, ) -> Result { 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(()) })