From 2a980e523c5b109b65a9c78fbcb6fd617f367bbd Mon Sep 17 00:00:00 2001 From: Chris Cochrun Date: Sun, 9 Mar 2025 07:10:36 -0500 Subject: [PATCH] adding updating functions for all other kinds of items --- src/core/images.rs | 27 +++++++- src/core/presentations.rs | 28 ++++++++- src/core/songs.rs | 61 +----------------- src/core/videos.rs | 30 ++++++++- src/ui/library.rs | 127 +++++++++++++++++++++++++++++++++++++- 5 files changed, 206 insertions(+), 67 deletions(-) diff --git a/src/core/images.rs b/src/core/images.rs index 85fd7e0..25f7a55 100644 --- a/src/core/images.rs +++ b/src/core/images.rs @@ -9,7 +9,10 @@ use super::{ use crisp::types::{Keyword, Symbol, Value}; use miette::{IntoDiagnostic, Result}; use serde::{Deserialize, Serialize}; -use sqlx::{query_as, SqliteConnection, SqlitePool}; +use sqlx::{ + pool::PoolConnection, query, query_as, Sqlite, SqliteConnection, + SqlitePool, +}; use std::path::PathBuf; use tracing::error; @@ -157,6 +160,28 @@ impl Model { } } +pub async fn update_image_in_db( + image: Image, + db: PoolConnection, +) -> Result<()> { + let path = image + .path + .to_str() + .map(|s| s.to_string()) + .unwrap_or_default(); + query!( + r#"UPDATE images SET title = $2, file_path = $3 WHERE id = $1"#, + image.id, + image.title, + path, + ) + .execute(&mut db.detach()) + .await + .into_diagnostic()?; + + Ok(()) +} + pub async fn get_image_from_db( database_id: i32, db: &mut SqliteConnection, diff --git a/src/core/presentations.rs b/src/core/presentations.rs index 9e213fc..a2cc842 100644 --- a/src/core/presentations.rs +++ b/src/core/presentations.rs @@ -2,8 +2,8 @@ use crisp::types::{Keyword, Symbol, Value}; use miette::{IntoDiagnostic, Result}; use serde::{Deserialize, Serialize}; use sqlx::{ - prelude::FromRow, query, sqlite::SqliteRow, Row, - SqliteConnection, SqlitePool, + pool::PoolConnection, prelude::FromRow, query, sqlite::SqliteRow, + Row, Sqlite, SqliteConnection, SqlitePool, }; use std::path::PathBuf; use tracing::error; @@ -206,6 +206,30 @@ impl Model { } } +pub async fn update_presentation_in_db( + presentation: Presentation, + db: PoolConnection, +) -> Result<()> { + let path = presentation + .path + .to_str() + .map(|s| s.to_string()) + .unwrap_or_default(); + let html = presentation.kind == PresKind::Html; + query!( + r#"UPDATE presentations SET title = $2, file_path = $3, html = $4 WHERE id = $1"#, + presentation.id, + presentation.title, + path, + html + ) + .execute(&mut db.detach()) + .await + .into_diagnostic()?; + + Ok(()) +} + pub async fn get_presentation_from_db( database_id: i32, db: &mut SqliteConnection, diff --git a/src/core/songs.rs b/src/core/songs.rs index c2eb87a..38fbe7d 100644 --- a/src/core/songs.rs +++ b/src/core/songs.rs @@ -375,65 +375,6 @@ pub async fn get_song_from_db( } impl Model { - pub async fn update_song_in_db( - &mut self, - item: Song, - db: &mut SqliteConnection, - ) -> Result<()> { - // self.update_item(item.clone(), index)?; - - let verse_order = { - if let Some(vo) = item.verse_order { - vo.into_iter() - .map(|mut s| { - s.push_str(" "); - s - }) - .collect::() - } else { - String::from("") - } - }; - - let audio = item - .audio - .map(|a| a.to_str().unwrap_or_default().to_string()); - - let background = item - .background - .map(|b| b.path.to_str().unwrap_or_default().to_string()); - - // let text_alignment = item.text_alignment.map(|ta| match ta { - // TextAlignment::TopLeft => todo!(), - // TextAlignment::TopCenter => todo!(), - // TextAlignment::TopRight => todo!(), - // TextAlignment::MiddleLeft => todo!(), - // TextAlignment::MiddleCenter => todo!(), - // TextAlignment::MiddleRight => todo!(), - // TextAlignment::BottomLeft => todo!(), - // TextAlignment::BottomCenter => todo!(), - // TextAlignment::BottomRight => todo!(), - // }) - - query!( - r#"UPDATE songs SET title = $2, lyrics = $3, author = $4, ccli = $5, verse_order = $6, audio = $7, font = $8, font_size = $9, background = $10 WHERE id = $1"#, - item.id, - item.title, - item.lyrics, - item.author, - item.ccli, - verse_order, - audio, - item.font, - item.font_size, - background - ) - .execute(db) - .await - .into_diagnostic()?; - - Ok(()) - } pub async fn new_song_model(db: &mut SqlitePool) -> Self { let mut model = Self { items: vec![], @@ -468,7 +409,7 @@ impl Model { } } -pub async fn update_song_in_db<'a>( +pub async fn update_song_in_db( item: Song, db: PoolConnection, ) -> Result<()> { diff --git a/src/core/videos.rs b/src/core/videos.rs index 0720cc7..19d447f 100644 --- a/src/core/videos.rs +++ b/src/core/videos.rs @@ -11,7 +11,10 @@ use cosmic::iced::Executor; use crisp::types::{Keyword, Symbol, Value}; use miette::{IntoDiagnostic, Result}; use serde::{Deserialize, Serialize}; -use sqlx::{query_as, SqliteConnection, SqlitePool}; +use sqlx::{ + pool::PoolConnection, query, query_as, Sqlite, SqliteConnection, + SqlitePool, +}; use std::path::PathBuf; use tracing::error; @@ -196,6 +199,31 @@ impl Model