moving to generic based models instead of traits
This commit is contained in:
parent
8f065380aa
commit
7a8e6c41cd
4 changed files with 150 additions and 140 deletions
|
@ -1,11 +1,68 @@
|
|||
use std::path::PathBuf;
|
||||
use sqlx::query_as;
|
||||
use tracing::error;
|
||||
use crate::model::Model;
|
||||
|
||||
#[derive(Clone, Debug, Default, PartialEq)]
|
||||
pub struct Image {
|
||||
_title: String,
|
||||
pub title: String,
|
||||
pub path: PathBuf,
|
||||
}
|
||||
|
||||
impl Model<Image> {
|
||||
pub fn load_from_db(&mut self) {
|
||||
let rt = tokio::runtime::Runtime::new().unwrap();
|
||||
rt.block_on(async {
|
||||
let result = query_as!(Image, r#"SELECT title as "title!", filePath as "path!" from images"#).fetch_all(&mut self.db).await;
|
||||
match result {
|
||||
Ok(v) => {
|
||||
for image in v.into_iter() {
|
||||
let _ = self.add_item(image);
|
||||
}
|
||||
}
|
||||
Err(e) => error!("There was an error in converting songs: {e}"),
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
use pretty_assertions::{assert_eq, assert_ne};
|
||||
|
||||
fn test_image(title: String) -> Image {
|
||||
Image {
|
||||
title,
|
||||
path: PathBuf::from("~/pics/camprules2024.mp4"),
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
pub fn test_image() {
|
||||
assert_eq!(true, true)
|
||||
pub fn test_db_and_model() {
|
||||
let mut image_model: Model<Image> = Model::default();
|
||||
image_model.load_from_db();
|
||||
if let Some(image) = image_model.get_item(3) {
|
||||
let test_image = test_image("ncca4".into());
|
||||
assert_eq!(test_image.title, image.title);
|
||||
} else {
|
||||
assert!(false);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
pub fn test_add_image() {
|
||||
let image = test_image("A new image".into());
|
||||
let mut image_model: Model<Image> = Model::default();
|
||||
let result = image_model.add_item(image.clone());
|
||||
let new_image = test_image("A newer image".into());
|
||||
match result {
|
||||
Ok(_) => {
|
||||
assert_eq!(&image, image_model.get_item(0).unwrap());
|
||||
assert_ne!(&new_image, image_model.get_item(0).unwrap());
|
||||
}
|
||||
Err(e) => assert!(false, "There was an error adding the image: {:?}", e),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue