adding all of our core sql systems
This commit is contained in:
parent
2408404ff4
commit
c9225680c3
20 changed files with 1055 additions and 227 deletions
|
@ -1,5 +1,5 @@
|
|||
use crate::model::Model;
|
||||
use color_eyre::eyre::Result;
|
||||
use super::model::Model;
|
||||
use miette::{Result, miette, IntoDiagnostic};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sqlx::{query_as, SqliteConnection};
|
||||
use std::path::PathBuf;
|
||||
|
@ -13,25 +13,26 @@ pub struct Image {
|
|||
}
|
||||
|
||||
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!", id as "id: i32" from images"#).fetch_all(&mut self.db).await;
|
||||
match result {
|
||||
Ok(v) => {
|
||||
for image in v.into_iter() {
|
||||
let _ = self.add_item(image);
|
||||
}
|
||||
pub async fn load_from_db(&mut self) {
|
||||
let result = query_as!(
|
||||
Image,
|
||||
r#"SELECT title as "title!", file_path as "path!", id as "id: i32" 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 images: {e}"),
|
||||
}
|
||||
});
|
||||
Err(e) => error!("There was an error in converting images: {e}"),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pub async fn get_image_from_db(database_id: i32, db: &mut SqliteConnection) -> Result<Image> {
|
||||
Ok(query_as!(Image, r#"SELECT title as "title!", filePath as "path!", id as "id: i32" from images where id = ?"#, database_id).fetch_one(db).await?)
|
||||
Ok(query_as!(Image, r#"SELECT title as "title!", file_path as "path!", id as "id: i32" from images where id = ?"#, database_id).fetch_one(db).await.into_diagnostic()?)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
@ -47,37 +48,38 @@ mod test {
|
|||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
pub fn test_db_and_model() {
|
||||
let mut image_model: Model<Image> = Model::default();
|
||||
image_model.load_from_db();
|
||||
#[tokio::test]
|
||||
pub async fn test_db_and_model() {
|
||||
let mut image_model: Model<Image> = Model {
|
||||
items: vec![],
|
||||
db: crate::core::model::get_db().await
|
||||
};
|
||||
image_model.load_from_db().await;
|
||||
dbg!(&image_model.items);
|
||||
if let Some(image) = image_model.find(|i| i.id == 3) {
|
||||
let test_image = test_image("nccq5".into());
|
||||
dbg!(&test_image);
|
||||
assert_eq!(test_image.title, image.title);
|
||||
} else {
|
||||
assert!(false);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
pub fn test_add_image() {
|
||||
#[tokio::test]
|
||||
pub async fn test_add_image() {
|
||||
let image = test_image("A new image".into());
|
||||
let mut image_model: Model<Image> = Model::default();
|
||||
let mut image_model: Model<Image> = Model {
|
||||
items: vec![],
|
||||
db: crate::core::model::get_db().await
|
||||
};
|
||||
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.find(|i| i.id == 0).unwrap());
|
||||
assert_ne!(
|
||||
&new_image,
|
||||
image_model.find(|i| i.id == 0).unwrap()
|
||||
);
|
||||
assert_ne!(&new_image, image_model.find(|i| i.id == 0).unwrap());
|
||||
}
|
||||
Err(e) => assert!(
|
||||
false,
|
||||
"There was an error adding the image: {:?}",
|
||||
e
|
||||
),
|
||||
Err(e) => assert!(false, "There was an error adding the image: {:?}", e),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue