diff --git a/.envrc b/.envrc index 6d52b20..a2fd1ef 100644 --- a/.envrc +++ b/.envrc @@ -4,6 +4,7 @@ export CMAKE_EXPORT_COMPILE_COMMANDS=1 export CMAKE_BUILD_TYPE=Debug export CXX=g++ export CC=gcc +export DATABASE_URL=sqlite:///home/chris/.local/share/lumina/library-db.sqlite3 # export CXXQT_INCLUDE_PATH=$GUIX_ENVIRONMENT/include # export INCLUDEPATH="/gnu/store/pkjvij1f6rvx42xv2kygicr7fsch41dl-profile/include" use flake . --impure diff --git a/shell.nix b/shell.nix index be990db..575c912 100644 --- a/shell.nix +++ b/shell.nix @@ -60,6 +60,7 @@ mkShell rec { cargo rustfmt rust-analyzer + sqlx-cli corrosion ]; diff --git a/src/rust/songs/song_model.rs b/src/rust/songs/song_model.rs index 945b897..0b7f539 100644 --- a/src/rust/songs/song_model.rs +++ b/src/rust/songs/song_model.rs @@ -289,13 +289,41 @@ impl Default for Song { } } -#[derive(Default, Debug)] -pub struct SongModelRust { +#[derive(Debug)] +pub struct SongModelRust<'a> { count: i32, highest_id: i32, songs: Vec, inner_songs: Vec, - db: SqliteConnection, + db: Option<&'a SqliteConnection>, +} + +impl Default for SongModelRust<'a> { + fn default() -> Self { + Self { + count: 0, + highest_id: 0, + songs: vec![], + inner_songs: vec![], + db: { + let rt = tokio::runtime::Runtime::new().unwrap(); + let mut data = dirs::data_local_dir().unwrap(); + data.push("lumina"); + data.push("library-db.sqlite3"); + let mut db_url = String::from("sqlite://"); + db_url.push_str(data.to_str().unwrap()); + rt.block_on(async { + match SqliteConnection::connect(&db_url).await { + Ok(db) => Some(&db), + Err(e) => { + error!("Error connecting to db: {e}"); + None + } + } + }) + } + } + } } impl song_model::SongModel { @@ -315,102 +343,50 @@ impl song_model::SongModel { let mut db_url = String::from("sqlite://"); db_url.push_str(data.to_str().unwrap()); println!("DB: {:?}", db_url); - static DATABASE_URL: &str = "sqlite:///home/chris/.local/share/lumina/library-db.sqlite3"; let thread = self.qt_thread(); let rt = tokio::runtime::Runtime::new().unwrap(); rt.block_on(async { - match SqliteConnection::connect(&db_url).await { - Ok(db) => { - thread.queue(|s| s.as_mut().rust_mut().db = db); - let result = query_as!(Song, r#"SELECT vorder as "verse_order!", fontSize as "font_size!: i32", backgroundType as "background_type!", horizontalTextAlignment as "horizontal_text_alignment!", verticalTextAlignment as "vertical_text_alignment!", title as "title!", font as "font!", background as "background!", lyrics as "lyrics!", ccli as "ccli!", author as "author!", audio as "audio!", id as "id: i32" from songs"#).fetch_all(&mut self.as_mut().rust_mut().db).await; - match result { - Ok(s) => s.into_iter().for_each(|s| self.as_mut().add_song(s)), - Err(e) => error!("There was an error in converting songs: {e}"), - } - }, - Err(e) => error!("There was an error in the db: {e}"), + let result = query_as!(Song, r#"SELECT vorder as "verse_order!", fontSize as "font_size!: i32", backgroundType as "background_type!", horizontalTextAlignment as "horizontal_text_alignment!", verticalTextAlignment as "vertical_text_alignment!", title as "title!", font as "font!", background as "background!", lyrics as "lyrics!", ccli as "ccli!", author as "author!", audio as "audio!", id as "id: i32" from songs"#).fetch_all(&mut self.as_mut().rust_mut().db.unwrap()).await; + match result { + Ok(s) => s.into_iter().for_each(|s| self.as_mut().add_song(s)), + Err(e) => error!("There was an error in converting songs: {e}"), } }); - - // run_migrations(db); - // let results = songs - // .load::(db) - // .expect("NO TABLE?????????????"); - // self.as_mut().rust_mut().highest_id = 0; - - // println!("SHOWING SONGS"); - // println!("--------------"); - // for song in results { - // println!("{}", song.title); - // println!("{}", song.id); - // println!( - // "{}", - // song.background.clone().unwrap_or_default() - // ); - // println!("--------------"); - // if self.as_mut().highest_id < song.id { - // self.as_mut().rust_mut().highest_id = song.id; - // } - - // let song = Song { - // id: song.id, - // title: song.title, - // lyrics: song.lyrics.unwrap_or_default(), - // author: song.author.unwrap_or_default(), - // ccli: song.ccli.unwrap_or_default(), - // audio: song.audio.unwrap_or_default(), - // verse_order: song.verse_order.unwrap_or_default(), - // background: song.background.unwrap_or_default(), - // background_type: song - // .background_type - // .unwrap_or_default(), - // horizontal_text_alignment: song - // .horizontal_text_alignment - // .unwrap_or_default(), - // vertical_text_alignment: song - // .vertical_text_alignment - // .unwrap_or_default(), - // font: song.font.unwrap_or_default(), - // font_size: song.font_size.unwrap_or_default(), - // }; - - // self.as_mut().add_song(song); - // } - // println!("--------------------------------------"); - // println!("{:?}", self.as_mut().songs); - // println!("--------------------------------------"); } pub fn remove_item(mut self: Pin<&mut Self>, index: i32) -> bool { if index < 0 || (index as usize) >= self.songs.len() { return false; } - let db = &mut self.as_mut().rust_mut().db; - let song_id = self.songs.get(index as usize).unwrap().id; + let song_id = self.as_mut().rust_mut().songs.get(index as usize).unwrap().id; + let thread = self.qt_thread(); + let db = &mut self.as_mut().rust_mut().db.unwrap(); let rt = tokio::runtime::Runtime::new().unwrap(); rt.block_on(async { let result = query!("delete from songs where id = ?", song_id).fetch_optional(db).await; match result { Ok(_) => { - unsafe { - self.as_mut().begin_remove_rows( - &QModelIndex::default(), - index, - index, - ); - self.as_mut() - .rust_mut() - .songs - .remove(index as usize); - self.as_mut() - .rust_mut() - .inner_songs - .remove(index as usize); - self.as_mut().end_remove_rows(); - } + thread.queue(move |mut song_model| { + unsafe { + song_model.as_mut().begin_remove_rows( + &QModelIndex::default(), + index, + index, + ); + song_model.as_mut() + .rust_mut() + .songs + .remove(index as usize); + song_model.as_mut() + .rust_mut() + .inner_songs + .remove(index as usize); + song_model.as_mut().end_remove_rows(); + } + }); debug!("removed-item-at-index: {:?}", song_id); true }, @@ -430,7 +406,7 @@ impl song_model::SongModel { pub fn new_song(mut self: Pin<&mut Self>) -> bool { let song_id = self.rust().highest_id + 1; let song_title = String::from("title"); - let db = &mut self.as_mut().get_db(); + let rt = tokio::runtime::Runtime::new().unwrap(); let song = Song { id: song_id, @@ -438,38 +414,22 @@ impl song_model::SongModel { ..Default::default() }; - let result = insert_into(songs) - .values(( - id.eq(&song_id), - title.eq(&song_title), - lyrics.eq(&song.lyrics), - author.eq(&song.author), - ccli.eq(&song.ccli), - audio.eq(&song.audio), - verse_order.eq(&song.verse_order), - background.eq(&song.background), - background_type.eq(&song.background_type), - horizontal_text_alignment - .eq(&song.horizontal_text_alignment), - vertical_text_alignment - .eq(&song.vertical_text_alignment), - font.eq(&song.font), - font_size.eq(&song.font_size), - )) - .execute(db); - println!("{:?}", result); - - match result { - Ok(_i) => { - self.as_mut().add_song(song); - println!("{:?}", self.as_mut().songs); - true + rt.block_on(async { + let result = query!(r#"INSERT into songs (vorder, fontSize, backgroundType, horizontalTextAlignment, verticalTextAlignment, title, font, background, lyrics, ccli, author, audio, id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"#, song.verse_order, song.font_size, song.background_type, song.horizontal_text_alignment, song.vertical_text_alignment, song.title, song.font, song.background, song.lyrics, song.ccli, song.author, song.audio, song.id).fetch_all(&mut self.as_mut().rust_mut().db.unwrap()).await; + + match result { + Ok(_i) => { + self.as_mut().add_song(song); + println!("{:?}", self.as_mut().songs); + true + } + Err(_e) => { + println!("Cannot connect to database"); + false + } } - Err(_e) => { - println!("Cannot connect to database"); - false - } - } + }); + true } fn add_song(mut self: Pin<&mut Self>, song: self::Song) { @@ -520,30 +480,37 @@ impl song_model::SongModel { ) -> bool { let (index, model_index, vector_roles) = self.as_mut().get_indices(song_id, SongRoles::Title); - let db = &mut self.as_mut().get_db(); - let result = update(songs.filter(id.eq(song_id))) - .set(title.eq(updated_title.to_string())) - .execute(db); - match result { - Ok(_i) => { - debug!(_i, index); - if let Some(song) = - self.as_mut().rust_mut().songs.get_mut(index) - { - debug!(?song, title = updated_title.to_string()); - song.title = updated_title.to_string(); - self.as_mut().data_changed( - &model_index, - &model_index, - &vector_roles, - ); - true - } else { - false + let rt = tokio::runtime::Runtime::new().unwrap(); + let db_string = updated_title.clone().to_string(); + rt.block_on(async { + let result = query!("UPDATE songs SET title = ?", db_string) + .execute(&mut self.as_mut().rust_mut().db.unwrap()) + .await; + + match result { + Ok(_i) => { + if let Some(song) = + self.as_mut().rust_mut().songs.get_mut(index) + { + debug!(?song, title = updated_title.to_string()); + song.title = updated_title.to_string(); + self.as_mut().data_changed( + &model_index, + &model_index, + &vector_roles, + ); + true + } else { + false + } } + Err(e) => { + error!("There was an error updating title in db: {e}"); + false + }, } - Err(_e) => false, - } + }); + true } pub fn update_lyrics( @@ -553,32 +520,40 @@ impl song_model::SongModel { ) -> bool { let (index, model_index, vector_roles) = self.as_mut().get_indices(song_id, SongRoles::Lyrics); - let db = &mut self.as_mut().get_db(); - let result = update(songs.filter(id.eq(song_id))) - .set(lyrics.eq(updated_lyrics.to_string())) - .execute(db); - match result { - Ok(_i) => { - if let Some(song) = - self.as_mut().rust_mut().songs.get_mut(index) - { - debug!( - ?song, - lyrics = updated_lyrics.to_string() - ); - song.lyrics = updated_lyrics.to_string(); - self.as_mut().data_changed( - &model_index, - &model_index, - &vector_roles, - ); - true - } else { - false + let db_string = updated_lyrics.clone().to_string(); + let rt = tokio::runtime::Runtime::new().unwrap(); + rt.block_on(async { + let result = query!("UPDATE songs SET lyrics = ?", db_string) + .execute(&mut self.as_mut().rust_mut().db.unwrap()) + .await; + + match result { + Ok(_i) => { + if let Some(song) = + self.as_mut().rust_mut().songs.get_mut(index) + { + debug!( + ?song, + lyrics = updated_lyrics.to_string() + ); + song.lyrics = updated_lyrics.to_string(); + self.as_mut().data_changed( + &model_index, + &model_index, + &vector_roles, + ); + true + } else { + false + } } + Err(e) => { + error!("There was an error updating lyrics in db: {e}"); + false + }, } - Err(_e) => false, - } + }); + true } pub fn update_author( @@ -589,32 +564,40 @@ impl song_model::SongModel { let (index, model_index, vector_roles) = self.as_mut().get_indices(song_id, SongRoles::Author); - let db = &mut self.as_mut().get_db(); - let result = update(songs.filter(id.eq(song_id))) - .set(author.eq(updated_author.to_string())) - .execute(db); - match result { - Ok(_i) => { - if let Some(song) = - self.as_mut().rust_mut().songs.get_mut(index) - { - debug!( - ?song, - author = updated_author.to_string() - ); - song.author = updated_author.to_string(); - self.as_mut().data_changed( - &model_index, - &model_index, - &vector_roles, - ); - true - } else { - false + let db_string = updated_author.clone().to_string(); + let rt = tokio::runtime::Runtime::new().unwrap(); + rt.block_on(async { + let result = query!("UPDATE songs SET author = ?", db_string) + .execute(&mut self.as_mut().rust_mut().db.unwrap()) + .await; + + match result { + Ok(_i) => { + if let Some(song) = + self.as_mut().rust_mut().songs.get_mut(index) + { + debug!( + ?song, + author = updated_author.to_string() + ); + song.author = updated_author.to_string(); + self.as_mut().data_changed( + &model_index, + &model_index, + &vector_roles, + ); + true + } else { + false + } } + Err(e) => { + error!("There was an error updating author in db: {e}"); + false + }, } - Err(_e) => false, - } + }); + true } pub fn update_audio( @@ -625,32 +608,40 @@ impl song_model::SongModel { let (index, model_index, vector_roles) = self.as_mut().get_indices(song_id, SongRoles::Audio); - debug!(?updated_audio); - let db = &mut self.as_mut().get_db(); - let result = update(songs.filter(id.eq(song_id))) - .set(audio.eq(updated_audio.to_string())) - .execute(db); - debug!(?result); - match result { - Ok(_i) => { - if let Some(song) = - self.as_mut().rust_mut().songs.get_mut(index) - { - debug!(?song, audio = updated_audio.to_string()); - song.audio = updated_audio.to_string(); - debug!(?song); - self.as_mut().data_changed( - &model_index, - &model_index, - &vector_roles, - ); - true - } else { - false + let db_string = updated_audio.clone().to_string(); + let rt = tokio::runtime::Runtime::new().unwrap(); + rt.block_on(async { + let result = query!("UPDATE songs SET audio = ?", db_string) + .execute(&mut self.as_mut().rust_mut().db.unwrap()) + .await; + + match result { + Ok(_i) => { + if let Some(song) = + self.as_mut().rust_mut().songs.get_mut(index) + { + debug!( + ?song, + audio = updated_audio.to_string() + ); + song.audio = updated_audio.to_string(); + self.as_mut().data_changed( + &model_index, + &model_index, + &vector_roles, + ); + true + } else { + false + } } + Err(e) => { + error!("There was an error updating audio in db: {e}"); + false + }, } - Err(_e) => false, - } + }); + true } pub fn update_ccli( @@ -661,29 +652,40 @@ impl song_model::SongModel { let (index, model_index, vector_roles) = self.as_mut().get_indices(song_id, SongRoles::Ccli); - let db = &mut self.as_mut().get_db(); - let result = update(songs.filter(id.eq(song_id))) - .set(ccli.eq(updated_ccli.to_string())) - .execute(db); - match result { - Ok(_i) => { - if let Some(song) = - self.as_mut().rust_mut().songs.get_mut(index) - { - debug!(?song, ccli = updated_ccli.to_string()); - song.ccli = updated_ccli.to_string(); - self.as_mut().data_changed( - &model_index, - &model_index, - &vector_roles, - ); - true - } else { - false + let db_string = updated_ccli.clone().to_string(); + let rt = tokio::runtime::Runtime::new().unwrap(); + rt.block_on(async { + let result = query!("UPDATE songs SET ccli = ?", db_string) + .execute(&mut self.as_mut().rust_mut().db.unwrap()) + .await; + + match result { + Ok(_i) => { + if let Some(song) = + self.as_mut().rust_mut().songs.get_mut(index) + { + debug!( + ?song, + ccli = updated_ccli.to_string() + ); + song.ccli = updated_ccli.to_string(); + self.as_mut().data_changed( + &model_index, + &model_index, + &vector_roles, + ); + true + } else { + false + } } + Err(e) => { + error!("There was an error updating ccli in db: {e}"); + false + }, } - Err(_e) => false, - } + }); + true } pub fn update_verse_order( @@ -694,33 +696,40 @@ impl song_model::SongModel { let (index, model_index, vector_roles) = self.as_mut().get_indices(song_id, SongRoles::VerseOrder); - let db = &mut self.as_mut().get_db(); - let result = update(songs.filter(id.eq(song_id))) - .set(verse_order.eq(updated_verse_order.to_string())) - .execute(db); - match result { - Ok(_i) => { - if let Some(song) = - self.as_mut().rust_mut().songs.get_mut(index) - { - debug!( - ?song, - verse_order = updated_verse_order.to_string() - ); - song.verse_order = - updated_verse_order.to_string(); - self.as_mut().data_changed( - &model_index, - &model_index, - &vector_roles, - ); - true - } else { - false + let db_string = updated_verse_order.clone().to_string(); + let rt = tokio::runtime::Runtime::new().unwrap(); + rt.block_on(async { + let result = query!("UPDATE songs SET vorder = ?", db_string) + .execute(&mut self.as_mut().rust_mut().db.unwrap()) + .await; + + match result { + Ok(_i) => { + if let Some(song) = + self.as_mut().rust_mut().songs.get_mut(index) + { + debug!( + ?song, + verse_order = updated_verse_order.to_string() + ); + song.verse_order = updated_verse_order.to_string(); + self.as_mut().data_changed( + &model_index, + &model_index, + &vector_roles, + ); + true + } else { + false + } } + Err(e) => { + error!("There was an error updating verse_order in db: {e}"); + false + }, } - Err(_e) => false, - } + }); + true } pub fn update_background( @@ -731,34 +740,40 @@ impl song_model::SongModel { let (index, model_index, vector_roles) = self.as_mut().get_indices(song_id, SongRoles::Background); - let db = &mut self.as_mut().get_db(); - let result = update(songs.filter(id.eq(song_id))) - .set(background.eq(updated_background.to_string())) - .execute(db); - match result { - Ok(_i) => { - if let Some(song) = - self.as_mut().rust_mut().songs.get_mut(index) - { - song.background = updated_background.to_string(); - debug!( - background = ?updated_background, - model_index = ?model_index, - roles = vector_roles.get(0) - ); - self.as_mut().data_changed( - &model_index, - &model_index, - &vector_roles, - ); - self.as_mut().background_changed(); - true - } else { - false + let db_string = updated_background.clone().to_string(); + let rt = tokio::runtime::Runtime::new().unwrap(); + rt.block_on(async { + let result = query!("UPDATE songs SET background = ?", db_string) + .execute(&mut self.as_mut().rust_mut().db.unwrap()) + .await; + + match result { + Ok(_i) => { + if let Some(song) = + self.as_mut().rust_mut().songs.get_mut(index) + { + debug!( + ?song, + background = updated_background.to_string() + ); + song.background = updated_background.to_string(); + self.as_mut().data_changed( + &model_index, + &model_index, + &vector_roles, + ); + true + } else { + false + } } + Err(e) => { + error!("There was an error updating background in db: {e}"); + false + }, } - Err(_e) => false, - } + }); + true } pub fn update_background_type( @@ -770,37 +785,40 @@ impl song_model::SongModel { .as_mut() .get_indices(song_id, SongRoles::BackgroundType); - let db = &mut self.as_mut().get_db(); - let result = update(songs.filter(id.eq(song_id))) - .set( - background_type - .eq(updated_background_type.to_string()), - ) - .execute(db); - match result { - Ok(_i) => { - if let Some(song) = - self.as_mut().rust_mut().songs.get_mut(index) - { - debug!( - ?song, - background_type = - updated_background_type.to_string() - ); - song.background_type = - updated_background_type.to_string(); - self.as_mut().data_changed( - &model_index, - &model_index, - &vector_roles, - ); - true - } else { - false + let db_string = updated_background_type.clone().to_string(); + let rt = tokio::runtime::Runtime::new().unwrap(); + rt.block_on(async { + let result = query!("UPDATE songs SET backgroundType = ?", db_string) + .execute(&mut self.as_mut().rust_mut().db.unwrap()) + .await; + + match result { + Ok(_i) => { + if let Some(song) = + self.as_mut().rust_mut().songs.get_mut(index) + { + debug!( + ?song, + background_type = updated_background_type.to_string() + ); + song.background_type = updated_background_type.to_string(); + self.as_mut().data_changed( + &model_index, + &model_index, + &vector_roles, + ); + true + } else { + false + } } + Err(e) => { + error!("There was an error updating background_type in db: {e}"); + false + }, } - Err(_e) => false, - } + }); + true } pub fn update_horizontal_text_alignment( @@ -812,38 +830,40 @@ impl song_model::SongModel { .as_mut() .get_indices(song_id, SongRoles::HorizontalTextAlignment); - let db = &mut self.as_mut().get_db(); - let result = - update(songs.filter(id.eq(song_id))) - .set(horizontal_text_alignment.eq( - updated_horizontal_text_alignment.to_string(), - )) - .execute(db); - match result { - Ok(_i) => { - if let Some(song) = - self.as_mut().rust_mut().songs.get_mut(index) - { - debug!( - ?song, - horizontal = - updated_horizontal_text_alignment - .to_string() - ); - song.horizontal_text_alignment = - updated_horizontal_text_alignment.to_string(); - self.as_mut().data_changed( - &model_index, - &model_index, - &vector_roles, - ); - true - } else { - false + let db_string = updated_horizontal_text_alignment.clone().to_string(); + let rt = tokio::runtime::Runtime::new().unwrap(); + rt.block_on(async { + let result = query!("UPDATE songs SET horizontalTextAlignment = ?", db_string) + .execute(&mut self.as_mut().rust_mut().db.unwrap()) + .await; + + match result { + Ok(_i) => { + if let Some(song) = + self.as_mut().rust_mut().songs.get_mut(index) + { + debug!( + ?song, + horizontal_text_alignment = updated_horizontal_text_alignment.to_string() + ); + song.horizontal_text_alignment = updated_horizontal_text_alignment.to_string(); + self.as_mut().data_changed( + &model_index, + &model_index, + &vector_roles, + ); + true + } else { + false + } } + Err(e) => { + error!("There was an error updating horizontal_text_alignment in db: {e}"); + false + }, } - Err(_e) => false, - } + }); + true } pub fn update_vertical_text_alignment( @@ -855,37 +875,40 @@ impl song_model::SongModel { .as_mut() .get_indices(song_id, SongRoles::VerticalTextAlignment); - let db = &mut self.as_mut().get_db(); - let result = update(songs.filter(id.eq(song_id))) - .set( - vertical_text_alignment - .eq(updated_vertical_text_alignment.to_string()), - ) - .execute(db); - match result { - Ok(_i) => { - if let Some(song) = - self.as_mut().rust_mut().songs.get_mut(index) - { - debug!( - ?song, - vertical = updated_vertical_text_alignment - .to_string() - ); - song.vertical_text_alignment = - updated_vertical_text_alignment.to_string(); - self.as_mut().data_changed( - &model_index, - &model_index, - &vector_roles, - ); - true - } else { - false + let db_string = updated_vertical_text_alignment.clone().to_string(); + let rt = tokio::runtime::Runtime::new().unwrap(); + rt.block_on(async { + let result = query!("UPDATE songs SET verticalTextAlignment = ?", db_string) + .execute(&mut self.as_mut().rust_mut().db.unwrap()) + .await; + + match result { + Ok(_i) => { + if let Some(song) = + self.as_mut().rust_mut().songs.get_mut(index) + { + debug!( + ?song, + vertical_text_alignment = updated_vertical_text_alignment.to_string() + ); + song.vertical_text_alignment = updated_vertical_text_alignment.to_string(); + self.as_mut().data_changed( + &model_index, + &model_index, + &vector_roles, + ); + true + } else { + false + } } + Err(e) => { + error!("There was an error updating vertical_text_alignment in db: {e}"); + false + }, } - Err(_e) => false, - } + }); + true } pub fn update_font( @@ -896,29 +919,40 @@ impl song_model::SongModel { let (index, model_index, vector_roles) = self.as_mut().get_indices(song_id, SongRoles::Font); - let db = &mut self.as_mut().get_db(); - let result = update(songs.filter(id.eq(song_id))) - .set(font.eq(updated_font.to_string())) - .execute(db); - match result { - Ok(_i) => { - if let Some(song) = - self.as_mut().rust_mut().songs.get_mut(index) - { - song.font = updated_font.to_string(); - debug!(?song, font = updated_font.to_string()); - self.as_mut().data_changed( - &model_index, - &model_index, - &vector_roles, - ); - true - } else { - false + let db_string = updated_font.clone().to_string(); + let rt = tokio::runtime::Runtime::new().unwrap(); + rt.block_on(async { + let result = query!("UPDATE songs SET font = ?", db_string) + .execute(&mut self.as_mut().rust_mut().db.unwrap()) + .await; + + match result { + Ok(_i) => { + if let Some(song) = + self.as_mut().rust_mut().songs.get_mut(index) + { + debug!( + ?song, + font = updated_font.to_string() + ); + song.font = updated_font.to_string(); + self.as_mut().data_changed( + &model_index, + &model_index, + &vector_roles, + ); + true + } else { + false + } } + Err(e) => { + error!("There was an error updating font in db: {e}"); + false + }, } - Err(_e) => false, - } + }); + true } pub fn update_font_size( @@ -929,32 +963,40 @@ impl song_model::SongModel { let (index, model_index, vector_roles) = self.as_mut().get_indices(song_id, SongRoles::FontSize); - let db = &mut self.as_mut().get_db(); - let result = update(songs.filter(id.eq(song_id))) - .set(font_size.eq(updated_font_size)) - .execute(db); - match result { - Ok(_i) => { - if let Some(song) = - self.as_mut().rust_mut().songs.get_mut(index) - { - debug!( - ?song, - font_size = updated_font_size.to_string() - ); - song.font_size = updated_font_size; - self.as_mut().data_changed( - &model_index, - &model_index, - &vector_roles, - ); - true - } else { - false + let db_string = updated_font_size.clone().to_string(); + let rt = tokio::runtime::Runtime::new().unwrap(); + rt.block_on(async { + let result = query!("UPDATE songs SET fontSize = ?", db_string) + .execute(&mut self.as_mut().rust_mut().db.unwrap()) + .await; + + match result { + Ok(_i) => { + if let Some(song) = + self.as_mut().rust_mut().songs.get_mut(index) + { + debug!( + ?song, + font_size = updated_font_size + ); + song.font_size = updated_font_size; + self.as_mut().data_changed( + &model_index, + &model_index, + &vector_roles, + ); + true + } else { + false + } } + Err(e) => { + error!("There was an error updating font_size in db: {e}"); + false + }, } - Err(_e) => false, - } + }); + true } pub fn get_item(&self, index: i32) -> QMap_QString_QVariant {