diff --git a/src/rust/core/lib.rs b/src/rust/core/lib.rs index b505e4d..bb3db24 100644 --- a/src/rust/core/lib.rs +++ b/src/rust/core/lib.rs @@ -6,3 +6,4 @@ pub mod service_items; pub mod slides; pub mod songs; pub mod videos; +pub mod file; diff --git a/src/rust/core/model.rs b/src/rust/core/model.rs index fbc152d..d29d55d 100644 --- a/src/rust/core/model.rs +++ b/src/rust/core/model.rs @@ -50,23 +50,25 @@ impl Default for Model { fn default() -> Self { Self { items: vec![], - db: { - let rt = tokio::runtime::Runtime::new().unwrap(); - let mut data = dirs::data_local_dir().unwrap(); - data.push("lumina"); - data.push("library-db.sqlite3"); - let mut db_url = String::from("sqlite://"); - db_url.push_str(data.to_str().unwrap()); - rt.block_on(async { - SqliteConnection::connect(&db_url) - .await - .expect("problems") - }) - }, + db: get_db(), } } } +pub fn get_db() -> SqliteConnection { + let rt = tokio::runtime::Runtime::new().unwrap(); + let mut data = dirs::data_local_dir().unwrap(); + data.push("lumina"); + data.push("library-db.sqlite3"); + let mut db_url = String::from("sqlite://"); + db_url.push_str(data.to_str().unwrap()); + rt.block_on(async { + SqliteConnection::connect(&db_url) + .await + .expect("problems") + }) +} + pub trait Modeling { type Item; @@ -86,5 +88,4 @@ pub trait Modeling { } #[cfg(test)] -mod test { -} +mod test {} diff --git a/src/rust/core/presentations.rs b/src/rust/core/presentations.rs index 9b1fde2..7777ef5 100644 --- a/src/rust/core/presentations.rs +++ b/src/rust/core/presentations.rs @@ -1,4 +1,12 @@ -#[derive(Debug, Clone, Default, PartialEq, Eq)] +use std::path::PathBuf; + +use serde::{Deserialize, Serialize}; +use sqlx::query; +use tracing::error; + +use crate::model::Model; + +#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)] pub enum PresKind { Html, #[default] @@ -6,10 +14,12 @@ pub enum PresKind { Generic, } -#[derive(Debug, Clone, Default, PartialEq, Eq)] +#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)] pub struct Presentation { - title: String, - kind: PresKind, + pub id: i32, + pub title: String, + pub path: PathBuf, + pub kind: PresKind, } impl Presentation { @@ -25,13 +35,64 @@ impl Presentation { } } +impl Model { + pub fn load_from_db(&mut self) { + let rt = tokio::runtime::Runtime::new().unwrap(); + rt.block_on(async { + let result = query!(r#"SELECT id as "id: i32", title, filePath as "path", html from presentations"#).fetch_all(&mut self.db).await; + match result { + Ok(v) => { + for presentation in v.into_iter() { + let _ = self.add_item(Presentation { + id: presentation.id, + title: presentation.title, + path: presentation.path.into(), + kind: if presentation.html { + PresKind::Html + } else { + PresKind::Pdf + } + }); + } + } + Err(e) => error!("There was an error in converting presentations: {e}"), + } + }); + } +} + #[cfg(test)] mod test { - use crate::presentations::{PresKind, Presentation}; + use super::*; + use pretty_assertions::assert_eq; + + fn test_presentation() -> Presentation { + Presentation { + id: 54, + title: "20240327T133649--12-isaiah-and-jesus__lesson_project_tfc".into(), + path: PathBuf::from( + "file:///home/chris/docs/notes/lessons/20240327T133649--12-isaiah-and-jesus__lesson_project_tfc.html", + ), + kind: PresKind::Html, + } + } #[test] - pub fn test_presentation() { + pub fn test_pres() { let pres = Presentation::new(); assert_eq!(pres.get_kind(), &PresKind::Pdf) } + + #[test] + pub fn test_db_and_model() { + let mut presentation_model: Model = + Model::default(); + presentation_model.load_from_db(); + if let Some(presentation) = presentation_model.get_item(10) { + let test_presentation = test_presentation(); + assert_eq!(&test_presentation, presentation); + } else { + assert!(false); + } + } } diff --git a/src/rust/core/service_items.rs b/src/rust/core/service_items.rs index 2dfb856..8224aff 100644 --- a/src/rust/core/service_items.rs +++ b/src/rust/core/service_items.rs @@ -1,3 +1,5 @@ +use color_eyre::eyre::Result; + use crate::images::Image; use crate::presentations::Presentation; use crate::songs::Song; @@ -6,10 +8,10 @@ use crate::videos::Video; use super::kinds::ServiceItemKind; #[derive(Debug, Default, PartialEq)] -struct ServiceItem { - id: i32, - database_id: i32, - kind: ServiceItemKind, +pub struct ServiceItem { + pub id: i32, + pub database_id: i32, + pub kind: ServiceItemKind, } #[derive(Debug, Default, PartialEq)] @@ -17,47 +19,96 @@ struct ServiceItemModel { items: Vec, } -impl From for ServiceItem { - fn from(_song: Song) -> Self { +impl From<&Song> for ServiceItem { + fn from(song: &Song) -> Self { Self { kind: ServiceItemKind::Song, + database_id: song.id, ..Default::default() } } } -impl From