adding updating functions for all other kinds of items

This commit is contained in:
Chris Cochrun 2025-03-09 07:10:36 -05:00
parent 62453866c5
commit 2a980e523c
5 changed files with 206 additions and 67 deletions

View file

@ -9,7 +9,10 @@ use super::{
use crisp::types::{Keyword, Symbol, Value}; use crisp::types::{Keyword, Symbol, Value};
use miette::{IntoDiagnostic, Result}; use miette::{IntoDiagnostic, Result};
use serde::{Deserialize, Serialize}; 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 std::path::PathBuf;
use tracing::error; use tracing::error;
@ -157,6 +160,28 @@ impl Model<Image> {
} }
} }
pub async fn update_image_in_db(
image: Image,
db: PoolConnection<Sqlite>,
) -> 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( pub async fn get_image_from_db(
database_id: i32, database_id: i32,
db: &mut SqliteConnection, db: &mut SqliteConnection,

View file

@ -2,8 +2,8 @@ use crisp::types::{Keyword, Symbol, Value};
use miette::{IntoDiagnostic, Result}; use miette::{IntoDiagnostic, Result};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use sqlx::{ use sqlx::{
prelude::FromRow, query, sqlite::SqliteRow, Row, pool::PoolConnection, prelude::FromRow, query, sqlite::SqliteRow,
SqliteConnection, SqlitePool, Row, Sqlite, SqliteConnection, SqlitePool,
}; };
use std::path::PathBuf; use std::path::PathBuf;
use tracing::error; use tracing::error;
@ -206,6 +206,30 @@ impl Model<Presentation> {
} }
} }
pub async fn update_presentation_in_db(
presentation: Presentation,
db: PoolConnection<Sqlite>,
) -> 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( pub async fn get_presentation_from_db(
database_id: i32, database_id: i32,
db: &mut SqliteConnection, db: &mut SqliteConnection,

View file

@ -375,65 +375,6 @@ pub async fn get_song_from_db(
} }
impl Model<Song> { impl Model<Song> {
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::<String>()
} 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 { pub async fn new_song_model(db: &mut SqlitePool) -> Self {
let mut model = Self { let mut model = Self {
items: vec![], items: vec![],
@ -468,7 +409,7 @@ impl Model<Song> {
} }
} }
pub async fn update_song_in_db<'a>( pub async fn update_song_in_db(
item: Song, item: Song,
db: PoolConnection<Sqlite>, db: PoolConnection<Sqlite>,
) -> Result<()> { ) -> Result<()> {

View file

@ -11,7 +11,10 @@ use cosmic::iced::Executor;
use crisp::types::{Keyword, Symbol, Value}; use crisp::types::{Keyword, Symbol, Value};
use miette::{IntoDiagnostic, Result}; use miette::{IntoDiagnostic, Result};
use serde::{Deserialize, Serialize}; 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 std::path::PathBuf;
use tracing::error; use tracing::error;
@ -196,6 +199,31 @@ impl Model<Video> {
} }
} }
pub async fn update_video_in_db(
video: Video,
db: PoolConnection<Sqlite>,
) -> Result<()> {
let path = video
.path
.to_str()
.map(|s| s.to_string())
.unwrap_or_default();
query!(
r#"UPDATE videos SET title = $2, file_path = $3, start_time = $4, end_time = $5, loop = $6 WHERE id = $1"#,
video.id,
video.title,
path,
video.start_time,
video.end_time,
video.looping,
)
.execute(&mut db.detach())
.await
.into_diagnostic()?;
Ok(())
}
pub async fn get_video_from_db( pub async fn get_video_from_db(
database_id: i32, database_id: i32,
db: &mut SqliteConnection, db: &mut SqliteConnection,

View file

@ -16,12 +16,12 @@ use tracing::{debug, error};
use crate::core::{ use crate::core::{
content::Content, content::Content,
images::Image, images::{update_image_in_db, Image},
model::{LibraryKind, Model}, model::{LibraryKind, Model},
presentations::Presentation, presentations::{update_presentation_in_db, Presentation},
service_items::ServiceItem, service_items::ServiceItem,
songs::{update_song_in_db, Song}, songs::{update_song_in_db, Song},
videos::Video, videos::{update_video_in_db, Video},
}; };
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
@ -50,6 +50,12 @@ pub(crate) enum Message {
SelectItem(Option<(LibraryKind, i32)>), SelectItem(Option<(LibraryKind, i32)>),
UpdateSong(Song), UpdateSong(Song),
SongChanged, SongChanged,
UpdateImage(Image),
ImageChanged,
UpdateVideo(Video),
VideoChanged,
UpdatePresentation(Presentation),
PresentationChanged,
Error(String), Error(String),
None, None,
} }
@ -141,6 +147,93 @@ impl<'a> Library {
debug!("song changed"); debug!("song changed");
Task::none() Task::none()
} }
Message::UpdateImage(image) => {
let Some((kind, index)) = self.editing_item else {
error!("Not editing an item");
return Task::none();
};
if kind != LibraryKind::Image {
error!("Not editing a image item");
return Task::none();
};
match self
.image_library
.update_item(image.clone(), index)
{
Ok(_) => Task::future(self.db.acquire())
.and_then(move |conn| {
Task::perform(
update_image_in_db(
image.clone(),
conn,
),
|_| Message::ImageChanged,
)
}),
Err(_) => todo!(),
}
}
Message::ImageChanged => todo!(),
Message::UpdateVideo(video) => {
let Some((kind, index)) = self.editing_item else {
error!("Not editing an item");
return Task::none();
};
if kind != LibraryKind::Video {
error!("Not editing a video item");
return Task::none();
};
match self
.video_library
.update_item(video.clone(), index)
{
Ok(_) => Task::future(self.db.acquire())
.and_then(move |conn| {
Task::perform(
update_video_in_db(
video.clone(),
conn,
),
|_| Message::VideoChanged,
)
}),
Err(_) => todo!(),
}
}
Message::VideoChanged => todo!(),
Message::UpdatePresentation(presentation) => {
let Some((kind, index)) = self.editing_item else {
error!("Not editing an item");
return Task::none();
};
if kind != LibraryKind::Presentation {
error!("Not editing a presentation item");
return Task::none();
};
match self
.presentation_library
.update_item(presentation.clone(), index)
{
Ok(_) => Task::future(self.db.acquire())
.and_then(move |conn| {
Task::perform(
update_presentation_in_db(
presentation.clone(),
conn,
),
|_| Message::PresentationChanged,
)
}),
Err(_) => todo!(),
}
}
Message::PresentationChanged => todo!(),
Message::Error(_) => todo!(), Message::Error(_) => todo!(),
} }
} }
@ -385,6 +478,34 @@ impl<'a> Library {
}) })
.into() .into()
} }
// fn update_item<C: Content>(self, item: C) -> Task<Message> {
// let Some((kind, index)) = self.editing_item else {
// error!("Not editing an item");
// return Task::none();
// };
// match kind {
// LibraryKind::Song => todo!(),
// LibraryKind::Video => todo!(),
// LibraryKind::Image => {
// match self
// .image_library
// .update_item(item as Image, index)
// {
// Ok(_) => Task::future(self.db.acquire())
// .and_then(|conn| {
// Task::perform(
// update_image_in_db(item, conn),
// |_| Message::ImageChanged,
// )
// }),
// Err(_) => todo!(),
// }
// }
// LibraryKind::Presentation => todo!(),
// }
// }
} }
async fn add_db() -> Result<SqlitePool> { async fn add_db() -> Result<SqlitePool> {