more functions for db actions

This commit is contained in:
Chris Cochrun 2026-04-07 09:45:46 -05:00
parent 4d8e2cf270
commit 82bed2c9b8
2 changed files with 69 additions and 40 deletions

View file

@ -960,17 +960,25 @@ impl Model<Song> {
}
pub async fn remove_from_db(
db: PoolConnection<Sqlite>,
db: Arc<SqlitePool>,
mut songs: Vec<Song>,
id: i32,
) -> Result<()> {
query!("DELETE FROM songs WHERE id = $1", id)
.execute(&mut db.detach())
.await
.into_diagnostic()
.map(|_| ())
) -> Result<Vec<Song>> {
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<Song>,
db: Arc<SqlitePool>,
) -> Result<Vec<Song>> {
@ -1021,7 +1029,8 @@ pub async fn add_song_to_db(
pub async fn update_song_in_db(
item: Song,
db: PoolConnection<Sqlite>,
songs: Vec<Song>,
db: Arc<SqlitePool>,
) -> 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()?;

View file

@ -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<str>, width: f32) -> String {
text
}
}
ÿÿÿÿ!ÿÿÿÿÿÿÿÿÿÿÿÿ·<EFBFBD>!Àƒr
¨ƒr
<EFBFBD>ƒr
xƒr
lƒr
qÿÿÿ<EFBFBD>A§áùñØ0yÈëúéºg