fix songs adding not working correctly
Some checks are pending
/ test (push) Waiting to run

This commit is contained in:
Chris Cochrun 2025-10-16 20:38:42 -05:00
parent 1a97622926
commit b4311a613e
3 changed files with 57 additions and 40 deletions

View file

@ -421,13 +421,13 @@ pub async fn remove_from_db(
}
pub async fn add_song_to_db(
song: Song,
db: PoolConnection<Sqlite>,
) -> Result<()> {
) -> Result<Song> {
let mut db = db.detach();
let mut song = Song::default();
let verse_order = {
if let Some(vo) = song.verse_order {
if let Some(vo) = song.verse_order.clone() {
vo.into_iter()
.map(|mut s| {
s.push(' ');
@ -441,13 +441,15 @@ pub async fn add_song_to_db(
let audio = song
.audio
.clone()
.map(|a| a.to_str().unwrap_or_default().to_string());
let background = song
.background
.clone()
.map(|b| b.path.to_str().unwrap_or_default().to_string());
query!(
let res = query!(
r#"INSERT INTO songs (title, lyrics, author, ccli, verse_order, audio, font, font_size, background) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)"#,
song.title,
song.lyrics,
@ -462,7 +464,8 @@ pub async fn add_song_to_db(
.execute(&mut db)
.await
.into_diagnostic()?;
Ok(())
song.id = res.last_insert_rowid() as i32;
Ok(song)
}
pub async fn update_song_in_db(

View file

@ -100,6 +100,7 @@ pub enum Message {
OpenContext(i32),
None,
AddFiles(Vec<ServiceItemKind>),
AddSong(Song),
AddImages(Option<Vec<Image>>),
AddVideos(Option<Vec<Video>>),
AddPresentations(Option<Vec<Presentation>>),
@ -141,19 +142,37 @@ impl<'a> Library {
Message::DeleteItem => {
return self.delete_items();
}
Message::AddSong(song) => {
if let Err(e) = self.song_library.add_item(song) {
error!(?e, "couldn't add song to model");
} else {
let index =
(self.song_library.items.len() - 1) as i32;
return Action::Task(Task::done(
Message::OpenItem(Some((
LibraryKind::Song,
index,
))),
));
}
}
Message::AddItem => {
let kind =
self.library_open.unwrap_or(LibraryKind::Song);
let item = match kind {
match kind {
LibraryKind::Song => {
let song = Song::default();
let index = self.song_library.items.len();
self.song_library
.add_item(song)
.map(|_| {
(LibraryKind::Song, index as i32)
let task = Task::future(self.db.acquire()).and_then(move |db| {
Task::perform(add_song_to_db(db), move |res| {
match res {
Ok(song) => {
Message::AddSong(song)
},
Err(e) => {error!(?e, "couldn't add song to db"); Message::None}
}
})
.ok()
});
return Action::Task(task);
}
LibraryKind::Video => {
return Action::Task(Task::perform(
@ -174,7 +193,6 @@ impl<'a> Library {
));
}
};
return self.update(Message::OpenItem(item));
}
Message::AddVideos(videos) => {
debug!(?videos);
@ -653,24 +671,20 @@ impl<'a> Library {
.add_item(song.clone())
.err()
else {
let task = Task::future(
self.db.acquire(),
)
let task =
Task::future(self.db.acquire())
.and_then(move |db| {
Task::perform(
add_song_to_db(
song.clone(),
db,
),
add_song_to_db(db),
{
let song = song.clone();
move |res| {
debug!(
?song,
"added to db"
if let Err(
e,
) = res
{
error!(
?e
);
if let Err(e) = res {
error!(?e);
}
Message::None
}

View file

@ -51,7 +51,7 @@ impl PresentationEditor {
Self {
presentation: None,
document: None,
title: "Death was Arrested".to_string(),
title: "".to_string(),
editing: false,
current_slide: None,
current_slide_index: None,
@ -183,7 +183,7 @@ impl PresentationEditor {
}
pub fn view(&self) -> Element<Message> {
let container = if let Some(slide) = &self.current_slide {
let presentation = if let Some(slide) = &self.current_slide {
container(
widget::image(slide)
.content_fit(ContentFit::ScaleDown),
@ -199,7 +199,7 @@ impl PresentationEditor {
];
let column = column![
self.toolbar(),
container.center(Length::Fill),
presentation.center(Length::Fill),
control_buttons
]
.spacing(theme::active().cosmic().space_l());