diff --git a/src/core/images.rs b/src/core/images.rs index e6a1502..098ccfb 100644 --- a/src/core/images.rs +++ b/src/core/images.rs @@ -15,7 +15,7 @@ use std::{ path::{Path, PathBuf}, sync::Arc, }; -use tracing::{debug, error}; +use tracing::error; #[derive( Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, @@ -157,69 +157,6 @@ impl ServiceTrait for Image { } impl Model { - pub async fn append_image( - &mut self, - image: Image, - db: &SqlitePool, - ) -> Result<()> { - todo!() - } - - pub async fn new_image( - &mut self, - db: &SqlitePool, - ) -> Result { - todo!() - } - pub async fn update_image( - &mut self, - image: Image, - db: &SqlitePool, - ) -> Result<()> { - let id = image.id; - self.update_item(image.clone(), |current_image| { - current_image.id == id - })?; - let path = image - .path - .to_str() - .map(std::string::ToString::to_string) - .unwrap_or_default(); - debug!(?image, "should be been updated"); - let result = query!( - r#"UPDATE images SET title = $2, file_path = $3 WHERE id = $1"#, - image.id, - image.title, - path, - ) - .execute(db) - .await.into_diagnostic(); - - match result { - Ok(_) => { - debug!("should have been updated"); - Ok(()) - } - Err(e) => { - error! {?e}; - Err(e) - } - } - } - - pub async fn remove_image( - &mut self, - id: i32, - db: &SqlitePool, - ) -> Result<()> { - self.remove_item(|image| image.id == id)?; - query!("DELETE FROM images WHERE id = $1", id) - .execute(db) - .await - .into_diagnostic() - .map(|_| ()) - } - pub async fn new_image_model(db: &mut SqlitePool) -> Self { let mut model = Self { items: vec![], @@ -328,7 +265,7 @@ pub async fn update_image( .expect("We should have this image already") })?; - replace(current_image, image); + let _ = replace(current_image, image); Ok(images) } diff --git a/src/core/model.rs b/src/core/model.rs index 6762a84..fdcf7c7 100644 --- a/src/core/model.rs +++ b/src/core/model.rs @@ -94,7 +94,7 @@ impl Model { self.items .iter() .position(predicate) - .ok_or(miette!("Item cannot be found")) + .ok_or_else(|| miette!("Item cannot be found")) .map(|index| self.items.get_mut(index).expect("Since we found position this should always exist")) .map(|current_item| { let _old_item = replace(current_item, item); @@ -108,7 +108,7 @@ impl Model { self.items .iter() .position(predicate) - .ok_or(miette!("Item cannot be found")) + .ok_or_else(|| miette!("Item cannot be found")) .map(|index| { self.items.remove(index); }) diff --git a/src/core/presentations.rs b/src/core/presentations.rs index 66f9d1b..554f8a7 100644 --- a/src/core/presentations.rs +++ b/src/core/presentations.rs @@ -4,8 +4,8 @@ use miette::{IntoDiagnostic, Result, miette}; use mupdf::{Colorspace, Document, Matrix}; use serde::{Deserialize, Serialize}; use sqlx::{ - Row, Sqlite, SqliteConnection, SqlitePool, pool::PoolConnection, - prelude::FromRow, query, sqlite::SqliteRow, + Row, SqliteConnection, SqlitePool, prelude::FromRow, query, + sqlite::SqliteRow, }; use std::{ mem::replace, @@ -302,35 +302,6 @@ impl FromRow<'_, SqliteRow> for Presentation { } impl Model { - pub async fn append_presentation( - &mut self, - presentation: Presentation, - db: &SqlitePool, - ) -> Result<()> { - todo!() - } - - pub async fn new_presentation( - &mut self, - db: &SqlitePool, - ) -> Result<()> { - todo!() - } - - pub async fn update_presentation( - &mut self, - presentation: Presentation, - db: &SqlitePool, - ) -> Result<()> { - todo!() - } - pub async fn remove_presentation( - &mut self, - id: i32, - db: &SqlitePool, - ) -> Result<()> { - todo!() - } pub async fn new_presentation_model(db: &mut SqlitePool) -> Self { let mut model = Self { items: vec![], @@ -519,7 +490,7 @@ pub async fn update_presentation( .expect("We should have this presentation already") })?; - replace(current_presentation, presentation); + let _ = replace(current_presentation, presentation); Ok(presentations) } diff --git a/src/core/songs.rs b/src/core/songs.rs index fb503ef..0661482 100644 --- a/src/core/songs.rs +++ b/src/core/songs.rs @@ -15,8 +15,8 @@ use itertools::Itertools; use miette::{IntoDiagnostic, Result, miette}; use serde::{Deserialize, Serialize}; use sqlx::{ - FromRow, Row, Sqlite, SqliteConnection, SqlitePool, - pool::PoolConnection, query, sqlite::SqliteRow, + FromRow, Row, SqliteConnection, SqlitePool, query, + sqlite::SqliteRow, }; use tracing::{debug, error}; @@ -509,8 +509,10 @@ impl FromRow<'_, SqliteRow> for Song { impl From for Song { fn from(value: OnlineSong) -> Self { - let mut song = Self::default(); - song.verse_map = Some(HashMap::new()); + let mut song = Self { + verse_map: Some(HashMap::new()), + ..Default::default() + }; for line in value.lyrics.lines() { let next_verse = song.get_next_verse_name(); if let Some(verse_map) = song.verse_map.as_mut() { @@ -740,187 +742,6 @@ pub async fn get_song_from_db( } impl Model { - // Not sure we will use this function. As it is, it makes more sense for - // a new song to be made within the model and then passed back out. - // But maybe for encapsulation reasons, it makes sense to have this? - pub async fn append_song( - &mut self, - song: Song, - db: &SqlitePool, - ) -> Result<()> { - self.add_item(song)?; - todo!() - } - - pub async fn new_song( - &mut self, - db: Arc, - ) -> Result { - let mut song = Song::default(); - - let verse_order = { - song.verse_order.clone().map_or_else(String::new, |vo| { - vo.into_iter() - .map(|mut s| { - s.push(' '); - s - }) - .collect::() - }) - }; - - 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()); - - 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, - song.author, - song.ccli, - verse_order, - audio, - song.font, - song.font_size, - background - ) - .execute(&*db) - .await - .into_diagnostic()?; - song.id = i32::try_from(res.last_insert_rowid()).expect( - "Fairly confident that this number won't get that high", - ); - self.add_item(song.clone())?; - Ok(song) - } - - pub async fn update_song( - &mut self, - song: Song, - db: &SqlitePool, - ) -> Result<()> { - let id = song.id; - self.update_item(song.clone(), |song| song.id == id)?; - // debug!(?item); - let verse_order = - ron::ser::to_string(&song.verses).into_diagnostic()?; - - let audio = song - .audio - .map(|a| a.to_str().unwrap_or_default().to_string()); - - let background = song - .background - .map(|b| b.path.to_str().unwrap_or_default().to_string()); - - let lyrics = song.verse_map.map(|map| { - map.iter() - .map(|(name, lyric)| { - let lyric = - lyric.trim_end_matches('\n').to_string(); - (name.to_owned(), lyric) - }) - .collect::>() - }); - let lyrics = - ron::ser::to_string(&lyrics).into_diagnostic()?; - - let (vertical_alignment, horizontal_alignment) = - song.text_alignment.map_or_else( - || ("center", "center"), - |ta| match ta { - TextAlignment::TopLeft => ("top", "left"), - TextAlignment::TopCenter => ("top", "center"), - TextAlignment::TopRight => ("top", "right"), - TextAlignment::MiddleLeft => ("center", "left"), - TextAlignment::MiddleCenter => { - ("center", "center") - } - TextAlignment::MiddleRight => ("center", "right"), - TextAlignment::BottomLeft => ("bottom", "left"), - TextAlignment::BottomCenter => { - ("bottom", "center") - } - TextAlignment::BottomRight => ("bottom", "right"), - }, - ); - - let stroke_size = song.stroke_size.unwrap_or_default(); - let shadow_size = song.shadow_size.unwrap_or_default(); - let (shadow_offset_x, shadow_offset_y) = - song.shadow_offset.unwrap_or_default(); - - let stroke_color = ron::ser::to_string(&song.stroke_color) - .into_diagnostic()?; - let shadow_color = ron::ser::to_string(&song.shadow_color) - .into_diagnostic()?; - - let style = ron::ser::to_string(&song.font_style) - .into_diagnostic()?; - let weight = ron::ser::to_string(&song.font_weight) - .into_diagnostic()?; - - // debug!( - // ?stroke_size, - // ?stroke_color, - // ?shadow_size, - // ?shadow_color, - // ?shadow_offset_x, - // ?shadow_offset_y - // ); - - let result = 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, horizontal_text_alignment = $11, vertical_text_alignment = $12, stroke_color = $13, shadow_color = $14, stroke_size = $15, shadow_size = $16, shadow_offset_x = $17, shadow_offset_y = $18, style = $19, weight = $20 WHERE id = $1"#, - song.id, - song.title, - lyrics, - song.author, - song.ccli, - verse_order, - audio, - song.font, - song.font_size, - background, - horizontal_alignment, - vertical_alignment, - stroke_color, - shadow_color, - stroke_size, - shadow_size, - shadow_offset_x, - shadow_offset_y, - style, - weight - ) - .execute(db) - .await - .into_diagnostic()?; - - debug!(rows_affected = ?result.rows_affected()); - - Ok(()) - } - - pub async fn remove_song( - &mut self, - id: i32, - db: &SqlitePool, - ) -> Result<()> { - self.remove_item(|current_song| id == current_song.id)?; - query!("DELETE FROM songs WHERE id = $1", id) - .execute(db) - .await - .into_diagnostic() - .map(|_| ()) - } - pub async fn new_song_model(db: &mut SqlitePool) -> Self { let mut model = Self { items: vec![], @@ -1131,7 +952,7 @@ pub async fn update_song( .position(|current_song| current_song.id == song.id) .ok_or_else(|| miette!("Could not find song in model"))?; - replace( + let _ = replace( songs .get_mut(index) .expect("We have found the song so this shouldn't fail"), @@ -1565,7 +1386,7 @@ You saved my soul" let mut song = test_song(); song.id = db_song.id; let conn = db.acquire().await.into_diagnostic()?; - update_song_in_db(song, conn).await?; + update_song(song, conn).await?; } Ok(()) } @@ -1640,7 +1461,7 @@ You saved my soul" let updated_model_song = song_model.find(|s| s.id == 7).unwrap(); assert_eq!(&cloned_song, updated_model_song); - match update_song_in_db( + match update_song( cloned_song.clone(), db.acquire().await.unwrap(), ) diff --git a/src/core/videos.rs b/src/core/videos.rs index 867aeac..74e689e 100644 --- a/src/core/videos.rs +++ b/src/core/videos.rs @@ -10,16 +10,13 @@ use super::{ use crisp::types::{Keyword, Symbol, Value}; use miette::{IntoDiagnostic, Result, miette}; use serde::{Deserialize, Serialize}; -use sqlx::{ - Sqlite, SqliteConnection, SqlitePool, pool::PoolConnection, - query, query_as, -}; +use sqlx::{SqliteConnection, SqlitePool, query, query_as}; use std::{ mem::replace, path::{Path, PathBuf}, sync::Arc, }; -use tracing::{debug, error}; +use tracing::error; #[derive( Clone, Debug, Default, PartialEq, Serialize, Deserialize, @@ -202,33 +199,6 @@ impl ServiceTrait for Video { } impl Model