From 82bed2c9b808973d2abcedc8e7c6e19bf9a34906 Mon Sep 17 00:00:00 2001 From: Chris Cochrun Date: Tue, 7 Apr 2026 09:45:46 -0500 Subject: [PATCH] more functions for db actions --- src/core/songs.rs | 31 ++++++++++++------- src/ui/library.rs | 78 +++++++++++++++++++++++++++++------------------ 2 files changed, 69 insertions(+), 40 deletions(-) diff --git a/src/core/songs.rs b/src/core/songs.rs index b474ba3..8237708 100644 --- a/src/core/songs.rs +++ b/src/core/songs.rs @@ -960,17 +960,25 @@ impl Model { } pub async fn remove_from_db( - db: PoolConnection, + db: Arc, + mut songs: Vec, id: i32, -) -> Result<()> { - query!("DELETE FROM songs WHERE id = $1", id) - .execute(&mut db.detach()) - .await - .into_diagnostic() - .map(|_| ()) +) -> Result> { + if let Some(index) = + songs.iter().position(|current_song| current_song.id == id) + { + songs.remove(index); + query!("DELETE FROM songs WHERE id = $1", id) + .execute(&*db) + .await + .into_diagnostic() + .map(|_| songs) + } else { + Err(miette!("Couldn't find song in model")) + } } -pub async fn add_song_to_db( +pub async fn add_to_db( mut songs: Vec, db: Arc, ) -> Result> { @@ -1021,7 +1029,8 @@ pub async fn add_song_to_db( pub async fn update_song_in_db( item: Song, - db: PoolConnection, + songs: Vec, + db: Arc, ) -> Result<()> { // self.update_item(item.clone(), index)?; @@ -1110,7 +1119,7 @@ pub async fn update_song_in_db( style, weight ) - .execute(&mut db.detach()) + .execute(&*db) .await .into_diagnostic()?; @@ -1540,7 +1549,7 @@ You saved my soul" async fn fill_db(db: &SqlitePool) -> Result<()> { for _ in 0..20 { let conn = db.acquire().await.into_diagnostic()?; - let db_song = add_song_to_db(conn).await?; + let db_song = add_to_db(conn).await?; let mut song = test_song(); song.id = db_song.id; let conn = db.acquire().await.into_diagnostic()?; diff --git a/src/ui/library.rs b/src/ui/library.rs index bb66445..399822d 100644 --- a/src/ui/library.rs +++ b/src/ui/library.rs @@ -34,7 +34,7 @@ use crate::core::{ update_presentation_in_db, }, service_items::ServiceItem, - songs::{self, Song, add_song_to_db, update_song_in_db}, + songs::{self, Song, add_to_db, update_song_in_db}, videos::{self, Video, add_video_to_db, update_video_in_db}, }; @@ -164,7 +164,7 @@ impl<'a> Library { let songs = self.song_library.items.drain(..).collect(); return Action::Task(Task::perform( - add_song_to_db(songs, Arc::clone(&self.db)), + add_to_db(songs, Arc::clone(&self.db)), move |res| match res { Ok(songs) => Message::ReaddSongs(songs), Err(e) => { @@ -591,27 +591,31 @@ impl<'a> Library { .add_item(song.clone()) .err() else { - let task = Task::future( - self.db.acquire(), - ) - .map_err(|e| { - miette::miette!( - "Database error: {e}" - ) - }) - .and_then(move |db| { - Task::perform( - add_song_to_db(db), - { - move |res| { - res.map(|_song| { - Message::None - }) + let songs = self + .song_library + .items + .drain(..) + .collect(); + let task = Task::perform( + songs::add_to_db( + songs, + Arc::clone(&self.db), + ), + { + move |res| match res { + Ok(songs) => { + Message::ReaddSongs( + songs, + ) } - }, - ) - }) - .map(|r| r.unwrap_or(Message::None)); + + Err(e) => { + error!(?e); + Message::None + } + } + }, + ); tasks.push(task); continue; }; @@ -1230,17 +1234,27 @@ impl<'a> Library { if let Some(song) = self.song_library.get_item(*index) { + let songs = self + .song_library + .items + .drain(..) + .collect(); Task::perform( - self.song_library - .remove_song(song.id, &self.db), - |r| { - if let Err(e) = r { - error!(?e); + songs::remove_from_db( + Arc::clone(&self.db), + songs, + song.id, + ), + |r| match r { + Ok(songs) => { + Message::ReaddSongs(songs) + } + Err(e) => { + error!(?e); + Message::None } - Message::None }, ) - .map(|m| Ok(m)) } else { Task::none() } @@ -1414,3 +1428,9 @@ pub fn elide_text(text: impl AsRef, width: f32) -> String { text } } +!!r +r +r +xr +lr +qAؖ0yg \ No newline at end of file