formatting and adding video model

This commit is contained in:
Chris Cochrun 2024-10-07 11:43:45 -05:00
parent de34c6818a
commit 887fc733e7
8 changed files with 196 additions and 84 deletions

View file

@ -1,5 +1,5 @@
pub struct Image { pub struct Image {
title: String title: String,
} }
#[cfg(test)] #[cfg(test)]

View file

@ -53,7 +53,9 @@ impl From<ServiceItemKind> for String {
ServiceItemKind::Song => "song".to_owned(), ServiceItemKind::Song => "song".to_owned(),
ServiceItemKind::Video => "video".to_owned(), ServiceItemKind::Video => "video".to_owned(),
ServiceItemKind::Image => "image".to_owned(), ServiceItemKind::Image => "image".to_owned(),
ServiceItemKind::Presentation(_) => "presentation".to_owned(), ServiceItemKind::Presentation(_) => {
"presentation".to_owned()
}
ServiceItemKind::Content => "content".to_owned(), ServiceItemKind::Content => "content".to_owned(),
} }
} }

View file

@ -1,8 +1,8 @@
pub mod service_items; pub mod images;
pub mod kinds; pub mod kinds;
pub mod model;
pub mod presentations;
pub mod service_items;
pub mod slides; pub mod slides;
pub mod songs; pub mod songs;
pub mod videos; pub mod videos;
pub mod presentations;
pub mod images;
pub mod model;

View file

@ -1,15 +1,62 @@
use color_eyre::eyre::Result; use color_eyre::eyre::Result;
use sqlx::{Connection, SqliteConnection}; use sqlx::{Connection, SqliteConnection};
use crate::songs::Song;
#[derive(Debug)] #[derive(Debug)]
pub struct Model<T> { pub struct Model<T> {
pub items: Vec<T>, pub items: Vec<T>,
pub db: SqliteConnection, pub db: SqliteConnection,
} }
pub trait Models { // impl<T> Modeling for Model<T> {
// type Item = T;
// fn add_item(&mut self, item: Self::Item) -> Result<()> {
// self.items.push(item);
// Ok(())
// }
// fn add_to_db(&mut self, item: Self::Item) -> Result<()> {
// todo!()
// }
// fn update_item(&mut self, item: Self::Item, index: i32) -> Result<()> {
// todo!()
// }
// fn remove_item(&mut self, index: i32) -> Result<()> {
// todo!()
// }
// fn get_item(&self, index: i32) -> Option<&Self::Item> {
// todo!()
// }
// fn insert_item(&mut self, item: Self::Item, index: i32) -> Result<()> {
// todo!()
// }
// }
impl<T> Default for Model<T> {
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")
})
},
}
}
}
pub trait Modeling {
type Item; type Item;
fn setup_db() -> SqliteConnection { fn setup_db() -> SqliteConnection {
@ -20,15 +67,26 @@ pub trait Models {
let mut db_url = String::from("sqlite://"); let mut db_url = String::from("sqlite://");
db_url.push_str(data.to_str().unwrap()); db_url.push_str(data.to_str().unwrap());
rt.block_on(async { rt.block_on(async {
SqliteConnection::connect(&db_url).await.expect("problems") SqliteConnection::connect(&db_url)
.await
.expect("problems")
}) })
} }
fn add_item(&mut self, item: Self::Item) -> Result<()>; fn add_item(&mut self, item: Self::Item) -> Result<()>;
fn add_to_db(&mut self, item: Self::Item) -> Result<()>; fn add_to_db(&mut self, item: Self::Item) -> Result<()>;
fn update_item(&mut self, item: Self::Item, index: i32) -> Result<()>; fn update_item(
&mut self,
item: Self::Item,
index: i32,
) -> Result<()>;
fn remove_item(&mut self, index: i32) -> Result<()>; fn remove_item(&mut self, index: i32) -> Result<()>;
fn get_item(&self, index: i32) -> Option<&Self::Item>; fn get_item(&self, index: i32) -> Option<&Self::Item>;
fn insert_item(
&mut self,
item: Self::Item,
index: i32,
) -> Result<()>;
} }
#[cfg(test)] #[cfg(test)]

View file

@ -1,4 +1,3 @@
#[derive(Debug, Clone, Default, PartialEq, Eq)] #[derive(Debug, Clone, Default, PartialEq, Eq)]
pub enum PresKind { pub enum PresKind {
Html, Html,

View file

@ -2,7 +2,10 @@ use std::path::PathBuf;
use color_eyre::eyre::Result; use color_eyre::eyre::Result;
use crate::{images::Image, kinds::ServiceItemKind, presentations::Presentation, songs::Song, videos::Video}; use crate::{
images::Image, kinds::ServiceItemKind,
presentations::Presentation, songs::Song, videos::Video,
};
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum TextAlignment { pub enum TextAlignment {
@ -22,7 +25,7 @@ pub enum TextAlignment {
pub enum Background { pub enum Background {
#[default] #[default]
Image, Image,
Video Video,
} }
impl From<String> for Background { impl From<String> for Background {
@ -100,10 +103,11 @@ impl From<Presentation> for Slide {
} }
impl SlideModel { impl SlideModel {
pub fn add_song_to_end(&mut self,song: Song) -> Result<()> { pub fn add_song_to_end(&mut self, song: Song) -> Result<()> {
let lyrics = song.get_lyrics()?; let lyrics = song.get_lyrics()?;
let mut slides: Vec<Slide> = lyrics.iter().map(|lyric| { let mut slides: Vec<Slide> = lyrics
Slide { .iter()
.map(|lyric| Slide {
background: song.background.clone(), background: song.background.clone(),
text: lyric.to_owned(), text: lyric.to_owned(),
font: song.font.clone(), font: song.font.clone(),
@ -111,8 +115,8 @@ impl SlideModel {
kind: ServiceItemKind::Song, kind: ServiceItemKind::Song,
text_alignment: song.text_alignment, text_alignment: song.text_alignment,
..Default::default() ..Default::default()
} })
}).collect(); .collect();
self.slides.append(&mut slides); self.slides.append(&mut slides);
Ok(()) Ok(())
} }

View file

@ -1,15 +1,18 @@
use std::{collections::HashMap, mem::replace, path::PathBuf}; use std::{collections::HashMap, mem::replace, path::PathBuf};
use color_eyre::eyre::{eyre, Report, Result}; use color_eyre::eyre::{eyre, Result};
use sqlx::{query, Connection, SqliteConnection}; use sqlx::{query, Connection, SqliteConnection};
use tracing::{debug, error}; use tracing::{debug, error};
use crate::{model::{Model, Models}, slides::{Background, TextAlignment}}; use crate::{
model::{Model, Modeling},
slides::{Background, TextAlignment},
};
#[derive(Clone, Debug, Default, PartialEq, Eq)] #[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Song { pub struct Song {
pub title: String, pub title: String,
pub lyrics: String, pub lyrics: Option<String>,
pub author: String, pub author: String,
pub ccli: String, pub ccli: String,
pub audio: PathBuf, pub audio: PathBuf,
@ -22,15 +25,14 @@ pub struct Song {
} }
const VERSE_KEYWORDS: [&'static str; 24] = [ const VERSE_KEYWORDS: [&'static str; 24] = [
"Verse 1", "Verse 2", "Verse 3", "Verse 4", "Verse 1", "Verse 2", "Verse 3", "Verse 4", "Verse 5", "Verse 6",
"Verse 5", "Verse 6", "Verse 7", "Verse 8", "Verse 7", "Verse 8", "Chorus 1", "Chorus 2", "Chorus 3",
"Chorus 1", "Chorus 2", "Chorus 3", "Chorus 4", "Chorus 4", "Bridge 1", "Bridge 2", "Bridge 3", "Bridge 4",
"Bridge 1", "Bridge 2", "Bridge 3", "Bridge 4", "Intro 1", "Intro 2", "Ending 1", "Ending 2", "Other 1",
"Intro 1", "Intro 2", "Ending 1", "Ending 2", "Other 2", "Other 3", "Other 4",
"Other 1", "Other 2", "Other 3", "Other 4",
]; ];
impl Models for Model<Song> { impl Modeling for Model<Song> {
type Item = Song; type Item = Song;
fn add_item(&mut self, item: Self::Item) -> Result<()> { fn add_item(&mut self, item: Self::Item) -> Result<()> {
self.items.push(item); self.items.push(item);
@ -41,12 +43,20 @@ impl Models for Model<Song> {
todo!() todo!()
} }
fn update_item(&mut self, item: Self::Item, index: i32) -> Result<()> { fn update_item(
if let Some(current_song) = self.items.get_mut(index as usize) { &mut self,
item: Self::Item,
index: i32,
) -> Result<()> {
if let Some(current_song) = self.items.get_mut(index as usize)
{
let _old_song = replace(current_song, item); let _old_song = replace(current_song, item);
Ok(()) Ok(())
} else { } else {
Err(eyre!("Song doesn't exist in model. Id was {}", index)) Err(eyre!(
"Song doesn't exist in model. Id was {}",
index
))
} }
} }
@ -58,41 +68,18 @@ impl Models for Model<Song> {
self.items.remove(index as usize); self.items.remove(index as usize);
Ok(()) Ok(())
} }
}
impl Default for Model<Song> { fn insert_item(
fn default() -> Self { &mut self,
Self { item: Self::Item,
items: vec![], index: i32,
db: { ) -> Result<()> {
let rt = tokio::runtime::Runtime::new().unwrap(); self.items.insert(index as usize, item);
let mut data = dirs::data_local_dir().unwrap(); Ok(())
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")
})
}
}
} }
} }
impl Model<Song> { impl Model<Song> {
pub fn get_song(&self, id: i32) -> Option<&Song> {
self.items.get(id as usize)
}
pub fn remove_song(&mut self, id: i32) -> Result<(), Report> {
if let Some(_song) = self.items.get(id as usize) {
let _song = self.items.remove(id as usize);
Ok(())
} else {
Err(eyre!("Song doesn't exist in model. Id was {}", id))
}
}
pub fn load_from_db(&mut self) { pub fn load_from_db(&mut self) {
// static DATABASE_URL: &str = "sqlite:///home/chris/.local/share/lumina/library-db.sqlite3"; // static DATABASE_URL: &str = "sqlite:///home/chris/.local/share/lumina/library-db.sqlite3";
let rt = tokio::runtime::Runtime::new().unwrap(); let rt = tokio::runtime::Runtime::new().unwrap();
@ -101,9 +88,9 @@ impl Model<Song> {
match result { match result {
Ok(s) => { Ok(s) => {
for song in s.into_iter() { for song in s.into_iter() {
self.add_song(Song { let _ = self.add_item(Song {
title: song.title, title: song.title,
lyrics: song.lyrics, lyrics: Some(song.lyrics),
author: song.author, author: song.author,
ccli: song.ccli, ccli: song.ccli,
audio: song.audio.into(), audio: song.audio.into(),
@ -133,9 +120,9 @@ impl Model<Song> {
impl Song { impl Song {
pub fn get_lyrics(&self) -> Result<Vec<String>> { pub fn get_lyrics(&self) -> Result<Vec<String>> {
let mut lyric_list = Vec::new(); let mut lyric_list = Vec::new();
let raw_lyrics = self.lyrics.as_str(); if let Some(raw_lyrics) = self.lyrics.clone() {
let verse_order = let raw_lyrics = raw_lyrics.as_str();
self.verse_order.clone(); let verse_order = self.verse_order.clone();
let mut lyric_map = HashMap::new(); let mut lyric_map = HashMap::new();
let mut verse_title = String::from(""); let mut verse_title = String::from("");
@ -191,14 +178,13 @@ impl Song {
for lyric in lyric_list.iter() { for lyric in lyric_list.iter() {
debug!(lyric = ?lyric) debug!(lyric = ?lyric)
} }
Ok(lyric_list) Ok(lyric_list)
} else {
Err(eyre!("There are no lyrics"))
}
} }
} }
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::*; use super::*;
@ -206,7 +192,8 @@ mod test {
#[test] #[test]
pub fn test_song_lyrics() { pub fn test_song_lyrics() {
let mut song = Song::default(); let mut song = Song::default();
song.lyrics = "Verse 1 song.lyrics = Some(
"Verse 1
When You found me, When You found me,
I was so blind I was so blind
My sin was before me, My sin was before me,
@ -262,12 +249,23 @@ Other 2
Ending 1 Ending 1
Oh Oh Oh Oh Oh Oh
From the day From the day
You saved my soul".to_string(); You saved my soul"
song.verse_order = "O1 V1 C1 C2 O2 V2 C3 C2 O2 B1 C2 C2 E1 O2".to_string().split(' ').map(|s| s.to_string()).collect(); .to_string(),
);
song.verse_order =
"O1 V1 C1 C2 O2 V2 C3 C2 O2 B1 C2 C2 E1 O2"
.to_string()
.split(' ')
.map(|s| s.to_string())
.collect();
let lyrics = song.get_lyrics(); let lyrics = song.get_lyrics();
match lyrics { match lyrics {
Ok(lyrics) => { assert_eq!(vec!["From the Day\nI Am They", "When You found me,\nI was so blind\nMy sin was before me,\nI was swallowed by pride", "But out of the darkness,\nYou brought me to Your light\nYou showed me new mercy\nAnd opened up my eyes", "From the day\nYou saved my soul\n'Til the very moment\nWhen I come home", "I'll sing, I'll dance,\nMy heart will overflow\nFrom the day\nYou saved my soul", "Where brilliant light\nIs all around\nAnd endless joy\nIs the only sound", "Oh, rest my heart\nForever now\nOh, in Your arms\nI'll always be found", "From the day\nYou saved my soul\n'Til the very moment\nWhen I come home", "I'll sing, I'll dance,\nMy heart will overflow\nFrom the day\nYou saved my soul", "My love is Yours\nMy heart is Yours\nMy life is Yours\nForever", "My love is Yours\nMy heart is Yours\nMy life is Yours\nForever", "From the day\nYou saved my soul\n'Til the very moment\nWhen I come home", "I'll sing, I'll dance,\nMy heart will overflow\nFrom the day\nYou saved my soul", "From the day\nYou saved my soul\n'Til the very moment\nWhen I come home", "I'll sing, I'll dance,\nMy heart will overflow\nFrom the day\nYou saved my soul", "Oh Oh Oh\nFrom the day\nYou saved my soul\n"], lyrics); }, Ok(lyrics) => {
Err(e) => { assert!(false, "{:?}", e) }, assert_eq!(vec!["From the Day\nI Am They", "When You found me,\nI was so blind\nMy sin was before me,\nI was swallowed by pride", "But out of the darkness,\nYou brought me to Your light\nYou showed me new mercy\nAnd opened up my eyes", "From the day\nYou saved my soul\n'Til the very moment\nWhen I come home", "I'll sing, I'll dance,\nMy heart will overflow\nFrom the day\nYou saved my soul", "Where brilliant light\nIs all around\nAnd endless joy\nIs the only sound", "Oh, rest my heart\nForever now\nOh, in Your arms\nI'll always be found", "From the day\nYou saved my soul\n'Til the very moment\nWhen I come home", "I'll sing, I'll dance,\nMy heart will overflow\nFrom the day\nYou saved my soul", "My love is Yours\nMy heart is Yours\nMy life is Yours\nForever", "My love is Yours\nMy heart is Yours\nMy life is Yours\nForever", "From the day\nYou saved my soul\n'Til the very moment\nWhen I come home", "I'll sing, I'll dance,\nMy heart will overflow\nFrom the day\nYou saved my soul", "From the day\nYou saved my soul\n'Til the very moment\nWhen I come home", "I'll sing, I'll dance,\nMy heart will overflow\nFrom the day\nYou saved my soul", "Oh Oh Oh\nFrom the day\nYou saved my soul\n"], lyrics);
}
Err(e) => {
assert!(false, "{:?}", e)
}
} }
} }
@ -275,13 +273,12 @@ You saved my soul".to_string();
pub fn test_db_and_model() { pub fn test_db_and_model() {
let mut song_model = Model::default(); let mut song_model = Model::default();
song_model.load_from_db(); song_model.load_from_db();
if let Some(song) = song_model.get_song(3) { if let Some(song) = song_model.get_item(3) {
let test_song = test_song(); let test_song = test_song();
assert_eq!(test_song.title, song.title); assert_eq!(test_song.title, song.title);
} else { } else {
assert!(false); assert!(false);
} }
} }
#[test] #[test]
@ -292,10 +289,12 @@ You saved my soul".to_string();
song_model.load_from_db(); song_model.load_from_db();
match song_model.update_item(song, 2) { match song_model.update_item(song, 2) {
Ok(()) => assert_eq!(&cloned_song, song_model.get_song(2).unwrap()), Ok(()) => assert_eq!(
&cloned_song,
song_model.get_item(2).unwrap()
),
Err(e) => assert!(false, "{:?}", e), Err(e) => assert!(false, "{:?}", e),
} }
} }
fn test_song() -> Song { fn test_song() -> Song {

View file

@ -1,3 +1,8 @@
use crate::model::Model;
use crate::model::Modeling;
use color_eyre::eyre::eyre;
use color_eyre::eyre::Result;
use std::mem::replace;
use std::path::PathBuf; use std::path::PathBuf;
#[derive(Clone, Debug, Default, PartialEq)] #[derive(Clone, Debug, Default, PartialEq)]
@ -9,9 +14,54 @@ pub struct Video {
looping: bool, looping: bool,
} }
#[derive(Clone, Debug, Default, PartialEq)] impl Model<Video> {}
pub struct VideoModel {
videos: Vec<Video> impl Modeling for Model<Video> {
type Item = Video;
fn add_item(&mut self, item: Self::Item) -> Result<()> {
self.items.push(item);
Ok(())
}
fn add_to_db(&mut self, item: Self::Item) -> Result<()> {
todo!()
}
fn update_item(
&mut self,
item: Self::Item,
index: i32,
) -> Result<()> {
if let Some(current_item) = self.items.get_mut(index as usize)
{
let _old_song = replace(current_item, item);
Ok(())
} else {
Err(eyre!(
"Song doesn't exist in model. Id was {}",
index
))
}
}
fn get_item(&self, index: i32) -> Option<&Self::Item> {
self.items.get(index as usize)
}
fn remove_item(&mut self, index: i32) -> Result<()> {
self.items.remove(index as usize);
Ok(())
}
fn insert_item(
&mut self,
item: Self::Item,
index: i32,
) -> Result<()> {
self.items.insert(index as usize, item);
Ok(())
}
} }
#[cfg(test)] #[cfg(test)]